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[], DB: db, const table[])
{
    new 
File: handle = fopen(filename, io_read);
    if (!
handle)
        return 
0;
    if (!
db)
    {
        
fclose(handle);
        return 
0;
    }
    
    new 
string[MAX_FIELDS * (MAX_FIELD_VALUE + MAX_FIELD_NAME)];
    
    
format(string, sizeof (string), "PRAGMA table_indo('%s')", table);
    new 
DBResult: result = db_query(db, string);
    new 
dbfieldcount;
    new 
dbfield[MAX_FIELDS][MAX_FIELD_NAME];
    if (!
result || db_num_rows(result) == 0)
    {
        
format(string, sizeof (string), "CREATE TABLE IF NOT EXISTS `%s` (`ID` INTEGER PRIMARY KEY, `%s` VARCHAR(64))", table, NEW_FIELD);
        if (!
db_query(db, string))
        {
            
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(result, 1, dbfield[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 
bool: fieldexist;
    new 
numcount;
    new 
bool: isfloat;
    
    while (
fread(handle, string))
    {
        
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], string, 0, pos);
        
strmid(value[fieldcount], string, pos + 1, strlen(string));
        
fieldexist = false;
        for (new 
i; i < dbfieldcount; i++)
        {
            if (!
strcmp(dbfield[i], field[fieldcount], true))
            {
                
fieldexist = true;
                break;
            }
        }
        if (
fieldexist)
        {
            
fieldcount++;
            continue;
        }
        
        
numcount = 0;
        
isfloat = false;
        for (new 
i, j = strlen(value[fieldcount]); i < j; i++)
        {
            if (
'0' <= value[fieldcount][i] <= '9')
                
numcount++;
            else if (
value[fieldcount][i] == '.')
                
isfloat = true;
            else
                
isfloat = false;
        }
        
        if (
numcount > 0 && !isfloat)
               
format(string, sizeof (string), "ALTER TABLE `%s` ADD COLUMN `%s` INT NOT NULL DEFAULT '0'", table, field[fieldcount]);
        else if (
numcount > 0 && isfloat)
            
format(string, sizeof (string), "ALTER TABLE `%s` ADD COLUMN `%s` FLOAT NOT NULL DEFAULT '0.0'", table, field[fieldcount]);
        else
               
format(string, sizeof (string), "ALTER TABLE `%s` ADD COLUMN `%s` VARCHAR(%i) NOT NULL DEFAULT ''", table, field[fieldcount], MAX_FIELD_VALUE);
        
db_query(db, string);
        
fieldcount++;
    }
    if (
fieldcount <= 0)
    {
        
fclose(handle);
        return 
0;
    }
    
format(string, sizeof (string), "INSERT INTO `%s` (`%s`", table, NEW_FIELD);
    for (new 
i; i < fieldcount; i++)
        
format(string, sizeof (string), "%s, `%s`", string, field[i]);
    new 
newfieldval[MAX_FIELD_NAME];
    
strmid(newfieldval, filename, 0, strlen(filename) - 4);
     
format(string, sizeof (string), "%s) VALUES ('%q'", string, newfieldval);
    for (new 
i; i < fieldcount; i++)
        
format(string, sizeof (string), "%s, '%q'", string, value[i]);
    
strcat(string, ")");
    
    if (!
db_query(db, string))
    {
        
fclose(handle);
        return 
0;
    }
    return 
1;
}
public 
OnFilterScriptInit()
{
    new 
DB: db;
    
db = db_open(DATABASE);
    
db_query(db, "PRAGMA synchronous = OFF");
    
    for (new 
i, j = sizeof (FILES); i < j; i++)
    {
        if (!
INI_To_SQLite(FILES[i], db, TABLE))
            
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.