【问题标题】:SQLite "file is encrypted or is not a database"SQLite“文件已加密或不是数据库”
【发布时间】:2014-11-05 08:43:26
【问题描述】:

我想这应该相当简单,因为我是 Xcode、Objective-C 和 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 "verb" ('ID' INTEGER PRIMARY KEY AUTOINCREMENT, 'verba' TEXT, 'verbb' TEXT); 插入“动词”值……

等等……

但我得到了错误:

prepare 语句 1 的问题:文件已加密或不是数据库

我们将不胜感激! (-:

【问题讨论】:

  • 给我你的邮件ID,我正在发送你的代码@user4030788
  • 您不能写入应用程序中的文件,并且 mainBundle 资源路径在应用程序中。
  • 好的。感谢您的回复!我会试试先生。 Erzékiels 的建议以某种方式... (-:
  • 我只想读取数据库文件!我真的尝试在互联网上研究这个,将db文件放在bundle中应该没有问题,只要我不会改变它。
  • 文件打开了吗?代码存在问题,如果文件无法打开,它将继续读取数据库。您需要在存在或打开故障时提前返回,或者在故障发生后跳过其余代码。问题:文件存在吗?打开了吗?

标签: ios database sqlite encryption


【解决方案1】:

尝试将您的sampledb.sql 写入documents 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 证书签名的代码。但文档目录不是。

【讨论】:

  • 好的...好吧,我尝试了您的代码,现在我得到了这个...找不到数据库文件'/Users/anker/Library/Application Support/iPhone Simulator/7.1/Applications/5F97A08C- 0F99-45C7-939C-00514CFC84FD/Documents/sampledb.sql'。 2014-09-11 22:32:17.647 TheDBproject [7691:60b] 准备语句 1 的问题:没有这样的表:动词
  • 哦,对不起-我不明白...我只想读取数据库文件!我真的尝试在互联网上研究这个,将db文件放在bundle中应该没有问题,只要我不会改变它。所以问题依然存在——为什么我不能用上面的代码读取 db 文件?
  • 上面的代码看起来是对的,但是为什么你一定要在bundle中写数据库文件呢?文档目录正是在这里存储应用程序相关文件。尝试sampledb.db 而不是sampledb.sql。如果它根本不起作用,您是否放置了一些断点来检查变量值?
  • 好吧,老实说,我不在乎数据库文件在哪里,只要它可以工作(我只是在尝试按照教程教给我的操作 :-) ... 但是您的代码向我显示的路径我的电脑上似乎不存在(当我手动搜索时,没有路径“users/anker/library/application support/...”),所以我不能真正将 db 文件放在那里。
  • 我尝试放置一些断点,但没有任何更改,它突然停止并显示“无法找到 db 文件”??好吧,那太好了,不知何故……现在它只是说“没有这样的表:动词”。所以现在我粘贴了 sql 文件中的第一个代码,它显示了“动词”表,据我所知。可能是sql文件有问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-03
  • 1970-01-01
相关资源
最近更新 更多