有很多事情要做:
- 将数据库放在应用程序中
- 导入库 sqlite3
- 创建两个文件作为 NSObject 的子类(调用数据库)
- 导入database.h
现在要在 database.h 中编写代码:
@interface 数据库:NSObject {
NSString *databaseName;
NSString *databasePath;
}
@property(nonatomic,retain)NSString *databaseName;
@property(nonatomic,retain)NSString *databasePath;
-(void)startDB;
-(void)checkAndCreateDatabase;
现在.m:
@implementation database : NSObject
@synthesize databasePath, databaseName;
-(void)startDB {
// Setup some globals
databaseName = @"yourdb.sqlite";
// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
// Execute the "checkAndCreateDatabase" function
[self checkAndCreateDatabase];
}
-(void)checkAndCreateDatabase{
// Check if the SQL database has already been saved to the users phone, if not then copy it over
BOOL success;
// Create a FileManager object, we will use this to check the status
// of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the database has already been created in the users filesystem
success = [fileManager fileExistsAtPath:databasePath];
// If the database already exists then return without doing anything
if(success) return;
// If not then proceed to copy the database from the application to the users filesystem
// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
用表的特定列填充数组:
在 ViewController.m 中导入 database.h 并编写以下代码:
-(void)viewWillAppear:(BOOL)animated {
[self queryNomi];
}
-(void)queryNomi {
[arrayName removeAllObjects];
[arrayEmail removeAllObjects];
database *dbDelegate = [[database alloc]init];
[dbDelegate startDB];
sqlite3 *database;
NSString *queryName = [NSString stringWithFormat:@"SELECT Name FROM nameTable"];
NSString *queryEmail = [NSString stringWithFormat:@"SELECT Email FROM nameTable"];
if(sqlite3_open([dbDelegate.databasePath UTF8String], &database) == SQLITE_OK) {
sqlite3_stmt *compiledStatement1;
sqlite3_stmt *compiledStatement2;
if(sqlite3_prepare_v2(database, [queryName UTF8String], -1, &compiledStatement1, NULL) == SQLITE_OK) {
while(sqlite3_step(compiledStatement1) == SQLITE_ROW) {
[arrayName addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement1, 0)]];
}
}
if(sqlite3_prepare_v2(database, [queryEmail UTF8String], -1, &compiledStatement2, NULL) == SQLITE_OK) {
while(sqlite3_step(compiledStatement2) == SQLITE_ROW) {
[arrayEmail addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement2, 0)]];
}
}
sqlite3_finalize(compiledStatement1);
sqlite3_finalize(compiledStatement2);
sqlite3_close(database);
}
// [self.tableView reloadData];
}
我似乎都写了..让我知道!