Raised This Month: $51 Target: $400
 12% 

Some help with SQL_PrepareQuery and syntax?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
IceCucumber
Member
Join Date: Dec 2011
Old 11-20-2014 , 08:06   Some help with SQL_PrepareQuery and syntax?
Reply With Quote #1

I'm trying to insert a row into a table, and then increment an integer value ("count"), using prepared statements, but I'm running into some issues with my syntax.
I've removed the variables for readability sake, but this reproduces the same error.

PHP Code:
...

new 
String:sqlString[256];
Format(sqlStringsizeof(sqlString), "INSERT INTO test2 (steamid, name) VALUES (\"foo\", \"bar\"); UPDATE test2 SET count = Coalesce(count, 0) + 1 WHERE steamid = \"foo\";");

new 
Handle:sqlQuery SQL_PrepareQuery(sqlConnectionsqlStringsqlErrorsizeof(sqlError));

if (
sqlQuery == INVALID_HANDLE)
{
    
PrintToServer("SQL error: %s"sqlError);
    return 
Plugin_Stop;
}

... 
This will stop at the first error check:
Quote:
SQL error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE test2 SET count = Coalesce(count, 0) + 1 WHERE steamid = "foo"' at line 1
The first part (INSERT INTO [...];) works fine. I've also tried running this query in my local Apache setup, and it's working just fine:
Quote:
INSERT INTO test2 (steamid, name) VALUES ("foo", "bar"); UPDATE test2 SET count = Coalesce(count, 0) + 1 WHERE steamid = "foo";
Any idea?

Last edited by IceCucumber; 11-20-2014 at 09:47. Reason: typo
IceCucumber is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 11-20-2014 , 09:03   Re: Some help with SQL_PrepareQuery?
Reply With Quote #2

Does your table have a column named "count"? If so, reference it like this: `count` also reference "steamid" as `steamid` in your update command.
__________________
WildCard65 is offline
IceCucumber
Member
Join Date: Dec 2011
Old 11-20-2014 , 09:41   Re: Some help with SQL_PrepareQuery?
Reply With Quote #3

Ah yes, pardon the lack of clarity. The column is called "count" indeed. Maybe not the most descriptive name.

I tried
PHP Code:
Format(sqlStringsizeof(sqlString), "INSERT INTO test2 (steamid, name) VALUES (\"foo\", \"bar\"); UPDATE test2 SET `count` = Coalesce(`count`, 0) + 1 WHERE `steamid` = \"foo\";"); 
but still get
Quote:
SQL error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE test2 SET `count` = Coalesce(`count`, 0) + 1 WHERE `steamid` = "foo"' at line 1
The query
Quote:
INSERT INTO test2 (steamid, name) VALUES ("foo", "bar"); UPDATE test2 SET `count` = Coalesce(`count`, 0) + 1 WHERE `steamid` = "foo";
works on my local SQL server.

Last edited by IceCucumber; 11-20-2014 at 09:44.
IceCucumber is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 11-20-2014 , 12:35   Re: Some help with SQL_PrepareQuery and syntax?
Reply With Quote #4

SM doesn't let you run multiple queries in one go.
__________________
asherkin is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 11-20-2014 , 14:19   Re: Some help with SQL_PrepareQuery and syntax?
Reply With Quote #5

What above guy have said.

I didn't find for ALTER TABLE a quick check if row already exist in table, then ADD it.
So I query first count field, if result is invalid, then add row in table.

Then there is prepared statement

*This is however example, maybe have some faults.

Spoiler

Last edited by Bacardi; 11-20-2014 at 14:21.
Bacardi is offline
IceCucumber
Member
Join Date: Dec 2011
Old 11-20-2014 , 17:25   Re: Some help with SQL_PrepareQuery and syntax?
Reply With Quote #6

Ah, I see. Thanks!
IceCucumber is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 18:43.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode