SQLite“文件被加密或不是数据库” [英] SQLite "file is encrypted or is not a database"
问题描述
我想这应该是相当简单,因为我是一个新的Xcode,Objective-C和SQLite,我只是想得到一个简单的教程工作。
I guess this should be fairly simple, since I an new to Xcode, Objective-C and SQLite, and I am just trying to get a simple tutorial to work.
我复制了sampled.sql文件到目录,这是连接的代码:
I copied the "sampled.sql" file to the directory and this is the code that connects:
-(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;
}
}
DATABASE FILE:
DATABASE FILE:
BEGIN TRANSACTION
CREATE TABLEverb('ID'INTEGER PRIMARY KEY AUTOINCREMENT,'verba'TEXT,'verbb'TEXT);
INSERT INTO'verb'VALUES ...
CREATE TABLE "verb" ('ID' INTEGER PRIMARY KEY AUTOINCREMENT, 'verba' TEXT, 'verbb' TEXT); INSERT INTO 'verb' VALUES …
等等...
错误:
准备语句1的问题:文件已加密或不是数据库
Problem with prepare statement 1: file is encrypted or is not a database
非常感激! ( - :
Help would be much appreciated! (-:
推荐答案
尝试将 sampledb.sql
文件目录而不是
包目录
:
// 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"];
没有办法写入bundle目录,因为它是使用SSL证书签名的代码,但文档目录不是。
There's no way to write into the bundle directory because it's code signed with SSL certificate. But the documents directory's not.
这篇关于SQLite“文件被加密或不是数据库”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!