Jump to content

[HELP] SQL problem


PauloCascus

Recommended Posts

Hello guys,

I have a problem. When I create a house, it is entered into the database is not correct, the icon appears with a standard information that I entered when creating, I can open different windows, but when I restart the script, the house is lost, just when I try to buy it, debugstring: attempt to concatenate local 'value' (a nil value)

local 'value' is the same like house ID

then when i've closed house window and opened it again debugstring: attempt to concatenate local 'owner' ( a nil value )

local owner in the client side, it's field with owner's name into the house window...

Function with local 'value'

function setHouseData(ID, typ, value) 
    -- Security array --  
    houseData[ID][typ] = value 
    setElementData(house[ID], typ, value) 
    if(saveableValues[typ]) then 
        local query = dbQuery(housingdab, "UPDATE houses SET "..saveableValues[typ].." = '"..value.."' WHERE ID = '"..ID.."';" ) 
        local result = dbPoll(query, -1) 
        if(result) then 
            dbFree(query) 
        else 
            error("Невозможно сохранить информацию "..typ.." со значением: "..value.." для дома с номером: "..ID..": Ошибка HD00") 
        end 
    end 
end 

Database:

local housingdab = dbConnect("sqlite","housing.db") 
  
function housingdb() 
if (housingdab) then  
dbExec(housingdab,"CREATE TABLE IF NOT EXISTS 'houses' ('ID' INT, 'X' FLOAT, 'Y' FLOAT, 'Z' FLOAT, 'INTERIOR' INT, 'INTX' FLOAT, 'INTY' FLOAT, 'INTZ' FLOAT, 'MONEY' INT, 'WEAP1' TEXT, 'WEAP2' TEXT, 'WEAP3' TEXT, 'LOCKED' INT, 'PRICE' INT, 'OWNER' TEXT, 'RENTABLE' INT, 'RENTALPRICE' INT, 'RENT1' INT, 'RENT2' INT, 'RENT3' INT, 'RENT4' INT, 'RENT5' INT)") 
outputDebugString("Вы успешно подключились к базе данных SQLite", thePlayer, 0, 200, 0 ) 
housesys_startup() 
else 
outputDebugString("Подключение сорвалось, пожалуйста проверьте базу данных", thePlayer, 200,0,0) 
end  
end 
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), housingdb) 

CreateHouse function:

  
addEventHandler("onHouseSystemHouseCreate", getRootElement(), function( x, y, z, int, intx, inty, intz, price) 
    local query = dbQuery(housingdab, "SELECT * FROM 'houses'") 
    local result, numrows = dbPoll(query, -1) 
    if(result) then 
        local newid = houseid+1 
        outputChatBox("Дом под номером: "..newid.." успешно создан", source, 0, 255, 0) 
        local weapontable = {} 
        weapontable[1] = 0 
        weapontable[2] = 0 
        weapontable[3] = 0 
        dbExec(housingdab, "INSERT INTO 'houses' ( ID, X, Y, Z, INTERIOR, INTX, INTY, INTZ, PRICE) VALUES ('"..newid.."','"..x.."', '"..y.."', '"..z.."', '"..int.."', '"..intx.."', '"..inty.."', '"..intz.."', '"..price.."');") 
        buildHouse(newid, x, y, z, int, intx, inty, intz, 0, weapontable, 0, price, "Недвижимость Штата", 0, 0, "Не арендовано", "Не арендовано", "Не арендовано", "Не арендовано", "Не арендовано") 
    else 
        outputChatBox("Сбой при создании дома: Ошибка #CH00", source, 255, 0, 0) 
        error("Дом номер: "..(houseid+1).." не может быть создан: Ошибка #CH01") 
    end 
end) 

Buy function:

addCommandHandler("buyhouse", function(thePlayer) 
    if(getElementData(thePlayer, "house:lastvisit")) and (getElementData(thePlayer, "house:lastvisit") ~= false)  then 
        local house = getElementData(thePlayer, "house:lastvisit") 
        if(house) then 
            local id = house 
            local owner = houseData[id]["OWNER"] 
            if(owner ~= "Недвижимость Штата") then 
                outputChatBox("Вы не можете купить этот дом", thePlayer, 255, 0, 0) 
            else 
                local houses = 0 
                for index, col in pairs(getElementsByType("colshape")) do 
                    local baga = getPlayerAccount(thePlayer) 
                    local bagas = getAccountName(baga) 
                    if(getElementData(col, "house") == true) and (houseData[getElementData(col, "ID")]["OWNER"] == bagas) then 
                        houses = houses+1 
                        if(houses == max_player_houses) then 
                            outputChatBox("У вас уже "..max_player_houses.." дома! Продайте один из них, чтобы приобрести новый", thePlayer, 255, 0, 0) 
                            return 
                        end 
                    end 
                end 
                local money = getPlayerMoney(thePlayer) 
                local price = houseData[id]["PRICE"] 
                if(money < price) then outputChatBox("У вас не достаточно денег, нужно еще "..(price-money).."$", thePlayer, 255, 0, 0) return end 
                setHouseData(id, "OWNER", bagas) 
                givePlayerMoney(thePlayer, -price) 
                setAccountData(thePlayer,"owner", id) 
                outputChatBox("Поздравляем! Вы приобрели себе новый дом", thePlayer, 0, 255, 0) 
                setElementModel(houseData[id]["PICKUP"], 1272) 
                setElementModel(houseData[id]["BLIP"], 32) 
            end 
        end 
    end 
end) 

