Yes, Matty.
I'm calling:
sqlite3_free(sql) and sqlite3_finalize(query);
I solved my problem right now, problem was that i was using the expression wrongly, i needed to put:
sql = sqlite3_mprintf("SELECT * from Users where id = '%s';", userid.toRawUTF8());
Now i finally made the complete function without any leaks.
My final function is:
UsersTBL LoginComponent::DBGetUserByID(String userid)
{
UsersTBL UserData;
int ret = 0;
String DBPath = getStoragePath();
char* sql = NULL;
sqlite3* pDb = NULL;
sqlite3_stmt* query = NULL;
UserData.ID = 0;
File fptr_db(DBPath);
if (fptr_db.existsAsFile())
{
sqlite3_initialize();
ret = sqlite3_open_v2(DBPath.toRawUTF8(), &pDb, SQLITE_OPEN_READONLY, NULL);
if (ret != SQLITE_OK)
{
sqlite3_close(pDb);
sqlite3_shutdown();
return UserData;
}
sql = sqlite3_mprintf("SELECT * from Users where id = '%s';", userid.toRawUTF8());
ret = sqlite3_prepare_v2(pDb, sql, -1, &query, NULL);
if (ret != SQLITE_OK)
{
sqlite3_free(sql);
sqlite3_finalize(query);
sqlite3_close(pDb);
sqlite3_shutdown();
return UserData;
}
if (SQLITE_ROW != (ret = sqlite3_step(query)))
{
UserData.ID = 0;
sqlite3_free(sql);
sqlite3_finalize(query);
sqlite3_close(pDb);
sqlite3_shutdown();
return UserData;
} else {
UserData.ID = (int)sqlite3_column_text(query, 0);
UserData.Name = (char*)sqlite3_column_text(query, 2);
UserData.Password = (char*)sqlite3_column_text(query, 3);
}
sqlite3_free(sql);
sqlite3_finalize(query);
sqlite3_close(pDb);
sqlite3_shutdown();
}
sql = nullptr;
pDb = nullptr;
query = nullptr;
return UserData;
}
Still have one issue with:
UserData.ID = (int)sqlite3_column_text(query, 0);
That in MacOSX says when compiling: "Cast from pointer to smaller type 'int' loses information".
If you have any advice it's great.
I share my function that perhaps can be improved but can also serve as reference to others.
Paulo