2014-09-11 123 views
0

我想这应该是相当简单的,因为我是一个新的Xcode,Objective-C和SQLite,我只是想获得一个简单的教程工作。SQLite的“文件被加密或不是数据库”

我复制了 “sampled.sql” 文件的目录,这是连接代码:

-(NSMutableArray *) authorList { 
theauthors = [[NSMutableArray alloc] initWithCapacity:10]; 
@try { 
    NSFileManager *fileMgr = [NSFileManager defaultManager]; 
    NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"sampledb.sql"]; 
    BOOL success = [fileMgr fileExistsAtPath:dbPath]; 
    if(!success) 
    { 
     NSLog(@"Cannot locate database file '%@'.", dbPath); 
    } 
    if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK)) 
    { 
     NSLog(@"An error has occured: %s", sqlite3_errmsg(db)); 

    } 

    const char *sql = "SELECT * FROM verb"; 

    sqlite3_stmt *sqlStatement; 

    if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK) 
    { 
     NSLog(@"Problem with prepare statement 1: %s", sqlite3_errmsg(db)); 
    } else { 

     while (sqlite3_step(sqlStatement)==SQLITE_ROW) { 
      Author * author = [[Author alloc] init]; 
      author.verb_nutid = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)]; 
      //author.title = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,2)]; 
      [theauthors addObject:author]; 
     } 
    } 
    sqlite3_finalize(sqlStatement); 
} 
@catch (NSException *exception) { 
    NSLog(@"Problem with prepare statement 2: %s", sqlite3_errmsg(db)); 
} 
@finally { 
    sqlite3_close(db); 
    return theauthors; 
} 

}

数据库文件:

BEGIN TRANSACTION 

CREATE TABLE“动词“('ID'INTEGER PRIMARY KEY AUTOINCREMENT,'verba'TEXT,'verbb'TEXT); INSERT INTO“动词”价值......

等等......

但我得到的错误:

问题的准备语句1:文件加密或不是一个数据库

帮助将非常感谢! ( - :

+0

给我你的mail-id我发送的是u代码@ user4030788 – sanjeet 2014-09-11 11:58:27

+0

你不能在应用程序中写入文件,mainBundle resourcePath在应用程序中 – zaph 2014-09-11 12:02:38

+0

好的,谢谢你的回复!我会试试Erzékiels先生某种程度上的建议...( - : – user4030788 2014-09-11 20:46:31

回答

1

尝试写你的sampledb.sqldocuments directory代替bundle directory

// Getting the documents directory path 
NSString *docsDir; 
NSArray *dirPaths; 
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = [dirPaths objectAtIndex:0]; 

// Getting your db's path 
NSString *dbPath = [docsDir stringByAppendingPathComponent:@"sampledb.sql"]; 

有没有办法写入包目录,因为它与SSL证书签名的代码,但该文件目录不是。

+0

好的...好吧,我试过你的代码,现在我得到了这个...不能找到数据库文件'/用户/ anker /库/应用程序支持/ iPhone模拟器/ 7.1 /应用程序/ 5F97A08C-0F99-45C7-939C-00514CFC84FD /文档/ sampledb.sql”。 2014-09-11 22:32:17.647 TheDBproject [7691:60b]问题与准备语句1:没有这样的表:动词 – user4030788 2014-09-11 20:40:49

+0

哦对不起 - 我没有得到它没有...我只想读取数据库文件!我真的试图在互联网上进行研究,只要我不会改变它,将db文件放在捆绑包中应该没有问题。所以问题依然存在 - 为什么我不能用上面的代码读取db文件? – user4030788 2014-09-11 23:54:50

+0

上面的代码似乎是正确的,但为什么你绝对想要在数据包中编写数据库文件?文档目录正是在这里存储应用相关的文件。尝试'sampledb.db'而不是'sampledb.sql'。如果它根本不起作用,你是否设置了一些断点来检查变量值? – 2014-09-12 10:17:36