Function to get houses from db after restart:

function housesys_startup() 
    if(created == true) then 
        error("Дома уже созданы. Ошибка BD00") 
        return 
    end 
    buildStartTick = getTickCount() 
    local query = dbQuery(housingdab, "SELECT * FROM 'houses' " ) 
    local result, numrows = dbPoll(query, -1) 
    if (result and numrows > 0) then 
        for index, row in pairs(result) do 
            local id = row['ID'] 
            local x, y, z = row['X'], row['Y'], row['Z'] 
            local int, intx, inty, intz = row['INTERIOR'], row['INTX'], row['INTY'], row['INTZ'] 
            local money, weap1, weap2, weap3 = row['MONEY'], row['WEAP1'], row['WEAP2'], row['WEAP3'] 
            local locked = row['LOCKED'] 
            local price = row['PRICE'] 
            local owner = row['OWNER'] 
            local rentable = row['RENTABLE'] 
            local rentalprice = row['RENTALPRICE'] 
            local rent1, rent2, rent3, rent4, rent5 = row['RENT1'],row['RENT2'], row['RENT3'], row['RENT4'], row['RENT5'] 
            local weapontable = {} 
            weapontable[1] = weap1 
            weapontable[2] = weap2 
            weapontable[3] = weap3 
            buildHouse(id, x, y, z, int, intx, inty, intz, money, weapontable, locked, price, owner, rentable, rentalprice, rent1, rent2, rent3, rent4, rent5) 
        end 
        dbFree(query) 
    else 
        error("База данных не найдена или пустая. Ошибка BD01") 
    end 
    created = true 
    setTimer(function() 
        local elapsed = (buildEndTick-buildStartTick) 
        outputServerLog("Это займет "..(elapsed/1000).." секунд для постройки всех домов") 
    end, 1000, 1) 
    rentTimer = setTimer(takePlayerRent, 60*60*1000, -1) 
end 

Link to comment

Buy function

  
addCommandHandler("buyhouse", function(thePlayer) 
    if isGuestAccount(getPlayerAccount(thePlayer)) then 
         return outputChatBox('Guests can\'t buy houses!', thePlayer) 
    end 
    if(getElementData(thePlayer, "house:lastvisit")) and (getElementData(thePlayer, "house:lastvisit") ~= false)  then 
        local house = getElementData(thePlayer, "house:lastvisit") 
        if(house) then 
            local id = house 
            local owner = houseData[id]["OWNER"] 
            if(owner ~= "Недвижимость Штата") then 
                outputChatBox("Вы не можете купить этот дом", thePlayer, 255, 0, 0) 
            else 
                local houses = 0 
                local baga = getPlayerAccount(thePlayer) 
                local bagas = getAccountName(baga) 
                for index, col in pairs(getElementsByType("colshape")) do 
  
                    if(getElementData(col, "house") == true) and (houseData[getElementData(col, "ID")]["OWNER"] == bagas) then 
                        houses = houses+1 
                        if(houses == max_player_houses) then 
                            outputChatBox("У вас уже "..max_player_houses.." дома! Продайте один из них, чтобы приобрести новый", thePlayer, 255, 0, 0) 
                            return 
                        end 
                    end 
                end 
                local money = getPlayerMoney(thePlayer) 
                local price = houseData[id]["PRICE"] 
                if(money < price) then outputChatBox("У вас не достаточно денег, нужно еще "..(price-money).."$", thePlayer, 255, 0, 0) return end 
                setHouseData(id, "OWNER", bagas) 
                givePlayerMoney(thePlayer, -price) 
                setAccountData(thePlayer,"owner", id) 
                outputChatBox("Поздравляем! Вы приобрели себе новый дом", thePlayer, 0, 255, 0) 
                setElementModel(houseData[id]["PICKUP"], 1272) 
                setElementModel(houseData[id]["BLIP"], 32) 
            end 
        end 
    end 
end) 
  
  

Edited by Guest
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...