Jump to content

db\db.lua:3: bad argument @ 'addeventhandler expected element at argument 2


amiz

Recommended Posts

  • Moderators

Hi!

getResourceRootElement is not exists (nil), use resourceRoot instead.
By the way, getResourceRootElement is an existing function in MTA, you just forgot the () from the end.

And I want to ask you to describe in more detail your question next time. Just pasting the error message here is not enough. Thanks.

Edited by Patrick
Link to comment

dbexec failed no such tables: vehicles 

but i have created db sqlite and i have put in the right db to read from i just dosent write 

how can i fix it ?

And I cant write changes in db sqlite , I cant click it its grey

 

Edited by amiz
Link to comment

I have done that

local db
 
addEventHandler('onResourceStart'resourceRootfunction()
    db = dbConnect('sqlite''global.db')
 
end)
 
function getdbconnection()
    return db 
end

Still dosent work

vehicles\vehicles .lua:9: dbExec Failed; (1) no such table: vehicles

Edited by amiz
Link to comment
vehicles\vehicles.lua:11: Bad argument @ 'createVehicle' [Expected number at argument 1, got nill]
dbExec failed; (1) near "exist": syntax error
 
outputChatBox("type")
 
function createVehicleForPlayer(playercommandmodel)
    local db = exports.db:getdbconnection()
    local x ,y ,z  = getElementPosition(player)
    y = 5
 
    dbExec (db"Create table if not exist vehicles id int, model int, x float, y float, z float")
    
    createVehicle(idmodelxyz)
end
 
addCommandHandler("createvehicle",  createVehicleForPlayerfalsefalse
addCommandHandler("createveh",  createVehicleForPlayerfalsefalse
addCommandHandler("makeveh",  createVehicleForPlayerfalsefalse
Link to comment

vehicles\vehicles.lua:9: dbExec failed; (1) near "exist": syntax error

outputChatBox("type")
 
function createVehicleForPlayer(playercommandmodel)
    local db = exports.db:getdbconnection()
    local x ,y ,z  = getElementPosition(player)
    y = 5
 
    dbExec (db,"Create table if not exist vehicles id int, model int, x float, y float, z float")
    
    createVehicle(modelxyz)
end
 
addCommandHandler("createvehicle",  createVehicleForPlayerfalsefalse
addCommandHandler("createveh",  createVehicleForPlayerfalsefalse
addCommandHandler("makeveh",  createVehicleForPlayerfalsefalse
Link to comment
11 hours ago, amiz said:

vehicles\vehicles.lua:9: dbExec failed; (1) near "exist": syntax error

outputChatBox("type")
 
function createVehicleForPlayer(playercommandmodel)
    local db = exports.db:getdbconnection()
    local x ,y ,z  = getElementPosition(player)
    y = 5
 
    dbExec (db,"Create table if not exist vehicles id int, model int, x float, y float, z float")
    
    createVehicle(modelxyz)
end
 
addCommandHandler("createvehicle",  createVehicleForPlayerfalsefalse
addCommandHandler("createveh",  createVehicleForPlayerfalsefalse
addCommandHandler("makeveh",  createVehicleForPlayerfalsefalse

I don't know what are you trying to do, but anyways try to replace the db string with this:

CREATE TABLE IF NOT EXISTS `vehicles` (id INT, model INT, x FLOAT, y FLOAT, z FLOAT)

 

Link to comment

vehicles\vehicles.lua:9: dbExec failed; (1) incomplete input 

am trying to make a database where my vehicles save, but it dosent work 

outputChatBox("type")
 
function createVehicleForPlayer(playercommandmodel)
    local db = exports.db:getdbconnection()
    local x ,y ,z  = getElementPosition(player)
    y = 5
 
    dbExec (db"CREATE TABLE IF NOT EXISTS `vehicles` (id INT, model INT, x FLOAT, y FLOAT, z FLOAT")
    
    createVehicle(modelxyz)
end
 
addCommandHandler("createvehicle",  createVehicleForPlayerfalsefalse
addCommandHandler("createveh",  createVehicleForPlayerfalsefalse
addCommandHandler("makeveh",  createVehicleForPlayerfalsefalse



 
Link to comment

Ok here is a simple example 

local db = dbConnect("sqlite", "database.db") --creating the connection (no need to create the 'database.db' file as the script does it for us);
local allVehiclesId = 0; --This is the start ID;

if db then -- make sure the db was created!
	dbExec(db, "CREATE TABLE IF NOT EXISTS `vehicles` (id INT, model INT, x FLOAT, y FLOAT, z FLOAT");
else
	return print("error db not existing"); --if not print error
end

function createVehicleForPlayer(player, command, model)
	local x, y, z = getElementPosition(player);
	local veh = createVehicle(model, x, y, z);
	if veh then
		allVehiclesId = allVehiclesId + 1; --Add the vehicle to the id list; (make sure here is not local variable)
		dbExec(db, "INSERT INTO `vehicles` (id, model, x, y, z) VALUES(?,?,?,?,?)", allVehiclesId, model, x, y, z); --save to database;
	end
end
addCommandHandler("createvehicle",  createVehicleForPlayer); --Create command;

--This part is a gift; :)

function loadSavedVehicles()
	if db then
		local p = dbPoll(dbQuery(db, "SELECT * FROM `vehicles`"), -1); --get all vehicles from db;
		if (#p > 0) then
			allVehiclesId = 0; --reset id's;
			for _,v in ipairs(p) do
				allVehiclesId = allVehiclesId + 1; -- re define id's
				createVehicle(v["model"], v["x"], v["y"], v["z"]);
			end
		end
	end
end
addCommandHandler("loadvehicles",  loadSavedVehicles);

Greetings.

Link to comment
4 hours ago, amiz said:

vehicles\vehicles.lua:6: dbExec failed; (1) incomplete input 

but I still have errors with the new script 

You're getting this error because you've missed a bracket on the end of the db string:

CREATE TABLE IF NOT EXISTS `vehicles` (id INT, model INT, x FLOAT, y FLOAT, z FLOAT)

 

  • Like 1
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...