I’m dealing here with a new problem.
I started with the following code:
-(IBAction) Login:(id) sender{
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat: @"SELECT * FROM Contacts WHERE Login= (\"%@\") AND Password = (\"%@\")", Login.text, Password.text];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
//Store '1' when a user wants to stay logged
**if(logSwitch.on){
[self aStayLogged];
}else{
[self aReleaseLogged];
}**
Model *model = [Model sharedModel];
model.paramLogged = Login.text;
//[self openSecondView:nil];
} else {
Status.text = @"Login failed: Please check your ID and Password";
}
sqlite3_finalize(statement);
}
sqlite3_close(contactDB);
}
}
-(void) aStayLogged {
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *updateSQL = [NSString stringWithFormat: @"UPDATE CONTACTS SET LOGGED = '1' WHERE LOGIN = (\"%@\")", Login.text];
const char *update_stmt = [updateSQL UTF8String];
sqlite3_prepare_v2(contactDB, update_stmt, -1, &statement, NULL);
int success = sqlite3_step(statement);
if (success == SQLITE_ERROR) {
//NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(contactDB));
Status.text = @"Update failed";
}else{
**Status.text = @"Stay OK";**
}
//sqlite3_bind_text(statement, 1, [coffeeName UTF8String], -1, SQLITE_TRANSIENT);
//sqlite3_bind_int(statement, 2, coffeeID);
sqlite3_finalize(statement);
}
sqlite3_close(contactDB);
}
Here’s the deal: If I link the method ‘aStayLogged’ as an IBAction to a button, the update is done correctly. But when I embed this method into the ‘Login’ method, nothing happens!
I check this with the method:
-(IBAction) Check{
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat: @"SELECT LOGIN FROM CONTACTS WHERE LOGGED = '1'"];
const char *query_stmt = [querySQL UTF8String];
sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_ROW) {
NSString *loggedField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
Status.text = loggedField;
[loggedField release];
}else{
Status.text = @"Update failed";
}
sqlite3_finalize(statement);
}
sqlite3_close(contactDB);
}
The funny thing is that the status changes into “Stay OK” which is a confirmation that it should have worked! Does anyone have an idea what could be the cause of this? Sorry for the long post – I also happen to ask stupid questions, but this time I really don’t see it…
Hm, I think what you used wrong to check with SQLITE_ERROR, because there are many other status code which mate not complete, you better to check on SQLITE_OK or SQLITE_ROW, and if you got this status code, than you’re done well.
If you got an error, than use
sqlite3_errmsgto find out what went wrong. I think there is something with SQLITE_BUSY perhaps or LOCK