TrickyTommy

MySQL script

Recommended Posts

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?

Share this post


Link to post

Sorry, we misunderstood each other. Let me show it to you on the following picture:
jjbKex9.png
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)

Share this post


Link to post
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

 

  • Thanks 1

Share this post


Link to post
_dbQuery = dbQuery
function dbQuery(query, ...)
	return _dbQuery(connection, query, ...)
end

Basis from quindo.

 

If you want to fill in multiple values.

Edited by IIYAMA
  • Thanks 1

Share this post


Link to post
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?

Share this post


Link to post

test it first with the default function.

If that is OK.

Then debug the arguments.

iprint(connection, query, ...)

 

  • Thanks 1

Share this post


Link to post
10 minutes ago, IIYAMA said:

test it first with the default function.

If that is OK.

Then debug the arguments.


iprint(connection, query, ...)

 

6L01gap.jpg

sorry, i dont get it, where do i need to put the iprint line?

Edited by TrickyTommy
  • Like 1

Share this post


Link to post

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.

 

 

Share this post


Link to post
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

 

Share this post


Link to post
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

 

  • Thanks 1

Share this post


Link to post

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.