Hi everyone,
Im having a problem with SQLite access. I write some entries to the DB and later I write some more so I first check if they are already in the DB to not get an error.
The problem is that if I write the entries, then exit the program (so close the database) and then start it up again and try to write the same entries, the "check" routine works perfectly and it doesnt write the same entries again.
But if I write them and then check if they are written inmediatly without closing the database. It doesnt find the entries just written!!
The code is very similar to this one although, the "check" part is another method. But I think this shows better my problem:
if (insert_statement == nil) {
static char *sql = "INSERT INTO todo (id,value) VALUES(?,?)";
if (sqlite3_prepare_v2(database, sql, -1, &insert_statement, NULL) != SQLITE_OK) {
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
}
sqlite3_bind_int(init_statement, 1, t.id);
sqlite3_bind_int(init_statement, 2, t.value);
int success = sqlite3_step(insert_statement);
sqlite3_reset(insert_statement);
if (success != SQLITE_ERROR) {
pk = sqlite3_last_insert_rowid(database);
} else {
NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
}
// Now check that the entry has been set to the database
if (check_statement == nil) {
const char *sql = "SELECT value FROM todo WHERE id=?";
if (sqlite3_prepare_v2(database, sql, -1, &check_statement, NULL) != SQLITE_OK) {
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
}
sqlite3_bind_int(check_statement, 1, t.id);
if (sqlite3_step(check_statement) == SQLITE_ROW) {
NSLog(@"Entry found in the database");
} else {
NSLog(@"Entry NOT found in the database");
}
sqlite3_reset(check_statement);
I always get the "Entry NOT found in the database" at least I dont close the database and open it up again.
Is there anyway to commit the insert so I can select the entries I have just inserted inmediatly?
Thanks in advance,
Picapau
Im having a problem with SQLite access. I write some entries to the DB and later I write some more so I first check if they are already in the DB to not get an error.
The problem is that if I write the entries, then exit the program (so close the database) and then start it up again and try to write the same entries, the "check" routine works perfectly and it doesnt write the same entries again.
But if I write them and then check if they are written inmediatly without closing the database. It doesnt find the entries just written!!
The code is very similar to this one although, the "check" part is another method. But I think this shows better my problem:
if (insert_statement == nil) {
static char *sql = "INSERT INTO todo (id,value) VALUES(?,?)";
if (sqlite3_prepare_v2(database, sql, -1, &insert_statement, NULL) != SQLITE_OK) {
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
}
sqlite3_bind_int(init_statement, 1, t.id);
sqlite3_bind_int(init_statement, 2, t.value);
int success = sqlite3_step(insert_statement);
sqlite3_reset(insert_statement);
if (success != SQLITE_ERROR) {
pk = sqlite3_last_insert_rowid(database);
} else {
NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
}
// Now check that the entry has been set to the database
if (check_statement == nil) {
const char *sql = "SELECT value FROM todo WHERE id=?";
if (sqlite3_prepare_v2(database, sql, -1, &check_statement, NULL) != SQLITE_OK) {
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
}
sqlite3_bind_int(check_statement, 1, t.id);
if (sqlite3_step(check_statement) == SQLITE_ROW) {
NSLog(@"Entry found in the database");
} else {
NSLog(@"Entry NOT found in the database");
}
sqlite3_reset(check_statement);
I always get the "Entry NOT found in the database" at least I dont close the database and open it up again.
Is there anyway to commit the insert so I can select the entries I have just inserted inmediatly?
Thanks in advance,
Picapau