【发布时间】: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