Hero192 Posted September 30, 2015 Share Posted September 30, 2015 (edited) Hey guys, im getting a vehicle color ( r, g, b) to storing it to a database but it save just R others no, i mean g and b Here is my code, can anyone give me a hand? function addToDatabase(vehicleName, vehicleid, vehicleType, r, g, b) if not doesVehExists(vehicleName) then local color = r..","..g..","..b dbExec(connection, "INSERT INTO vehicle VALUES ( ?,?,?,? )", tostring(vehicleName),tostring(vehicleid),tostring(vehicleType), tostring(color)) return true else return false end end And i want to get the veh color later so please tell me the solution of this Edited September 30, 2015 by Guest Link to comment
ozulus Posted September 30, 2015 Share Posted September 30, 2015 I don't know the first problem but maybe i can help you about getting vehicle color into db. You must edit dbQuery function cuz i dont know your table(s) name. Maybe it wont work (i wrote that on my cellphone) function getVehicleColorFromDb(vehicleName) local query = dbQuery( connection, "SELECT color FROM `vehicle` WHERE vehicleName='".. tostring(vehicleName) .."'") local result, numrows, errmsg = dbPoll( query, -1 ) if (result) then local vehicleColorString = tostring(result[1]["color"]) end local vehicleColorTable = split(vehicleColorString, string.byte(',')) if vehicleColorTable then return tonumber(vehicleColorTable[1]), tonumber(vehicleColorTable[2]), tonumber(vehicleColorTable[3]) -- r, g, b end return false end Link to comment
Noki Posted October 1, 2015 Share Posted October 1, 2015 function doesVehExists(vehicleName) local qh = dbQuery(connection, "SELECT * FROM `vehicle` WHERE `vehicleName`=? LIMIT 1", vehicleName) local result = dbPoll(qh, -1) if (result and #result > 0) then return true end return false end function addToDatabase(vehicleName, vehicleid, vehicleType, r, g, b) if (not doesVehExists(vehicleName)) then local color = toJSON({r, g, b}) dbExec(connection, "INSERT INTO `vehicle` VALUES (?, ?, ?, ?)", tostring(vehicleName), tostring(vehicleid), tostring(vehicleType), color) return true end return false end function getVehicleColor(vehicleid) local qh = dbQuery(connection, "SELECT `color` FROM `vehicle` WHERE `vehicleid`=? LIMIT 1", vehicleid) local result = dbPoll(qh, -1) if (result and #result > 0) then return tonumber(unpack(fromJSON(result[1].color))) end return false end I ended up using JSON to store the colour, because there are multiple values and it's better than slabbing each colour in a separate column. I also don't know your column names, so make sure to replace them in the code. Link to comment
Hero192 Posted October 1, 2015 Author Share Posted October 1, 2015 Yes good way but it give error bad argument #1 to 'unpack' (table expected, got nil) in this part function getVehiclesColor(vehicleid) local qh = dbQuery(connection, "SELECT `color` FROM `vehicle` WHERE `vehicleid`=? LIMIT 1", vehicleid) local result = dbPoll(qh, -1) if (result and #result > 0) then return tonumber(unpack(fromJSON(result[1].color))) end return false end I do like that to get the vehicle color but not working local r,g,b = getVehiclesColor(vehicleid) -- i define the vehicle id setVehicleColor(veh,r,g,b) Link to comment
TAPL Posted October 1, 2015 Share Posted October 1, 2015 Have you updated your database with JSON string or it still the old string? Also you shouldn't use tonumber within unpack. Link to comment
Hero192 Posted October 1, 2015 Author Share Posted October 1, 2015 Have you updated your database with JSON string or it still the old string? Also you shouldn't use tonumber within unpack. I didn't update anything, can you fix that with example Link to comment
Noki Posted October 1, 2015 Share Posted October 1, 2015 Then you need to update it in the database with the new JSON string. As TAPL said, remove the 'tonumber' next to 'unpack'. That was my bad there. Your code should end up looking like this. function doesVehExists(vehicleName) local qh = dbQuery(connection, "SELECT * FROM `vehicle` WHERE `vehicleName`=? LIMIT 1", vehicleName) local result = dbPoll(qh, -1) if (result and #result > 0) then return true end return false end function addToDatabase(vehicleName, vehicleid, vehicleType, r, g, b) if (not doesVehExists(vehicleName)) then local color = toJSON({r, g, b}) dbExec(connection, "INSERT INTO `vehicle` VALUES (?, ?, ?, ?)", tostring(vehicleName), tostring(vehicleid), tostring(vehicleType), color) return true end return false end function getVehicleColor(vehicleid) local qh = dbQuery(connection, "SELECT `color` FROM `vehicle` WHERE `vehicleid`=? LIMIT 1", vehicleid) local result = dbPoll(qh, -1) if (result and #result > 0) then return unpack(fromJSON(result[1].color)) end return false end Update the database with the JSON string and get back to us. Link to comment
Hero192 Posted October 1, 2015 Author Share Posted October 1, 2015 But when i try to get r,g,b from the function it tell me the same error error bad argument #1 to 'unpack' (table expected, got nil) local r,g,b = getVehiclesColor(vehicleid) Link to comment
Noki Posted October 1, 2015 Share Posted October 1, 2015 local qh = dbQuery(connection, "SELECT `color` FROM `vehicle` WHERE `vehicleid`=? LIMIT 1", vehicleid) return unpack(fromJSON(result[1].color)) Replace 'color' (and possibly `vehicleid` in the SQL query itself) with the name of the corresponding column. Link to comment
Hero192 Posted October 1, 2015 Author Share Posted October 1, 2015 local qh = dbQuery(connection, "SELECT `color` FROM `vehicle` WHERE `vehicleid`=? LIMIT 1", vehicleid) return unpack(fromJSON(result[1].color)) Replace 'color' (and possibly `vehicleid` in the SQL query itself) with the name of the corresponding column. Show me example please, i won't be wrong Link to comment
Noki Posted October 1, 2015 Share Posted October 1, 2015 No, I gave already given you the code. What you need to do is fill in the bits I have told you to. I don't know the setup of what you're working with, only you do. All you need to do is replace some words here and there. Link to comment
Hero192 Posted October 1, 2015 Author Share Posted October 1, 2015 Yes i know but my problem is toJSON don't save all the numbers it save only r but g, b no i checked the mysql table in the color column and i saw that Link to comment
Hero192 Posted October 1, 2015 Author Share Posted October 1, 2015 Yes i know but my problem is toJSON don't save all the numbers it save only r but g, b no i checked the mysql table in the color column and i saw that Anyone can help me to solve this? Link to comment
TAPL Posted October 1, 2015 Share Posted October 1, 2015 Try to output r, g and b to the chat, those in this function: function addToDatabase(vehicleName, vehicleid, vehicleType, r, g, b) Link to comment
Hero192 Posted October 1, 2015 Author Share Posted October 1, 2015 It's working, i sent the r,g,b from client to server side and i tested it by using outputChatBox function the problem is when i use toJSON to store the r,g,b colors in one columns at the database of mysql it store just r but g and b no so when i try to return the color it return false so here where is the problem exist Link to comment
TAPL Posted October 1, 2015 Share Posted October 1, 2015 Post the code where you create the sql table. 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