Karuzo

database delete statement

Recommended Posts

Hey Guys,

So i have a problem with my car shop.

I have a command with which you can sell the car. So i delete the row in the database, but after you sold the car you can't buy a new one somehow.

This is my code:

Buy Function:

addEvent("CarBuy",true) 
function CarBuyFunc(lp,veh,price) 
    local db = dbConnect("sqlite","susa_data/accounts.db") 
    local sx,sy,sz = -1935.45764, 269.70041, 44.49506 
    local query = dbQuery(db,"SELECT * FROM cars WHERE OWNER=?",username) 
--... 
    if money > tonumber(price) then 
        local result,num_rows = dbPoll(query,-1) 
        if num_rows == 0 then 
            setPlayerMoney(lp,money-price) 
            local exec = dbQuery(db,"INSERT INTO cars(CARID,SPAWNX,SPAWNY,SPAWNZ,ROTX,ROTY,ROTZ,OWNER,CARKEY,R,G,B,PRICE) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)",carid,sx,sy,sz,0,0,50,username,carkey,r,g,b,price) 
            if exec then 
                outputDebugString("Inserting Car Shop successfull!") 
                dbFree(exec) 
            else 
                outputDebugString("Inserting Car Shop wasn't successfull!") 
            end 
            local spawnedVehicle = createVehicle(carid, sx, sy, sz, 0, 0, 50) 
            setElementData(spawnedVehicle, "Owner", username) 
            warpPedIntoVehicle(lp,spawnedVehicle) 
        else 
            outputChatBox("You already have a car!",lp,180,0,0,false) 
        end 
    else 
        outputChatBox("You don't have enough money to buy this Car!",lp,180,0,0,false) 
    end 
end 
addEventHandler("CarBuy",root,CarBuyFunc) 

My sell function:

ddCommandHandler("sellmycar", 
    function(player,cmd) 
    local price 
    local pName = getElementData(player,"username") 
    local sql = dbQuery(db,"SELECT * FROM cars WHERE OWNER=?",pName) 
    local result, num_rows = dbPoll(sql, -1) 
    local money = getPlayerMoney(player) 
    if num_rows >= 1 then 
        for _, row in pairs (result) do 
            price = row["PRICE"] 
        end 
        local sellprice = price * 0.40 
        dbExec(db,"DELETE FROM cars WHERE OWNER=?",pName ) 
        outputChatBox("You just sold your car for 40% of the original price: "..sellprice,player,0,180,0) 
        setPlayerMoney(player,money+sellprice,false) 
        for _, veh in pairs ( getElementsByType("vehicle") ) do  
            if(tostring(getElementData(veh, "Owner")) == tostring(pName)) then 
                destroyElement(veh) 
            end 
        end 
    end 
end) 

Share this post


Link to post
What does the result returns?

What does num_rows return?

Is num_rows really 0?

Which result do you mean?

num_rows outputs 1 for some reason.

Share this post


Link to post
local result,num_rows = dbPoll(query,-1)

What does result return? Output it :P

After i sold my car it does not output anything. But if i already have a car it outputs all the datas such as color,model etc..

Share this post


Link to post
Check if result is empty instead of this:

if num_rows >= 1 then

I solved the problem by switching to mysql instead of sqlite. Sqlite did not refresh the database when i tried to buy the car.

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.