Lloyd Logan Posted January 17, 2014 Posted January 17, 2014 Why does this keep INSERTING even if there is already the same serial in the table? local data = dbPoll(dbQuery(server, "SELECT serial FROM accounts WHERE serial = ?", serial), -1) if data and type (data) == "table" and #data > 0 then dbExec (server, "UPDATE accounts SET serial = ?, WHERE serial = '"..serial.."'", tostring (serialz)) else dbExec (server, "INSERT INTO accounts (serial) VALUES (?)", tostring (serialz)) end If you need an Intermediate scripter feel free to PM me as I will accept "almost" any job, STATUS: UNAVAILABLE SCOTLAND, my hometown, and the Home of GTA!
3NAD Posted January 18, 2014 Posted January 18, 2014 Try This: local data = dbQuery ( server, "SELECT serial FROM accounts WHERE serial = ?", serial ) if #data > 0 then dbExec ( server, "UPDATE accounts SET serial = ? WHERE serial = ?", serial, tostring (serialz)) else dbExec ( server, "INSERT INTO accounts (serial) VALUES (?)", tostring (serialz)) end
Lloyd Logan Posted January 18, 2014 Author Posted January 18, 2014 Try This: local data = dbQuery ( server, "SELECT serial FROM accounts WHERE serial = ?", serial ) if #data > 0 then dbExec ( server, "UPDATE accounts SET serial = ? WHERE serial = ?", serial, tostring (serialz)) else dbExec ( server, "INSERT INTO accounts (serial) VALUES (?)", tostring (serialz)) end It gives me the error; attempt to get length of local 'data' a userdata value If you need an Intermediate scripter feel free to PM me as I will accept "almost" any job, STATUS: UNAVAILABLE SCOTLAND, my hometown, and the Home of GTA!
TAPL Posted January 18, 2014 Posted January 18, 2014 dbPoll is required to get the result of dbQuery.
Lloyd Logan Posted January 18, 2014 Author Posted January 18, 2014 dbPoll is required to get the result of dbQuery. So; function getSerial() local serialz = getPlayerSerial(source) local queryResult = dbQuery ( server, "SELECT serial FROM accounts WHERE serial = ?", serialz ) local database = dbPoll(queryResult, -1) if #database > 0 then dbExec ( server, "UPDATE accounts SET serial = ? WHERE serial = ?", serial, tostring (serialz)) else dbExec ( server, "INSERT INTO accounts (serial) VALUES (?)", tostring (serialz)) end end addEventHandler("onPlayerJoin", getRootElement(), getSerial) If you need an Intermediate scripter feel free to PM me as I will accept "almost" any job, STATUS: UNAVAILABLE SCOTLAND, my hometown, and the Home of GTA!
TAPL Posted January 18, 2014 Posted January 18, 2014 Why you do update the serial if it already the same in the database? You should only insert it if it doesn't exists. function getSerial() local serialz = getPlayerSerial(source) local database = dbPoll(dbQuery(server, "SELECT serial FROM accounts WHERE serial = ?", serialz), -1) if database and type(database) == "table" and #database == 0 then dbExec ( server, "INSERT INTO accounts (serial) VALUES (?)", serialz) end end addEventHandler("onPlayerJoin", root, getSerial) There also another way, but i am not sure if it right or not, you can try it. function getSerial() local serialz = getPlayerSerial(source) dbExec(server, "INSERT INTO accounts (serial) VALUES (serialz) WHERE NOT EXISTS (SELECT serial FROM accounts WHERE serial = '"..serialz.."'") end addEventHandler("onPlayerJoin", root, getSerial)
Lloyd Logan Posted January 18, 2014 Author Posted January 18, 2014 Why you do update the serial if it already the same in the database?You should only insert it if it doesn't exists. function getSerial() local serialz = getPlayerSerial(source) local database = dbPoll(dbQuery(server, "SELECT serial FROM accounts WHERE serial = ?", serialz), -1) if database and type(database) == "table" and #database == 0 then dbExec ( server, "INSERT INTO accounts (serial) VALUES (?)", serialz) end end addEventHandler("onPlayerJoin", root, getSerial) There also another way, but i am not sure if it right or not, you can try it. The first on works! Thank you! function getSerial() local serialz = getPlayerSerial(source) dbExec(server, "INSERT INTO accounts (serial) VALUES (serialz) WHERE NOT EXISTS (SELECT serial FROM accounts WHERE serial = '"..serialz.."'") end addEventHandler("onPlayerJoin", root, getSerial) If you need an Intermediate scripter feel free to PM me as I will accept "almost" any job, STATUS: UNAVAILABLE SCOTLAND, my hometown, and the Home of GTA!
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