Hi Jules,
I'm working with SQLite databases and i have functions that will work with the databases.
What is happening is that when my software starts, needs to call a function that will create the database and populate with all needed tables.
My function is this way:
bool CreateDefaultDB(String DBPath) {
char *sql;
int rc = 0;
sqlite3* pDb = NULL;
sqlite3_stmt* query = NULL;
File *fptr_db = new File(DBPath);
if(fptr_db->existsAsFile())
{
} else {
sqlite3_initialize( );
rc = sqlite3_open_v2( DBPath.toRawUTF8(), &pDb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL );
if ( rc != SQLITE_OK) {
sqlite3_close( pDb );
exit( -1 );
}
// ----------------------- TABLE USERS ---------------------------- //
sql = (char*)&"CREATE TABLE IF NOT EXISTS users ('id' integer PRIMARY KEY NOT NULL, 'groupId' integer NOT NULL, 'name' VARCHAR(40) NOT NULL COLLATE NOCASE, 'password' VARCHAR(200) COLLATE NOCASE, 'address' VARCHAR(200) COLLATE NOCASE, 'postalCode' VARCHAR(20), vat VARCHAR(30), 'bi' INT, 'LastSale' DATE, 'limits' DECIMAL(12,6), 'commission' DECIMAL(12,6), 'birthDate' DATE, 'phone' VARCHAR(20), 'mobilePhone' VARCHAR(20), 'email' VARCHAR(200) COLLATE NOCASE, 'obs' VARCHAR, 'defaultSerieId' integer NOT NULL ); ";
rc = sqlite3_prepare_v2( pDb, sql, -1, &query, NULL );
if ( rc != SQLITE_OK) exit( -1 );
rc = sqlite3_step( query );
if ( rc != SQLITE_DONE ) exit ( -1 );
(....)
sqlite3_finalize(query);
sqlite3_close(pDb);
sqlite3_shutdown();
}
return 0;
}
And after this function is called, depending on the option choosed, i need to populate the tables with demo data.
So I call second function, that is this way:
bool insertDemoData(String DBPath) {
char *sql;
int rc = 0;
sqlite3* pDb = NULL;
sqlite3_stmt* query = NULL;
File *fptr_db = new File(DBPath);
if (fptr_db->existsAsFile())
{
Logger::outputDebugString("DB not found");
}
else {
sqlite3_initialize();
rc = sqlite3_open_v2(DBPath.toRawUTF8(), &pDb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
if (rc != SQLITE_OK) {
Logger::outputDebugString("Error stage 1");
sqlite3_close(pDb);
exit(-1);
}
// ----------------------- TABLE USERS ---------------------------- //
sql = (char*)&"INSERT INTO [users] ([id], [groupId], [name], [password], [defaultSerieId]) VALUES (1, 0, 'Paulo', '1234', 0)";
rc = sqlite3_prepare_v2(pDb, sql, -1, &query, NULL);
if (rc != SQLITE_OK) exit(-1);
rc = sqlite3_step(query);
if (rc != SQLITE_DONE) exit(-1);
(....)
sqlite3_finalize(query);
sqlite3_close(pDb);
sqlite3_shutdown();
}
return 0;
}
What is happening is that when I call the first function, it's created a thread, so when i call second one, the database does not exists, since the thread is not finished.
I tried to make a simple and direct question, to avoid to put code here and make it more complex.
Paulo