12.04.2016, 03:48
Hello mates i really want help in this is there any ways to convert a ini script to sqllite ??
#pragma dynamic (20000)
#define MAX_STRING_SIZE (256)
#define MAX_INI_FIELDS (32)
stock SQL_Convert(const file[], DB:db, const table[])
{
new File:h = fopen(file, io_read);
if (! h)
return printf("[initosql.inc] - ERROR: Cannot open file '%s'.", file);
if (db == DB:0)
return printf("[initosql.inc] - ERROR: Cannot open database file(.db) 'handle: %i'.", _:db);
if (! table[0])
return print("[initosql.inc] - ERROR: No table name entered.");
new read[MAX_STRING_SIZE];
new pos;
new field[MAX_INI_FIELDS][32], value[MAX_INI_FIELDS][MAX_STRING_SIZE];
new count;
while (fread(h, read))
{
if (! read[0])
continue;
pos = strfind(read, "=");
strmid(field[count], read, 0, ((read[pos - 1] == ' ') ? (pos - 1) : (pos)));
strmid(value[count], read, ((read[pos + 1] == ' ') ? (pos + 1) : (pos)), strlen(read));
count++;
}
new query[MAX_INI_FIELDS * MAX_STRING_SIZE + 256];
format(query, sizeof (query), "INSERT INTO `%s` (", table);
for (new i; i < count; i++)
{
if (i == (count - 1))
format(query, sizeof (query), "%s`%s`) VALUES(", query, field[i]);
else
format(query, sizeof (query), "%s`%s`, ", query, field[i]);
}
for (new i; i < count; i++)
{
if (i == (count - 1))
format(query, sizeof (query), "%s'%q')", query, value[i]);
else
format(query, sizeof (query), "%s'%q', ", query, value[i]);
}
db_query(db, query);
fclose(h);
return printf("[initosql.inc] - SUCCESS: INI file '%s' was successfilly contverted to SQLite row in table '%s'.", file, table);
}
main()
{
new DB:db = db_open("Test.db");
db_query(db, "CREATE TABLE IF NOT EXISTS `Users` (`Name` TEXT PRIMARY KEY, `Score` INTEGER)");
/*
test.ini has the following lines
- Name = Gammix
- Score = 69
*/
SQL_Convert("test.ini", db, "users");
db_close(db);
}