[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[], 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.
Reply
#2

Good job!
Reply
#3

That's nice.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)