Hi all,
I'm trying to do a simply query in a database that I built, but is not working. The Path of the database looks very strange, I don't know if it is correct. I added the database in the "Resource" folder.
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"];
}
Output:
**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 /* SQL error or missing database */
It means that my database is not being copied correctly? What should I do?
Other notes:
1- I tryed to use 'query' as const char* too
;
2- I read in "sqlite3.h" that sqlite3_open(...)
never will return an error, unless the iPhone run out of memory;
3- I checked the name of my database lots of time, it is exactly "FirstDataBase.sqlite".
Thanks in advance,
Claudio