tags:

views:

111

answers:

2

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

+1  A: 

You mean const char *, not const NSString * (besides, const NSString * is pretty much always wrong; you mean NSString * const if anything).

Also consider using NSLog(@"%s", sqlite3_errmsg(database)), which usually prints out a more helpful error message.

tc.
It's still not working. I got this error on NSLog(@"%s", sqlite3_errmsg(database):Error = unrecognized token: "`èd»"I don't know what it means. Could you help me?
Claudio
Replace `const NSString *query = @"select first_name from tbl_student";` with `const char * query = "select first_name from tbl_student";`. Llisten to compiler warnings.
tc.
I've already done it, and it's still the same error. I double checked the query too, it's working directly on the database but it's not working on IPhone code yet.
Claudio
A: 

Ok guys, now I'm sure I solved it. But it is not the way I'd like, because I think it will not work when I compile/copy the code for the device.

I copied the database directly to that directory that is shown in the output. I don't know why the code is not getting the path from my database.

Claudio