aka Blue Posted May 7, 2017 Share Posted May 7, 2017 Buenas. La GM de Paradise tiene la opción de devolver la ID la cual corresponde a x dato insertado en una base de datos. Me gustaría saber cómo obtener este dato usando funciones SQLite. local vehicleID, error = exports.sql:query_insertid( "INSERT INTO vehicles (model, posX, posY, posZ, rotX, rotY, rotZ, numberplate, color1, color2, respawnPosX, respawnPosY, respawnPosZ, respawnRotX, respawnRotY, respawnRotZ, interior, dimension, respawnInterior, respawnDimension) VALUES (" .. table.concat( { model, x, y, z, 0, 0, rz, '"%s"', color1, color2, x, y, z, 0, 0, rz, getElementInterior( player ), getElementDimension( player ), getElementInterior( player ), getElementDimension( player ) }, ", " ) .. ")", getVehiclePlateText( vehicle ) ) Gracias de antemano. Link to comment
MTA Team 0xCiBeR Posted May 8, 2017 MTA Team Share Posted May 8, 2017 local result, num_affected_rows, last_insert_id = dbPoll ( qh, -1 ) Link to comment
aka Blue Posted May 8, 2017 Author Share Posted May 8, 2017 @.:CiBeR:. Muchas gracias Ciber, lo testearé mas tarde Link to comment
aka Blue Posted May 8, 2017 Author Share Posted May 8, 2017 Vale, ahora tengo un problema. Lo que quiero hacer es hacer una especie de ID para un vehículo y guardarlo en la base de datos SQLite. Tengo esto actualmente pero no sé como hacer que al crear el vehículo se almacene la ID correctamente. ¿Alguna ayuda? local a_veh = { } local vehDB = dbConnect( "sqlite", "databaseVehs.db" ) local veh = { } local vehID = { } addEventHandler( "onResourceStart", resourceRoot, function( ) local checkCreate = dbQuery ( vehDB , "CREATE TABLE IF NOT EXISTS vehicles (id TEXT,model TEXT,owner TEXT)" ) end ) addCommandHandler( "cveh", function( player, _, car ) local veh = getVehicleModelFromName( car ) if veh then local acc = getAccountName( getPlayerAccount( player ) ) if acc then local qh = dbQuery ( connection , "INSERT INTO vehicles (model,owner) VALUES (?,?)", car , acc ) local result, _, id = dbPoll ( qh, -1 ) if result then outputChatBox( "Vehículo insertado en la base de datos, id: "..tonumber( id ) ) else outputChatBox( "?", player, 255, 0, 0 ) end else outputChatBox( "?", player, 255, 0, 0 ) end end end ) Link to comment
MTA Team 0xCiBeR Posted May 9, 2017 MTA Team Share Posted May 9, 2017 (edited) local a_veh = { } local vehDB = dbConnect( "sqlite", "databaseVehs.db" ) local veh = { } local vehID = { } addEventHandler( "onResourceStart", resourceRoot, function( ) local checkCreate = dbQuery ( vehDB , "CREATE TABLE IF NOT EXISTS vehicles (id INTEGER PRIMARY KEY,model TEXT,owner TEXT)" ) dbFree(checkCreate) end ) addCommandHandler( "cveh", function( player, _, car ) local veh = getVehicleModelFromName( car ) if veh then local acc = getAccountName( getPlayerAccount( player ) ) if acc then local qh = dbQuery ( connection , "INSERT INTO vehicles (id,model,owner) VALUES (NULL,?,?)", car , acc ) local result, _, id = dbPoll ( qh, -1 ) if result then outputChatBox( "Vehículo insertado en la base de datos, id: "..tonumber( id ) ) else outputChatBox( "?", player, 255, 0, 0 ) end else outputChatBox( "?", player, 255, 0, 0 ) end end end ) Proba así y me contas. Recorda borrar tu tabla anterior llamada vehicles Edited May 9, 2017 by .:CiBeR:. Link to comment
aka Blue Posted May 9, 2017 Author Share Posted May 9, 2017 @.:CiBeR:. Sí, ha servido, gracias Ciber! Ahora mi duda es, para cargar los datos y crear los vehículos, hay que hacer ésto, no? addEventHandler( "onResourceStart", resourceRoot, function( ) checkCreate = dbQuery ( vehDB , "CREATE TABLE IF NOT EXISTS vehicles (id INTEGER PRIMARY KEY,model TEXT,owner TEXT,x TEXT,y, TEXT,z TEXT)" ) local result = dbPoll( checkCreate, -1 ) if result and #result > 0 then for i=1, #result do local id, model, owner, x, y, z = result[i][1], result[i][2], result[i][3], result[i][4], result[i][5], result[i][6] veh[id] = createVehicle( model, tonumber( x ), tonumber( y ), tonumber( z ) ) vehID[ veh[id] ] = id end end end ) Gracias por la ayuda! 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