Lloyd Logan Posted January 17, 2014 Share 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 Link to comment
3NAD Posted January 18, 2014 Share 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 Link to comment
Lloyd Logan Posted January 18, 2014 Author Share 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 Link to comment
TAPL Posted January 18, 2014 Share Posted January 18, 2014 dbPoll is required to get the result of dbQuery. Link to comment
Lloyd Logan Posted January 18, 2014 Author Share 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) Link to comment
TAPL Posted January 18, 2014 Share 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) Link to comment
Lloyd Logan Posted January 18, 2014 Author Share 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) 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