I am getting an Unknown Error SQLite error when attempting to query my app's database with a select statement. I have gone over the code about a dozen times but it looks fine to me (obviously there is something wrong somewhere though).
Here is my code where the issue happen:
- (Boolean)loginAttempt
{
// Do any additional setup after loading the view.
//Startup Checks on Database
//Get Docs Directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
docDir = [dirPaths objectAtIndex:0];
//Build path to DB file
dbPath = [[NSString alloc] initWithString: [docDir stringByAppendingPathComponent: @"AGDB.sqlite"]];
//NSC
fManager = [NSFileManager defaultManager];
if ([fManager fileExistsAtPath: dbPath] == NO)
{
[self callAlert:@"Database Not Found!" :@"For some reason we could not find your Angles and Gridz Database. Let us know about this error right away!" :@"Ok"];
}
else
{
dbpath = [dbPath UTF8String];
}
if (sqlite3_open(dbpath,&dbCon) == SQLITE_OK)
{
const char *insertSQL = "select Password from Login where Username=?";
if (sqlite3_prepare_v2(dbCon,insertSQL,-1,&stmt,NULL) == SQLITE_OK)
{
if (sqlite3_bind_text(stmt,1,[user_Text.text UTF8String],-1,NULL) != SQLITE_OK)
{
sqlErr = [NSString stringWithUTF8String:sqlite3_errmsg(dbCon)];
return FALSE;
}
if (sqlite3_step(stmt) == SQLITE_ROW)
{
int cols = sqlite3_column_count(stmt);
if (cols > 0)
{
for (int i1 = 0; i1 < cols; i1++)
{
switch(i1)
{
case 2:
rPass = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(stmt,i1)];
break;
default:
break;
}
}
}
sqlite3_clear_bindings(stmt);
sqlite3_finalize(stmt);
sqlite3_close(dbCon);
//Check against supplied password
if (![rPass isEqualToString:pass_Text.text])
{
return FALSE;
}
else
{
return TRUE;
}
}
else
{
sqlErr = [NSString stringWithUTF8String:sqlite3_errmsg(dbCon)];
return FALSE;
}
}
else
{
sqlErr = [NSString stringWithUTF8String:sqlite3_errmsg(dbCon)];
return FALSE;
}
}
else
{
sqlErr = [NSString stringWithUTF8String:sqlite3_errmsg(dbCon)];
return FALSE;
}
}
It all looks correct to me. This is the only place in this .m file where the database is opened and in the other places within the app where it is opened I am finalizing any statements and closing the database connection once the operations on the database are finished being carried out.
It is able to find the database and the table as I had a different error just before this one stating that the Username column did not exist (it was actually Usename, typoed it while I was writing the select statement).
If anyone spots anything wrong with the code or knows of what might be causing this please let me know! Thanks.
Aucun commentaire:
Enregistrer un commentaire