我有一个已经制作好的 .sqlite 文件,我可以使用 mozilla 插件并查看所有字段并编辑/添加等。我想将此文件读入 iphone 应用程序。如何读取文件并将每个条目保存到新的 NSObject 中?
当我搜索这个问题时,我在 stackoverflow 上所能找到的只是人们说要使用 mozilla 插件,但实际上并没有谈论目标 c 方面的事情。
在此先感谢,奥利
为此,您需要一个通过将数据库从项目资源复制到应用程序使用的实际目录来创建数据库的类。
因此,像这样创建一个名为 DataController 的 Objective C 类。在 DataController.h 中这样做
#import <Foundation/Foundation.h>
#import <sqlite3.h>
@interface DataController : NSObject
{
sqlite3 *databaseHandler ;
}
-(void)initDatabase;
-(NSArray*)getBooks;
-(NSString*)getChapter:(NSString*) bible:(NSString*) book:(NSString*) chapter;
@end
在它的实现中这样做。假设,您的数据库是 bible.sqlite 。它基本上做的是,它检查文档目录是否存在数据库,如果不存在,它将您已经创建的数据库从项目资源复制到实际目录。这是代码。
#import "DataController.h"
#import <sqlite3.h>
@implementation DataController
-(void)initDatabase
{
// Create a string containing the full path to the sqlite.db inside the documents folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"Bibles.sqlite"];
// Check to see if the database file already exists
bool databaseAlreadyExists = [[NSFileManager defaultManager] fileExistsAtPath:databasePath];
// Open the database and store the handle as a data member
if (sqlite3_open([databasePath UTF8String], &databaseHandler) == SQLITE_OK)
{
// Create the database if it doesn't yet exists in the file system
if (!databaseAlreadyExists)
{
NSLog(@"Database doesn't Exists");
BOOL success = NO ;
NSFileManager *filemngr = [NSFileManager defaultManager];
NSError *error;
NSString *defaultDbPath = [[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:@"Bibles.sqlite"];
NSLog(@"Size : %lld" , [[[NSFileManager defaultManager] attributesOfItemAtPath:defaultDbPath error:nil] fileSize]);
[filemngr removeItemAtPath:databasePath error:&error];
success = [filemngr copyItemAtPath:defaultDbPath toPath:databasePath error:&error];
if (!success){
NSLog(@"Error : %@" , [error localizedDescription]);
} else{
NSLog(@"Copy Successful");
NSLog(@"Size : %lld" , [[[NSFileManager defaultManager] attributesOfItemAtPath:databasePath error:nil] fileSize]);
}
} else{
NSLog(@"Database already Exists of Size : %lld" , [[[NSFileManager defaultManager] attributesOfItemAtPath:databasePath error:nil] fileSize]);
}
}
}
- (void)dealloc {
sqlite3_close(databaseHandler);
}
-(NSArray*)getBooks
{
NSMutableArray *Books = [[NSMutableArray alloc]init];
NSString *queryStatement = [NSString stringWithFormat:@"SELECT Name FROM Book ORDER BY ID"];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(databaseHandler, [queryStatement UTF8String], -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW) {
NSString *bookName = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 0)];
[Books addObject:bookName];
NSLog(@"Book : %@" , bookName);
}
sqlite3_finalize(statement);
} else{
NSLog(@"Error : %s",sqlite3_errmsg(databaseHandler));
}
return Books ;
}
-(NSString*)getChapter:(NSString*) bible:(NSString*) book:(NSString*) chapter{
NSString *verse = [[NSString alloc]init];
NSString *queryStatement = [NSString stringWithFormat:@"SELECT Text FROM %@ WHERE BookID = %@ AND Chapter = %@" , bible , book , chapter];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(databaseHandler, [queryStatement UTF8String], -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW) {
NSString *temp = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 0)];
verse = [[NSString alloc]initWithFormat:@"%@\n\n%@",verse ,temp];
// NSLog(@"Book : %@" , verse);
}
sqlite3_finalize(statement);
}
return verse ;
}
@end
好的...我为您创建了 2 种方法来展示如何使用它。
首先,在 Appdelegate.m 中,您必须通过这样做来初始化数据库
DataController *c = [[DataController alloc]init];
[c initDatabase];
现在,让我们使用它。这很简单。只需获取一个 DataController 实例并调用在数据库中运行 SQL 的方法,就像我编写的最后两个方法一样。希望它能解决你的问题。
I had the same issue a couple of months ago, I found the solution in youtube.
You have to copy your sqlite file to your bundle, import all the link libraries then write some small pieces of code mainly.
Here's a video, you don't need to watch it all, its kinda long.
https://www.youtube.com/watch?v=SVMorX_2Ymk
If you just need the codes let me know ;D