Maurize Posted May 27, 2014 Posted May 27, 2014 Hey, I'm working on my SQL Functions... So, my question is, if it's possible to make something like this: function setSQLData( element, row, data ) local db = executeSQLQuery( "SELECT * FROM ? WHERE name = ?", "db", getPlayerName( element ) ) if ( db and #db == 1 ) then executeSQLUpdate( "db", row.." = '"..data.."'", "name = '"..getPlayerName( element ).."'" ) else executeSQLQuery( "INSERT INTO db( '..row..' ) VALUES( '..data..' ) WHERE name = '"..getPlayerName( element ).."'" ) end end So functions requests if data is already there and if not the data gets insert so I don't have to make a single large insert execute...
Gtagasje Posted May 28, 2014 Posted May 28, 2014 If you mean you want to do that for every sql insert you do, you could use this perhaps. Keep in mind that the old value can't be a player element for example, if you want it to be a player's name you should send the name of the player with the function, and not the player element. function setSQLData(column, oldvalue, newvalue) local dbname = "YOUR DATABASE NAME HERE" -- Fill in the database name local column, oldvalue, newvalue if type(column) == "string" then if type(oldvalue) ~= "string" then -- Make sure everything is a string oldvalue = tostring(oldvalue) end if type(newvalue) ~= "string" then -- Idem newvalue = tostring(newvalue) end local sh = executeSQLQuery("SELECT * FROM ?? WHERE ? = ?", dbname, column, oldvalue) -- Check if the entry already exists if #sh == 0 or not sh then local ih = executeSQLQuery("INSERT INTO ??(?) VALUES(?)", dbname, column, newvalue) -- Nope, insert it return ih else local uh = executeSQLQuery("UPDATE ?? SET ? = ? WHERE ?", dbname, column, newvalue, oldvalue) -- Yes, update it return uh end else return false -- Woops, the column name you provided is not a string end return false end
Maurize Posted May 28, 2014 Author Posted May 28, 2014 and this way? function setSQLData( element, row, data ) local db = executeSQLQuery( "SELECT * FROM ? WHERE name = ?", "db", getPlayerName( element ) ) if ( db and #db == 1 ) then executeSQLQuery( "UPDATE db SET '"..row.."' = '"..data.."' WHERE name = '"..getPlayerName( element ).."'", "db", getPlayerName( element ) ) else executeSQLQuery( "INSERT INTO db( '"..row.."' ) VALUES( '"..data.."' )", "db", getPlayerName( element ) ) end end
Castillo Posted May 31, 2014 Posted May 31, 2014 function setSQLData ( element, row, data ) local db = executeSQLQuery ( "SELECT * FROM ?? WHERE name = ?", "db", getPlayerName ( element ) ) if ( db and #db == 1 ) then executeSQLQuery ( "UPDATE db SET ?? = ? WHERE name = ?", row, data, getPlayerName ( element ) ) else executeSQLQuery ( "INSERT INTO db ( '"..row.."' ) VALUES ( ? )", data, getPlayerName ( element ) ) end end Try it.
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