TrickyTommy Posted October 31, 2017 Share Posted October 31, 2017 connection = dbConnect( "mysql", "dbname="..mySQLDetails["db"]..";host="..mySQLDetails["host"].."", ""..mySQLDetails["acc"].."", ""..mySQLDetails["pass"].."", "share=1" ) function dbQuery(query, values) qh = dbQuery(connection, query, values) end Hi! I am making a mysql script, and i would not like to do the dbConnect stuff and things like this everytime. But after the query, there might be values, not only just one. So for example in the query, i want to insert 2 things, and it would look like this: -------------------------------------------------string query------------------------------------------- ---------------------values------------- dbQuery(connection, "INSERT INTO users (username, password) VALUES (?, ?)", Username, md5(Password)) But the password would not be inserted! I know that i'll have to use table. but how? Link to comment
Moderators IIYAMA Posted October 31, 2017 Moderators Share Posted October 31, 2017 executeSQLQuery("CREATE TABLE IF NOT EXISTS `users` (username TEXT, password TEXT)") https://wiki.multitheftauto.com/wiki/ExecuteSQLQuery This will create a table `users` if it doesn't exist already. Link to comment
TrickyTommy Posted October 31, 2017 Author Share Posted October 31, 2017 Sorry, we misunderstood each other. Let me show it to you on the following picture: I want to make some king of a "useful function", and i would like to use this instead of dbQuery, because the connection was already declared here, and i would not like to declare an other connection in an other script. So i can export the function and use it like exports.db:sqlQuery(string query, variables for the query) Link to comment
quindo Posted October 31, 2017 Share Posted October 31, 2017 connection = dbConnect( "mysql", "dbname="..mySQLDetails["db"]..";host="..mySQLDetails["host"].."", ""..mySQLDetails["acc"].."", ""..mySQLDetails["pass"].."", "share=1" ) _dbQuery = dbQuery function dbQuery(query, values) return _dbQuery(connection,query,values) end 1 Link to comment
Moderators IIYAMA Posted October 31, 2017 Moderators Share Posted October 31, 2017 (edited) _dbQuery = dbQuery function dbQuery(query, ...) return _dbQuery(connection, query, ...) end Basis from quindo. If you want to fill in multiple values. Edited October 31, 2017 by IIYAMA 1 Link to comment
TrickyTommy Posted October 31, 2017 Author Share Posted October 31, 2017 function sqlQuery(query, ...) return dbQuery(connection, query, ...) end local Query = sqlQuery ("INSERT INTO test (asd1, asd2) VALUES (?, ?)", "asd1", "asd2") if Query then outputServerLog ("true") else outputServerLog ("false") end it is not inserting to the database, however it returns true. what have gone wrong? Link to comment
Moderators IIYAMA Posted October 31, 2017 Moderators Share Posted October 31, 2017 test it first with the default function. If that is OK. Then debug the arguments. iprint(connection, query, ...) 1 Link to comment
TrickyTommy Posted October 31, 2017 Author Share Posted October 31, 2017 (edited) 10 minutes ago, IIYAMA said: test it first with the default function. If that is OK. Then debug the arguments. iprint(connection, query, ...) sorry, i dont get it, where do i need to put the iprint line? Edited October 31, 2017 by TrickyTommy 1 Link to comment
Moderators IIYAMA Posted October 31, 2017 Moderators Share Posted October 31, 2017 Yes there, that is correct. Values are looking correct. Did you test it with the default function? And did that work? And please post more of the updated code. Link to comment
TrickyTommy Posted October 31, 2017 Author Share Posted October 31, 2017 function sqlQuery(query, ...) return dbQuery(connection, query, ...) end iprint(connection, "INSERT INTO test (asd1, asd2) VALUES (?, ?)", "asd1", "asd2") local Query = sqlQuery ("INSERT INTO test (asd1, asd2) VALUES (?, ?)", "asd1", "asd2") if Query then outputServerLog ("true") else outputServerLog ("false") end i might be dumb, and messed it up. not sure if it's correct. FIXED: re-created the table, it is working now. function sqlQuery(query, ...) return dbQuery(connection, query, ...) end local Query = sqlQuery ("INSERT INTO test (test1, test2) VALUES (?, ?)", "Successsss 1", "Successsss 2") if Query then outputServerLog ("true") else outputServerLog ("false") end Link to comment
Moderators IIYAMA Posted October 31, 2017 Moderators Share Posted October 31, 2017 function sqlQuery(query, ...) iprint(connection, query, ...) -- < iprint here return dbQuery(connection, query, ...) end -- do the original query function, if something goes wrong then it is not a problem with your utility function. local qh = dbQuery(connection, "INSERT INTO test (asd1, asd2) VALUES (?, ?)", "asd1 original", "asd2 original") dbFree ( qh ) -- do yours. local Query = sqlQuery ("INSERT INTO test (asd1, asd2) VALUES (?, ?)", "asd1", "asd2") dbFree ( Query ) if Query then outputServerLog ("true") else outputServerLog ("false") end 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