1. HomeViewController.h代码:
#import <UIKit/UIKit.h>
#import "sqlite3.h"
@interface HomeViewController : UIViewController{
sqlite3 *db//声明一个sqlite3数据库
}
- (NSString *)filePath//数据库文件的路径。一般在沙箱的Documents里边 *** 作
@end
2. HomeViewController.m代码:
#import "HomeViewController.h"
@interface HomeViewController ()
@end
@implementation HomeViewController
//该方法用于返回数据库在Documents文件夹中的全路径信息
- (NSString *)filePath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
NSString *documentsDir = [paths objectAtIndex:0]
return [documentsDir stringByAppendingPathComponent:@"Contacts.sqlite"]
}
//打开数据库的方法
- (void)openDB{
if (sqlite3_open([[self filePath] UTF8String], &db) != SQLITE_OK) {
sqlite3_close(db)
NSAssert(0, @"数据库打开失败。")
}
}
- (void)getAllContacts{
NSString *sql = @"SELECT * FROM members"
sqlite3_stmt *statement
if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
char *name = (char *)sqlite3_column_text(statement, 0)
NSString *nameStr = [[NSString alloc] initWithUTF8String:name]
char *email = (char *)sqlite3_column_text(statement, 1)
NSString *emailStr = [[NSString alloc] initWithUTF8String:email]
char *birthday = (char *)sqlite3_column_text(statement, 2)
NSString *birthdayStr = [[NSString alloc] initWithUTF8String:birthday]
NSString *info = [[NSString alloc] initWithFormat:@"%@ - %@ - %@",
nameStr, emailStr, birthdayStr]
NSLog(info)
[nameStr release]
[emailStr release]
[birthdayStr release]
[info release]
}
sqlite3_finalize(statement)
}
}
实际上libsqlite3.0.dylib本身是个链接,它指向libsqlite3.dylib。也就是说在项目里如果你添加libsqlite3.dylib和添加libsqlite3.0.dylib其实是添加了同一个文件,没有区别,那为什么要添加libsqlite3.dylib呢?其实这个指针总是指向最新的sqlite3动态库,比如说sqlite3库更新了,如果我们引用的是libsqlite3.0.dylib你就不需要做任何修改了。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)