开发者

IPhone Development - Failed to use SQLite

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:2开发者_运维技巧07] 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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