fairyoggy Posted January 21, 2020 Share Posted January 21, 2020 (edited) How do I implement the following action. There is a row in the database with the name "uid" How to check if there is no such UID then writes an error. Example: User type in edit UID "2" but it is not in the database, there is only "1" then The message appears with error. if user type in edit UID "1" then The message appears without error. --server dbSecurityConnection = dbConnect( 'sqlite', 'test.db') dbExec( dbSecurityConnection, ' CREATE TABLE if not exists `accs` (uid INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, login) ' ) function date(uid, userlogin) dbExec( dbSecurityConnection, ' INSERT INTO `accs` VALUES(?, ? ) ', uid, userlogin) end Edited January 21, 2020 by slapztea 1 Link to comment
Moderators IIYAMA Posted January 21, 2020 Moderators Share Posted January 21, 2020 Use the SELECT query. It is not as if you should combine queries. The error is given because it is protecting itself from item duplication. Which means that it already went wrong. SELECT uid FROM accs WHERE uid = ? LIMIT 1 1 Link to comment
fairyoggy Posted January 21, 2020 Author Share Posted January 21, 2020 10 minutes ago, IIYAMA said: Use the SELECT query. It is not as if you should combine queries. The error is given because it is protecting itself from item duplication. Which means that it already went wrong. SELECT uid FROM accs WHERE uid = ? LIMIT 1 It was just an example How to write it right? I need to know how to write a condition for checking in the database. need to check on the line "UID" on datebase and if this is not found then If in "uid" there will be such values as "1" and "2" how to work with other? I guess in the line "Limit 1" work is underway on this figure, but if it is in the database? I can be wrong, I have not worked with it yet. It is necessary that there is a check for "uid" and if this is not the case, then the method of exclusion In this case, there are no more digits 3 and above , but if appear "3" in uid how then to write correctly? Then there should be a check if This is in the database. 1 Link to comment
Moderators IIYAMA Posted January 21, 2020 Moderators Share Posted January 21, 2020 2 hours ago, slapztea said: If in "uid" there will be such values as "1" and "2" how to work with other? I am not sure what uid is. But normally you would have 1 column called: id Which auto increment when items are added. This value makes sure that each item added is unique. In some cases for example a login system you can also set the username as primary key, which is to prevents duplication for the usernames. So make sure that you understand your own data. LIMIT is used to select only a maximum items. If you need 1 item, you put the limit to 1, so that the database knows that it has to stop when it found what it is looking for. - Selecting a max amount of items. - Increase the performance. 1 Link to comment
fairyoggy Posted January 22, 2020 Author Share Posted January 22, 2020 (edited) 19 hours ago, IIYAMA said: I am not sure what uid is. But normally you would have 1 column called: id Which auto increment when items are added. This value makes sure that each item added is unique. In some cases for example a login system you can also set the username as primary key, which is to prevents duplication for the usernames. So make sure that you understand your own data. LIMIT is used to select only a maximum items. If you need 1 item, you put the limit to 1, so that the database knows that it has to stop when it found what it is looking for. - Selecting a max amount of items. - Increase the performance. UID works just like a normal id. I’ll ask a little different. function fun1 () dbSecurityConnection = dbConnect( 'sqlite', 'accs.db') local getAnswerData = dbQuery( dbSecurityConnection, ' SELECT `email` FROM `accs` WHERE uid = ? ', 1 ) local result = dbPoll( getAnswerData, -1 ) local finduid = result[1].email outputChatBox ( ""..finduid.."" ) end addCommandHandler ( "tt", fun1 ) If I use this code, I use "1" in line local getAnswerData = dbQuery( dbSecurityConnection, ' SELECT `email` FROM `accs` WHERE uid = ? ', 1 ) I will get the output in chat: "mail.ru" if i use "2" in line local getAnswerData = dbQuery( dbSecurityConnection, ' SELECT `email` FROM `accs` WHERE uid = ? ', 2 ) i will get the output in chat: "dat.ru" It all works as planned BUT if i use "3" in line: local getAnswerData = dbQuery( dbSecurityConnection, ' SELECT `email` FROM `accs` WHERE uid = ? ', 3 ) As you can see on the screen of the database, there is no such line(with uid "3") A question: How can I make it so that when I type a non-existent UID in the database, I get an error in the chat NOT in code like this: I wanted to use something like this, but it not working: function fun1 () dbSecurityConnection = dbConnect( 'sqlite', 'accs.db') local getAnswerData = dbQuery( dbSecurityConnection, ' SELECT `email` FROM `accs` WHERE uid = ? ', 3 ) if getAnswerData then local result = dbPoll( getAnswerData, -1 ) local finduid = result[1].email outputChatBox ( ""..finduid.."" ) else outputChatBox ( "error" ) end end addCommandHandler ( "tt", fun1 ) Edited January 22, 2020 by slapztea 1 Link to comment
Moderators IIYAMA Posted January 22, 2020 Moderators Share Posted January 22, 2020 (edited) 10 minutes ago, slapztea said: but it not working: You can't index a non existing table. So you have to check if it exist before indexing a second time. if result[1] then Edited January 22, 2020 by IIYAMA 1 Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now