[FilterScript] INI to SQLite converter
#1

CONFIGURATION
PHP код:
#define DATABASE "user.db"
#define TABLE "users"
#define NEW_FIELD "name"
new const FILES[][] =
{
    
"user1.ini",
    
"user2.ini"
}; 
  • DATABASE - enter your database where you want to port all INI files
  • TABLE - enter the table name which will be verified for porting the record into
  • NEW_FIELD - enter the field name which will hold the unique identity of the file, basically file names are used as unique identity of INI files so this field will hold the file name without the .ini extension
  • FILES - this array holds all the file name which will be considered for converting (you can add as many you want)
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.
Reply
#2

Good job!
Reply
#3

That's nice.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)