Jump to content

sql syntax mistakes


IIYAMA

Recommended Posts

  • Moderators

Hi,

I can't figure out why my system isn't saving and loading.

Can somebody check my code? Thank you!

  
-- loading -- 
local query = dbQuery(DB_connection, "SELECT * FROM `world` WHERE `area` = ?", gridX .. "/" .. gridY) 
if query then 
    outputDebugString("query") 
    local result, rows = dbPoll(query, -1) 
    if rows ~= 0 and type(result) == "table" and #result ~= 0 then 
        worldPartTable = fromJSON(result[1]) 
        outputDebugString("Loaded from DB") 
    end 
end 
------------ 

-- storing -- 
local query = dbQuery(DB_connection, "SELECT * FROM `world` WHERE `area` = ?", gridX .. "/" .. gridY) 
if query then 
    local result, rows = dbPoll(query, -1) 
    if result then 
        if rows == 0 then 
            dbExec(DB_connection, "INSERT INTO `world` (area) VALUES (?)", gridX .. "/" .. gridY) 
        end 
                 
        local resultOfUpdate = dbExec(DB_connection, "UPDATE `world` SET `data` = ? WHERE `area` = ?",toJSON(worldPartTable),gridX .. "/" .. gridY) 
         
         
        outputChatBox("resultOfUpdate: " .. tostring(resultOfUpdate)) 
    else 
        dbFree( query ) 
    end 
end 
----------- 

-- onResourceStart -- 
DB_connection = dbConnect ( "sqlite", "file.db" ) 
if DB_connection then 
    outputChatBox("connected to db") 
    dbExec(DB_connection, "CREATE TABLE IF NOT EXISTS `world` ( area TEXT )")     
else 
    local cancelReason = "MTA_map_generator: db failed to load." 
    outputDebugString(cancelReason) 
    cancelEvent(true,cancelReason) 
end 
--------------------- 

Link to comment

result table will looks like:

result = { 
    [1] = { 
        ["columnName"] = value, 
        ["columnName"] = value, 
        ["columnName"] = value 
    }, 
    [2] = { 
        ["columnName"] = value, 
        ["columnName"] = value, 
        ["columnName"] = value 
    }, 
    [3] = { 
        ["columnName"] = value, 
        ["columnName"] = value, 
        ["columnName"] = value 
    } 
} 

So defiantly result[1] won't be JSON string ( line 8 ).

Also you've used UPDATE for column 'data' but your sql table doesn't have column with such name.

Link to comment
  • Moderators

Well the structure I want is this:

result = { 
    [1] = { 
        ["columnName"] = value, 
        ["columnName"] = value, 
        ["columnName"] = value 
    } 
} 

I want 1 key and a value.

I don't want more rows.

But if that is incorrect, how should I put it?

"UPDATE `world` SET `data` = ? WHERE `area` = ?" 

Like this?

"UPDATE `world` SET `area` = ?" 

or this?

"UPDATE `world` SET `area` = ? WHERE `area` = ?" 

or must I change the inset into?

Link to comment
  • Moderators

And how will this structure be look like?

This?

    result = { 
        [1] = { 
            ["area"] = value, 
            ["data"] = value 
        } 
    } 

I ended up with:

local query = dbQuery(DB_connection, "SELECT * FROM `world` WHERE `area` = ?", gridX .. "/" .. gridY) 
if query then 
    local result, rows = dbPoll(query, -1) 
    if result then 
        if rows ~= 0 and type(result) == "table" and #result ~= 0 then 
            dbExec(DB_connection, "INSERT INTO `world` VALUES(area,value)",gridX .. "/" .. gridY,toJSON(worldPartTable)) 
        else 
           dbExec(DB_connection, "UPDATE `world` SET `value` = ? WHERE `area` = ?",toJSON(worldPartTable),gridX .. "/" .. gridY) 
        end 
    else 
        dbFree( query ) 
    end 
end 

And this warning:

WARNING: mta_map_generator\server_worldgeneration:425: dbExec failed; (1) no such column: value

This sql request: "UPDATE `world` SET `value` = ? WHERE `area` = ?"

Link to comment

But why are you updating a column which doesn't even exist? In your first post you are updating 2 columns ( area and data )

This???

  
dbExec(DB_connection, "UPDATE `world` SET `data` = ? WHERE `area` = ?", area, data) 

Anyway if you want to add a column named "value" then simply use alter.

dbExec(DB_connection, "ALTER TABLE world ADD value TEXT") 

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