update Username

Creating desktop or client-server database solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Samuele
Posts: 79
Joined: Mon Oct 11, 2021 7:05 pm

update Username

Post by Samuele » Wed Nov 24, 2021 5:09 pm

hi, in my stack the user at the opening, writes a Username, but he can change it afterwards, i was thinking, how can i update that on sql? then i had an adea to make an Auto Increment record that is like the id of the user, but i don't know how can i take that data (the ID) when its created, and then maybe from there i can put it on a field so then every time the username changes his username the record can be updated (because every Username has also a High Score record).
so basically the question is, how can i know what's the user ID after he changed his Username?
thanks!

Klaus
Posts: 12591
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: update Username

Post by Klaus » Wed Nov 24, 2021 6:04 pm

When the user enters his name (before changing it), how do you check if that user is valid?
I guess you do some SQL here, so you should fetch the ID of the user, too.
Then store it in a (hidden) field or custom property, then use it late to update the username.

Pseudo SQL code:

Code: Select all

update 'usernames' set 'username' = (the new username) where ID = (the value from field or custom property)

Samuele
Posts: 79
Joined: Mon Oct 11, 2021 7:05 pm

Re: update Username

Post by Samuele » Wed Nov 24, 2021 8:53 pm

yes i do, i check if it's already in the database (in use)

Code: Select all

put "SELECT EXISTS(SELECT * from " & tTableName & " WHERE UserName = " & ' & _guserName & ' & ")" into tSQL
   put revDataFromQuery(tab, cr, gConnectionID, tSQL) into tAnswer
   put tAnswer into field "gg"
   if tAnswer = "1"
   then 
      answer "Username already exists"
      #if the environment is "mobile" then
         set the text of widget "EnterUsername" to empty
         exit to top
      else
how can i get from here the id?

Samuele
Posts: 79
Joined: Mon Oct 11, 2021 7:05 pm

Re: update Username

Post by Samuele » Wed Nov 24, 2021 9:41 pm

ok, sorry i think i got it, thanks!

Samuele
Posts: 79
Joined: Mon Oct 11, 2021 7:05 pm

Re: update Username

Post by Samuele » Wed Nov 24, 2021 11:29 pm

i've put this code in a button "OK" that appears when the user wants to change the username, but it doesn't work, any ideas? thanks!

Code: Select all

put "Persons" into tTableName
   put "UserName, HighScore" into tFields
   put field "UserName" of card "HomeCard" into tUsername
   put field "ID" of card "HomeCard" into tID
   put "UPDATE " & tTableName & " SET Username= " & ' & tUsername & ' & " WHERE PersonId= " & tUsername into tSQL
   revExecuteSQL gConnectionID, tSQL
in the field ID there is the id, i checked, so i don't know what's the problem because it doesn't even give an error, but it doesn't update the username :?
thanks!

Klaus
Posts: 12591
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: update Username

Post by Klaus » Thu Nov 25, 2021 10:09 am

put "UPDATE " & tTableName & " SET Username= " & ' & tUsername & ' & " WHERE PersonId= " & tUsername into tSQL

Sure? :D

Ajm
Posts: 10
Joined: Tue Mar 04, 2014 7:21 pm

Re: update Username

Post by Ajm » Thu Nov 25, 2021 10:26 am

Hi,

Shouldn't the single quotes be inside the double quotes as well.

Code: Select all

put "Persons" into tTableName
   put "UserName, HighScore" into tFields
   put field "UserName" of card "HomeCard" into tUsername
   put field "ID" of card "HomeCard" into tID
   put "UPDATE " & tTableName & " SET Username= '" & tUsername & "' WHERE PersonId= '" & tUsername & "'" into tSQL
   revExecuteSQL gConnectionID, tSQL
   
But as Klaus points out that maybe your username column in the database is not called PersonID.
Regards

Andy
DB Integration Limited

Klaus
Posts: 12591
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: update Username

Post by Klaus » Thu Nov 25, 2021 10:31 am

Shouldn't the single quotes be inside the double quotes as well.
If this is a question, then the answer is YES! :-)

I always use these functions when it come to work with databases, of course helpful in other situations, too!
They will QUOTE or SINGEL QUOTE a passed string:

Code: Select all

## QUotes:
function q tString
   return QUOTE & tString & QUOTE
end q

## Single quotes
function q2 tString
   return "'" & tString & "'"
end q2
Since adding quotes to more than one string manually is a PITA! 8)

Samuele
Posts: 79
Joined: Mon Oct 11, 2021 7:05 pm

Re: update Username

Post by Samuele » Thu Nov 25, 2021 11:55 am

yes , right i changed it

Code: Select all

  put field "TypeUser" into tUsername
      put field "ID" of card "HomeCard" into tID
      put "UPDATE " & tTableName & " SET Username= " & ' & tUsername & ' & " WHERE PersonId= " & tID into tSQL
      revExecuteSQL gConnectionID, tSQL
and for the single quotes they are inside the double quotes as well.
now it works, thanks!

Klaus
Posts: 12591
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: update Username

Post by Klaus » Thu Nov 25, 2021 12:06 pm

Samuele wrote:
Thu Nov 25, 2021 11:55 am
...
and for the single quotes they are inside the double quotes as well.
...
Not they aren't! 8)

Code: Select all

 " & ' & tUsername & ' & "...
 # This is OUTSIDE the quotes
With my functions this is a snap and not prone to errors!

Code: Select all

...
## Hint: do not use SPACES, use the double &&
put "UPDATE" && q2(tTableName) && "SET Username= " & q2(tUsername) && "WHERE PersonId=" & q2(tID) into tSQL
...

Samuele
Posts: 79
Joined: Mon Oct 11, 2021 7:05 pm

Re: update Username

Post by Samuele » Sun Nov 28, 2021 6:54 pm

Klaus wrote:
Thu Nov 25, 2021 12:06 pm
Samuele wrote:
Thu Nov 25, 2021 11:55 am
...
and for the single quotes they are inside the double quotes as well.
...
Not they aren't! 8)

Code: Select all

 " & ' & tUsername & ' & "...
 # This is OUTSIDE the quotes
With my functions this is a snap and not prone to errors!

Code: Select all

...
## Hint: do not use SPACES, use the double &&
put "UPDATE" && q2(tTableName) && "SET Username= " & q2(tUsername) && "WHERE PersonId=" & q2(tID) into tSQL
...
strange, because it works... so i don't want to change it :wink:

Klaus
Posts: 12591
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: update Username

Post by Klaus » Sun Nov 28, 2021 7:53 pm

Samuele wrote:
Sun Nov 28, 2021 6:54 pm
strange, because it works... so i don't want to change it :wink:
Yes, sure, best you can do if something works.
I meant my hints for future use... :D

Samuele
Posts: 79
Joined: Mon Oct 11, 2021 7:05 pm

Re: update Username

Post by Samuele » Mon Nov 29, 2021 12:41 pm

:lol: thanks

Post Reply

Return to “Databases”