Jump to content

Get highest value in database?


Maurize

Recommended Posts

Posted

so I have a database.

My question is, how can I get the highest value?

I mean I want to set an ID. The first player who registers gets ID 1, the second ID 2, but how is this possible?

Posted

You are talking about SQL/MySQL?

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted (edited)

hmm, createDB is buggy... database doesn't get created...

addEventHandler( "onResourceStart", resourceRoot, 
function() 
    executeSQLCreateTable( "db_data", "id INT IDENTITY( 1,1 ) PRIMARY KEY, name STRING, code STRING, life INT, armor INT, skin INT, meele INT, gun INT, ammo INT, pX FLOAT, pY FLOAT, pZ FLOAT, rZ FLOAT, int INT" ) 
end ) 

this works. but id is not in table...

Edited by Guest
Posted
executeSQLQuery( "CREATE TABLE db_data( id INT NOT NULL AUTO_INCREMENT, name STRING, code STRING, life INT, armor INT, skin INT, meele INT, gun INT, ammo INT, pX FLOAT, pY FLOAT, pZ FLOAT, rZ FLOAT, int INT )" ) 
    executeSQLQuery( "INSERT INTO db_data( name, code, life, armor, skin, meele, gun, ammo, pX, pY, pZ, rZ, int ) VALUES( '"..getPlayerName( source ).."', '"..code.."', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' )" ) 

error, database cannot be created. syntax error with auto increment.

Posted

SQLite by default has a column called "rowid", you don't have to create another column to store the ID.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

Doesn't work...

addCommandHandler( "getid", 
function( player, cmd, target ) 
if ( getPlayerFromName( target ) ) then 
    outputChatBox( "Das Ziel trägt die ID: "..getSQLData( getPlayerFromName( target ), "db_data", "rowid" ), player, 255, 255, 0 ) 
    end 
end ) 

Posted

Post the function "getSQLData".

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted
function getSQLData( element, db, row ) 
    local db = executeSQLSelect( db, "*", "name = '"..getPlayerName( element ).."'" ) 
if ( db and #db == 1 ) then 
    return db[1][row] 
    end 
end 

Posted
function getSQLData ( element, db, row ) 
    local db = executeSQLQuery ( "SELECT * FROM ? WHERE name = ?", db, getPlayerName ( element ) ) 
    if ( db and #db == 1 ) then 
        return db [ 1 ] [ row ] 
    end 
end 

Try that one instead.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

My bad, I forgot to rename the function, copy the code again, it works ( tested ).

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

Ye it get's values...

addCommandHandler( "getid", 
function( player, cmd, target ) 
if ( getPlayerFromName( target ) ) then 
    outputChatBox( "Das Ziel trägt die ID: "..tonumber( getSQLData( getPlayerFromName( target ), "db_data", "rowid" ) ), player, 255, 255, 0 ) 
    end 
end ) 

error attempt to concatenate nil value :/

Posted

And, is the name of the player in the table?

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

of course it is.

  
executeSQLQuery( "CREATE TABLE IF NOT EXISTS db_data( id INT IDENTITY( 1,1 ) PRIMARY KEY, name STRING, code STRING, life INT, armor INT, skin INT, meele INT, gun INT, ammo INT, pX FLOAT, pY FLOAT, pZ FLOAT, rZ FLOAT, int INT )" ) 
... 
    executeSQLQuery( "INSERT INTO db_data( name, code, life, armor, skin, meele, gun, ammo, pX, pY, pZ, rZ, int ) VALUES( '"..getPlayerName( source ).."', '"..code.."', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' )" ) 

Posted

Have you tried getting another data name? such as "name" instead of "rowid".

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

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