简体   繁体   English

iPhone开发-无法使用SQLite

[英]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: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" 来自“ 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 ; 1-我也尝试使用'query'作为const char* too ;
2- I read in "sqlite3.h" that sqlite3_open(...) never will return an error, unless the iPhone run out of memory; 2-我读到“ sqlite3.h”中,除非iPhone内存不足,否则sqlite3_open(...)永远不会返回错误;
3- I checked the name of my database lots of time, it is exactly "FirstDataBase.sqlite". 3-我经常检查数据库的名称,它恰好是“ 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). 您的意思是const char * ,而不是const NSString * (此外, const NSString * 几乎总是错误的 ;您的意思是NSString * const如果有的话)。

Also consider using NSLog(@"%s", sqlite3_errmsg(database)) , which usually prints out a more helpful error message. 还可以考虑使用NSLog(@"%s", sqlite3_errmsg(database)) ,它通常会打印出更有用的错误消息。

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. 我不知道为什么代码没有从数据库中获取路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM