SA-MP Forums Archive
[FilterScript] INI to SQLite converter - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Filterscripts (https://sampforum.blast.hk/forumdisplay.php?fid=17)
+--- Thread: [FilterScript] INI to SQLite converter (/showthread.php?tid=622591)



INI to SQLite converter - Gammix - 24.11.2016

CONFIGURATION
PHP код:
#define DATABASE "user.db"
#define TABLE "users"
#define NEW_FIELD "name"
new const FILES[][] =
{
    
"user1.ini",
    
"user2.ini"
}; 
DOWNLOAD
PHP код:
#include <a_samp>
#pragma dynamic (10000)
#define DATABASE "user.db"
#define TABLE "users"
#define NEW_FIELD "name"
new const FILES[][] =
{
    
"user1.ini",
    
"user2.ini",
    
"user3.ini",
    
"user4.ini",
    
"user5.ini",
    
"user6.ini",
    
"user7.ini"
};
#define MAX_FIELDS (16)
#define MAX_FIELD_NAME (32)
#define MAX_FIELD_VALUE (256)
INI_To_SQLite(const filename[], DBdb, const table[])
{
    new 
Filehandle fopen(filenameio_read);
    if (!
handle)
        return 
0;
    if (!
db)
    {
        
fclose(handle);
        return 
0;
    }
    
    new 
string[MAX_FIELDS * (MAX_FIELD_VALUE MAX_FIELD_NAME)];
    
    
format(stringsizeof (string), "PRAGMA table_indo('%s')"table);
    new 
DBResultresult db_query(dbstring);
    new 
dbfieldcount;
    new 
dbfield[MAX_FIELDS][MAX_FIELD_NAME];
    if (!
result || db_num_rows(result) == 0)
    {
        
format(stringsizeof (string), "CREATE TABLE IF NOT EXISTS `%s` (`ID` INTEGER PRIMARY KEY, `%s` VARCHAR(64))"tableNEW_FIELD);
        if (!
db_query(dbstring))
        {
            
fclose(handle);
            return 
0;
        }
    }
    else
    {
        do
        {
            if (
dbfieldcount == MAX_FIELDS)
            {
                print(
"Please increase the value of \"MAX_FIELDS\" in order to load all fields from database. Some fields were missed.");
                break;
            }
            
            
db_get_field(result1dbfield[dbfieldcount], MAX_FIELD_NAME);
            
dbfieldcount++;
        }
        while (
db_next_row(result));
        
db_free_result(result);
    }
    new 
pos;
    new 
fieldcount;
    new 
field[MAX_FIELDS][MAX_FIELD_NAME];
    new 
value[MAX_FIELDS][MAX_FIELD_VALUE];
    new 
boolfieldexist;
    new 
numcount;
    new 
boolisfloat;
    
    while (
fread(handlestring))
    {
        
pos strfind(string"=");
        if (!
pos)
            continue;
        if (
fieldcount == MAX_FIELDS)
        {
            print(
"Please increase the value of \"MAX_FIELDS\" in order to load all fields from INI file. Some fields were missed.");
            break;
        }
            
        
strmid(field[fieldcount], string0pos);
        
strmid(value[fieldcount], stringpos 1strlen(string));
        
fieldexist false;
        for (new 
idbfieldcounti++)
        {
            if (!
strcmp(dbfield[i], field[fieldcount], true))
            {
                
fieldexist true;
                break;
            }
        }
        if (
fieldexist)
        {
            
fieldcount++;
            continue;
        }
        
        
numcount 0;
        
isfloat false;
        for (new 
istrlen(value[fieldcount]); ji++)
        {
            if (
'0' <= value[fieldcount][i] <= '9')
                
numcount++;
            else if (
value[fieldcount][i] == '.')
                
isfloat true;
            else
                
isfloat false;
        }
        
        if (
numcount && !isfloat)
               
format(stringsizeof (string), "ALTER TABLE `%s` ADD COLUMN `%s` INT NOT NULL DEFAULT '0'"tablefield[fieldcount]);
        else if (
numcount && isfloat)
            
format(stringsizeof (string), "ALTER TABLE `%s` ADD COLUMN `%s` FLOAT NOT NULL DEFAULT '0.0'"tablefield[fieldcount]);
        else
               
format(stringsizeof (string), "ALTER TABLE `%s` ADD COLUMN `%s` VARCHAR(%i) NOT NULL DEFAULT ''"tablefield[fieldcount], MAX_FIELD_VALUE);
        
db_query(dbstring);
        
fieldcount++;
    }
    if (
fieldcount <= 0)
    {
        
fclose(handle);
        return 
0;
    }
    
format(stringsizeof (string), "INSERT INTO `%s` (`%s`"tableNEW_FIELD);
    for (new 
ifieldcounti++)
        
format(stringsizeof (string), "%s, `%s`"stringfield[i]);
    new 
newfieldval[MAX_FIELD_NAME];
    
strmid(newfieldvalfilename0strlen(filename) - 4);
     
format(stringsizeof (string), "%s) VALUES ('%q'"stringnewfieldval);
    for (new 
ifieldcounti++)
        
format(stringsizeof (string), "%s, '%q'"stringvalue[i]);
    
strcat(string")");
    
    if (!
db_query(dbstring))
    {
        
fclose(handle);
        return 
0;
    }
    return 
1;
}
public 
OnFilterScriptInit()
{
    new 
DBdb;
    
db db_open(DATABASE);
    
db_query(db"PRAGMA synchronous = OFF");
    
    for (new 
isizeof (FILES); ji++)
    {
        if (!
INI_To_SQLite(FILES[i], dbTABLE))
            
printf("File \"%s\" unable to convert to SQLite database."FILES[i]);
        else
            
printf("File \"%s\" successfully converted to SQLite database."FILES[i]);
    }
    
    
db_close(db);
    
    return 
1;

You can also take out the function "INI_To_SQLite" itself and use it anywhere, it works great.


Re: INI to SQLite converter - BrianFaria - 24.11.2016

Good job!


Re: INI to SQLite converter - Jelly23 - 24.11.2016

That's nice.