Page 1 of 1
update Username
Posted: Wed Nov 24, 2021 5:09 pm
by Samuele
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!
Re: update Username
Posted: Wed Nov 24, 2021 6:04 pm
by Klaus
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)
Re: update Username
Posted: Wed Nov 24, 2021 8:53 pm
by Samuele
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?
Re: update Username
Posted: Wed Nov 24, 2021 9:41 pm
by Samuele
ok, sorry i think i got it, thanks!
Re: update Username
Posted: Wed Nov 24, 2021 11:29 pm
by Samuele
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!
Re: update Username
Posted: Thu Nov 25, 2021 10:09 am
by Klaus
put "UPDATE " & tTableName & " SET Username= " & ' & tUsername & ' & " WHERE
PersonId= " &
tUsername into tSQL
Sure?
Re: update Username
Posted: Thu Nov 25, 2021 10:26 am
by Ajm
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.
Re: update Username
Posted: Thu Nov 25, 2021 10:31 am
by Klaus
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!
Re: update Username
Posted: Thu Nov 25, 2021 11:55 am
by Samuele
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!
Re: update Username
Posted: Thu Nov 25, 2021 12:06 pm
by Klaus
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!
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
...
Re: update Username
Posted: Sun Nov 28, 2021 6:54 pm
by Samuele
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!
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
Re: update Username
Posted: Sun Nov 28, 2021 7:53 pm
by Klaus
Samuele wrote: ↑Sun Nov 28, 2021 6:54 pm
strange, because it works... so i don't want to change it
Yes, sure, best you can do if something works.
I meant my hints for future use...
Re: update Username
Posted: Mon Nov 29, 2021 12:41 pm
by Samuele
thanks