Jump to content

What's wrong with this?


Lloyd Logan

Recommended Posts

Why doesn't this INSERT a new value?

if data and type (data) == "table" and #data > 0 then 
        dbExec (server, "UPDATE accounts SET (x, y, z, cx, cy, cz, carid) VALUES (?, ?, ?, ?, ?, ?, ?)", tostring (a), tostring (b), tostring (c), tostring (x), tostring (y), tostring (z), tostring (vehname) "WHERE serial ='"..serial.."'") 
        else 
        dbQuery (server, "INSERT INTO accounts VALUES (x, y, z, cx, cy, cz, carid) VALUES (?, ?, ?, ?, ?, ?, ?)", tostring (a), tostring (b), tostring (c), tostring (x), tostring (y), tostring (z), tostring (vehname)) 
     

Link to comment

You should use dbExec for INSERT, also you have wrong syntax.

if data and type (data) == "table" and #data > 0 then 
        dbExec(server, "UPDATE accounts SET x = ?, y = ?, z = ?, cx = ?, cy = ?, cz = ?, carid = ? WHERE serial = '"..serial.."'", tostring (a), tostring (b), tostring (c), tostring (x), tostring (y), tostring (z), tostring (vehname)) 
        else 
        dbExec(server, "INSERT INTO accounts VALUES (?, ?, ?, ?, ?, ?, ?)", tostring (a), tostring (b), tostring (c), tostring (x), tostring (y), tostring (z), tostring (vehname)) 

Also you didn't insert any serial, you will have trouble because you have no serial column. So make sure to fix this one.

And check this website constantly:

http://www.w3schools.com/sql/sql_update.asp

http://www.w3schools.com/sql/sql_insert.asp

http://www.w3schools.com/sql/sql_where.asp

Edited by Guest
Link to comment

Thank you, also those websites you refer me to, I don't fully understand it;

In MTA It would equal to = dbExec(server, "INSERT INTO accounts blah)

Whereas

On that website it says = UPDATE table_name

SET column1=value1,column2=value2,...

WHERE some_column=some_value;

It's different, how do I apply the website syntax into the MTA one.

Link to comment
local data = dbPoll(dbQuery(server, "SELECT money FROM accounts WHERE serial = ?", serial), -1) 
        if data and type (data) == "table" and #data > 0 then 
        dbExec (server, "UPDATE accounts SET x = ?, y = ?, z = ?, cx = ?, cy = ?, cz = ?, carid = ? WHERE serial = '"..serial.."'", tostring (a), tostring (b), tostring (c), tostring (x), tostring (y), tostring (z), tostring (vehname),) 
        else 
        dbExec (server, "INSERT INTO accounts VALUES (x, y, z, cx, cy, cz, carid) VALUES (?, ?, ?, ?, ?, ?, ?)", tostring (a), tostring (b), tostring (c), tostring (x), tostring (y), tostring (z), tostring (vehname)) 
    end 

It now gives me the error of unexpected symbol near ) at line 15

Link to comment

It's the same, see the syntax for UPDATE:

http://www.w3schools.com/sql/sql_update.asp

UPDATE table_name SET column1=value1,column2=value2,... WHERE some_column=some_value; 

You will need to replace table_name with your table name, in your case the table is named accounts.

You will replace to column1 with the column you want to update and value1 with the new value you want for the column, if you have more than one column to update you can put comma and put the column2 and value2 and so on, no need to repeat SET again.

You do see in MTA we use ?, this is used to allow us to put the value after the comma as you can see here:

dbExec(server, "UPDATE accounts SET x = ?, y = ?, z = ?, cx = ?, cy = ?, cz = ?, carid = ? WHERE serial = '"..serial.."'", tostring (a), tostring (b), tostring (c), tostring (x), tostring (y), tostring (z), tostring (vehname)) 

But we still can do like in the web site:

dbExec(server, "UPDATE accounts SET x = '"..tostring(a)"', y = '"..tostring(b)"', z = '"..tostring(c)"', cx = '"..tostring(x)"', cy = '"..tostring(y)"', cz = '"..tostring(z)"', carid = '"..tostring(vehname)"' WHERE serial = '"..serial.."'") 

local data = dbPoll(dbQuery(server, "SELECT money FROM accounts WHERE serial = ?", serial), -1) 
        if data and type (data) == "table" and #data > 0 then 
        dbExec (server, "UPDATE accounts SET x = ?, y = ?, z = ?, cx = ?, cy = ?, cz = ?, carid = ? WHERE serial = '"..serial.."'", tostring (a), tostring (b), tostring (c), tostring (x), tostring (y), tostring (z), tostring (vehname),) 
        else 
        dbExec (server, "INSERT INTO accounts VALUES (x, y, z, cx, cy, cz, carid) VALUES (?, ?, ?, ?, ?, ?, ?)", tostring (a), tostring (b), tostring (c), tostring (x), tostring (y), tostring (z), tostring (vehname)) 
    end 

It now gives me the error of unexpected symbol near ) at line 15

Try this, and make sure to fix your serial column as i said before, you must have 8 columns at table accounts.

if data and type (data) == "table" and #data > 0 then 
        dbExec(server, "UPDATE accounts SET x = ?, y = ?, z = ?, cx = ?, cy = ?, cz = ?, carid = ? WHERE serial = '"..serial.."'", tostring (a), tostring (b), tostring (c), tostring (x), tostring (y), tostring (z), tostring (vehname)) 
        else 
        dbExec(server, "INSERT INTO accounts VALUES (?, ?, ?, ?, ?, ?, ?)", tostring (a), tostring (b), tostring (c), tostring (x), tostring (y), tostring (z), tostring (vehname)) 

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