AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   SQL syntax (https://forums.alliedmods.net/showthread.php?t=57962)

krekers 07-15-2007 16:38

SQL syntax
 
Code:

dbi_query(dbc, "INSERT INTO table (blah) VALUES (variable)")
How can I put there working variable? I tried many ways:

Code:

dbi_query(dbc, "INSERT INTO table (blah) VALUES (variable)")
dbi_query(dbc, "INSERT INTO table (blah) VALUES (\"variable\")")
dbi_query(dbc, 'INSERT INTO table (blah) VALUES ("variable")')

It doesn't work. Please help me :)

Drak 07-15-2007 17:17

Re: SQL syntax
 
Format a string.
Code:
new String[256] format(String,255,"%i",myVariable); dbi_query(dbc,String);

Sylwester 07-15-2007 17:40

Re: SQL syntax
 
SixTwin that's not the answer and you don't have to use format.
PHP Code:

 dbi_query(dbc"INSERT INTO %s (%s) VALUES ('%d')"table_namecolumn_namevalue

krekers try :
PHP Code:

 dbi_query(dbc"INSERT INTO table (blah) VALUES ('variable')"

replace variable with formatting code

krekers 07-16-2007 07:19

Re: SQL syntax
 
Thank you very much :)

All ok, but when I compiled, there showed some warning:

PHP Code:

// plugin.sma(31) : warning 217: loose indentation 

the code:

PHP Code:

dbi_query(dbc"INSERT INTO table (blah) VALUES ('amx')"

The plugin works, but why there showed warning?

DotNetJunkie 07-16-2007 08:06

Re: SQL syntax
 
Quote:

Originally Posted by krekers (Post 503950)
Thank you very much :)

All ok, but when I compiled, there showed some warning:

PHP Code:

// plugin.sma(31) : warning 217: loose indentation 

the code:

PHP Code:

dbi_query(dbc"INSERT INTO table (blah) VALUES ('amx')"

The plugin works, but why there showed warning?

That warning does not effect the runtime of your plugin, its just a compiler
warning telling you that you have messy code usually.

One thing I'd like to point out is that if you want to protect your server from
SQL injection then use full quotions:

Code:

dbi_query(dbc, "INSERT INTO `table` ( `blah` ) VALUES ( ^"variable^" );");

krekers 07-16-2007 15:45

Re: SQL syntax
 
Please help me with SELECT. :oops:
I have read tutorial, but i didn't understand...
Can someone write some example ? The query:
Code:

dbi_query(dbc, "SELECT map FROM maps LIMIT 10")
I need the result print to chat. ( client_print() )


Thanks.

DotNetJunkie 07-16-2007 23:22

Re: SQL syntax
 
Quote:

Originally Posted by krekers (Post 504075)
Please help me with SELECT. :oops:
I have read tutorial, but i didn't understand...
Can someone write some example ? The query:
Code:

dbi_query(dbc, "SELECT map FROM maps LIMIT 10")
I need the result print to chat. ( client_print() )


Thanks.

Do you have to use DBI? SQLx is much better.

Code:

new Handle:mapQuery = SQL_PrepareQuery(g_Database, "SELECT `map` FROM `maps` LIMIT 10;");
if( !SQL_Execute(mapQuery) )
{
SQL_FreeHandle(mapQuery);
return PLUGIN_HANDLED;
}
new numMaps = SQL_NumResults(mapQuery);
new i = 0;
if( numMaps > 0 )
{
new printstr[512];
add(printstr, sizeof(printstr)-1, "We have the following maps: ");
while( SQL_MoreResults(mapQuery) )
{
new mapName[64];
SQL_ReadResult(mapQuery, 0, mapName, sizeof(mapName)-1);
if( i >= numMaps )
{
add(printstr, sizeof(printstr)-1, " and ");
}
else
{
add(printstr, sizeof(printstr)-1, ", ");
}
add(printstr, sizeof(printstr)-1, mapName);
i++;
SQL_NextRow(mapQuery);
}
client_print(id, print_chat, printstr);
}
else
{
client_print(id, print_chat, "We have no available maps.");
}
SQL_FreeHandle(mapQuery);
return PLUGIN_HANDLED;

Not tested so I don't know if it'll work without changes.
I haven't used DBI in awhile so I can't remember off the top of my head.

krekers 07-25-2007 16:10

Re: SQL syntax
 
Is there some simpler examle ? I have to learn basic and later i need to learn advanced scripting :]

I need just the query and print :)

Thank you anyway :)


All times are GMT -4. The time now is 21:35.

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