AlliedModders

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

Mr.Waffle 01-09-2013 09:36

SQL column.
 
hello evreyone,

this code is open the table "psher" and 9 Columns.
PHP Code:

Queries SQL_PrepareQuery(SqlConnection,"CREATE TABLE IF NOT EXISTS psher( steamid varchar(32), name varchar(32), column1 SMALLINT(1), column2 SMALLINT(1), column3 SMALLINT(1), column4 SMALLINT(1), column5 SMALLINT(1), column6 SMALLINT(1),column7 SMALLINT(1), column8 SMALLINT(1), column9 SMALLINT(1))"

and i thought to do it like this:
PHP Code:

new const tablenames[] = 
{
    
"column1",
    
"column2",
    
"column3",
    
"column4",
    
"column5",
    
"column6",
    
"column7",
    
"column8",
    
"column9"    
}
        
    for(new 
09i++)
    {
        
Queries SQL_PrepareQuery(SqlConnection,"CREATE TABLE IF NOT EXISTS psher( steamid varchar(32), name varchar(32), %s SMALLINT(1))"tablenames[i])
    } 

but there is a problem i thinks because "CREATE TABLE IF NOT EXISTS" and table is already exists so its won't open another column..
someone has an idea how i can do this in my way( array ) , How to make it work?

thanks for helping

YamiKaitou 01-09-2013 10:20

Re: SQL column.
 
You have to list each column out as you have in the first code snippet, you cannot do what you are thinking.

Mr.Waffle 01-09-2013 10:25

Re: SQL column.
 
OK, maybe there is another way to do it ? cuz i need to open like 30 column , and its really bad to write them 1 by one 1.

YamiKaitou 01-09-2013 10:27

Re: SQL column.
 
Use a for loop to add the columns to a formatted string and then pass that formatted string to SQL_PrepareQuery.

Mr.Waffle 01-09-2013 11:21

Re: SQL column.
 
somting like this : ?
PHP Code:

new const tablenames[] = 
{
    
"column1",
    
"column2",
    
"column3",
    
"column4",
    
"column5",
    
"column6",
    
"column7",
    
"column8",
    
"column9"    
}



    new 
szText[2048]
    for(new 
09i++)
    {   
        
format(szTextcharsmaxszText ),"CREATE TABLE IF NOT EXISTS expmod( %s SMALLINT(1))"tablenames[i])
    }
    
Queries SQL_PrepareQuery(SqlConnection,"%s"szText


AngeIII 01-09-2013 11:39

Re: SQL column.
 
no.

you should to format inside loop only tables one by one.

%s smallint(1), %s smallint(1), ...
PHP Code:

new szText[2048];
new 
szMotdHeader[] = "CREATE TABLE IF NOT EXISTS expmod(";
new 
iLen=0;
for(new 
i=0;i<9;i++)
   
iLen += formatexszText[iLen], charsmax(szText)-iLen"%s smallint(1)%s"tablenames[i],(i==8)?"":",");
iLen+=formatex(szText[iLen], charsmax(szText)-iLen")");
Queries SQL_PrepareQuery(SqlConnection,"%s",szText); 



and also:

Queries = SQL_PrepareQuery(SqlConnection,"%s",szText);
Queries = SQL_PrepareQuery(SqlConnection, szText);

YamiKaitou 01-09-2013 11:43

Re: SQL column.
 
Has nobody checked the link I gave...

Mr.Waffle 01-09-2013 12:11

Re: SQL column.
 
Quote:

Originally Posted by YamiKaitou (Post 1870330)
Has nobody checked the link I gave...

i did but I understood you wrong, You can give an example please.

Quote:

Originally Posted by AngeIII (Post 1870327)
no.

you should to format inside loop only tables one by one.

%s smallint(1), %s smallint(1), ...
PHP Code:

new szText[2048];
new 
szMotdHeader[] = "CREATE TABLE IF NOT EXISTS expmod(";
new 
iLen=0;
for(new 
i=0;i<9;i++)
   
iLen += formatexszText[iLen], charsmax(szText)-iLen"%s smallint(1)%s"tablenames[i],(i==8)?"":",");
iLen+=formatex(szText[iLen], charsmax(szText)-iLen")");
Queries SQL_PrepareQuery(SqlConnection,"%s",szText); 



and also:

Queries = SQL_PrepareQuery(SqlConnection,"%s",szText);
Queries = SQL_PrepareQuery(SqlConnection, szText);

i think its will be bugged cuz if i change map this funcc will called and its wil open another column
or reset them.

YamiKaitou 01-09-2013 12:22

Re: SQL column.
 
Try this

PHP Code:

new const szColumns[][]= { ... };

new 
szQuery[2048], temp[64];
formatex(szQuerycharsmax(szQuery), "CREATE TABLE IF NOT EXISTS `expmod` (");

new 
iColumns sizeof(szColumns);
for (new 
0iColumns; )
{
    
formatex(tempcharsmax(temp), "`%s` SMALLINT(1)%s"szColumns[i], (++iColumns) ? ", " ")");
    
add(szQuerycharsmax(szQuery), temp);
}

hQuery SQL_PrepareQuery(SqlConnectionszQuery); 


Mr.Waffle 01-09-2013 12:36

Re: SQL column.
 
not working, there is eror in the logs :
in this line :
PHP Code:

formatex(tempcharsmax(temp), "`%s` SMALLINT(1)%s"szColumns[i], (++iColumns) ? ", " ")"); 

Quote:

L 01/09/2013 - 19:26:39: Start of error session.
L 01/09/2013 - 19:26:39: Info (map "de_dust2_long") (file "addons/amxmodx/logs/error_20130109.log")
L 01/09/2013 - 19:26:39: [AMXX] Displaying debug trace (plugin "Prej.amxx")
L 01/09/2013 - 19:26:39: [AMXX] Run time error 4: index out of bounds
L 01/09/2013 - 19:26:39: [AMXX] [0] Prej.sma::MySql_Init (line 268)


All times are GMT -4. The time now is 13:46.

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