Jump to content

Make SQL Data Request easier?


Maurize

Recommended Posts

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...

Link to comment

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 

Link to comment

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 

Link to comment
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.

Link to comment

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.
×
×
  • Create New...