I am trying to make a simple query on a database that I created, but it does not work. The database path looks very strange, I don't know if this is correct. I added the database to the Resource folder.
the code:
-(void)getInitialDataToDisplay:(NSString *)dbPath {
int result;
SQLiteTestAppDelegate *appDelegate = (SQLiteTestAppDelegate *)[[UIApplication sharedApplication] delegate];
result = sqlite3_open([dbPath UTF8String], &database) ;
if (result == SQLITE_OK) {
const char *query = "select first_name from tbl_student";
sqlite3_stmt *selectstmt;
result = sqlite3_prepare_v2(database, query, -1, &selectstmt, nil);
if(result == SQLITE_OK) {
NSLog(@"Query executed with success!!!!");
}
else {
NSLog(@"Error on execute query! Error = %i",result);
}
}
else {
sqlite3_close(database);
NSLog(@"Error on connect to database! Error = %i",result);
}
}
-(NSString *)getDBPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"FirstDataBase.sqlite"];
}
Conclusion:
**2010-08-20 08:55:15.810 SQLiteTest[263:207] Begin: connect to database
2010-08-20 08:55:15.843 SQLiteTest[263:207] Database copied with success! Location: /Users/claudio/Library/Application Support/iPhone Simulator/4.0/Applications/358B8748-7A2A-4FD4-943E-31B801279CA1/Documents/FirstDataBase.sqlite
2010-08-20 08:55:15.844 SQLiteTest[263:207] Error on execute query! Error = 1**
From "sqlite.h"
#define SQLITE_ERROR 1
Does this mean that my database is not copied correctly? What should I do?
Other notes:
1- I tried to use the "query" as const char* too;
2- I read in "sqlite3.h" that it sqlite3_open(...)will never return an error if the iPhone does not run out of memory.
3- I checked the name of my database many times, this is "FirstDataBase.sqlite".
Thanks in advance,
Claudio
source
share