参考:http://www.cnblogs.com/hanjun/archive/2012/10/29/2744573.html
总结4点:
1)open数据库
2)插入数据
3)查询数据
4)close数据库
一、新建Empty Appliation,添加一个HomeViewController,和一个组件libsqlite3.dylib,来支持对sqlite3的连接,关闭,增删改查等操作。
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)insertRecordIntoTableName:(NSString *)tableName
withField1:(NSString *)field1 field1Value:(NSString *)field1Value
andField2:(NSString *)field2 field2Value:(NSString *)field2Value
andField3:(NSString *)field3 field3Value:(NSString *)field3Value{
/*方法1:经典方法
NSString *sql = [NSString stringWithFormat:@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES('%@', '%@', '%@')", tableName, field1, field2, field3, field1Value, field2Value, field3Value];
char *err;
if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) {
sqlite3_close(db);
NSAssert(0, @"插入数据错误!");
}
*/
//方法2:变量的绑定方法
NSString *sql = [NSString stringWithFormat:@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES (?, ?, ?)",tableName, field1, field2, field3];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK) {
sqlite3_bind_text(statement, 1, [field1Value UTF8String], -1,NULL);
sqlite3_bind_text(statement, 2, [field2Value UTF8String], -1,NULL);
sqlite3_bind_text(statement, 3, [field3Value UTF8String], -1,NULL);
}
if (sqlite3_step(statement) != SQLITE_DONE) {
NSAssert(0, @"插入数据失败!");
sqlite3_finalize(statement);
}
}
//查询数据
- (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);
}
}
- (void)viewDidLoad
{
[self openDB];
[self insertRecordIntoTableName:@"members" withField1:@"name" field1Value:@"李1" andField2:@"email" field2Value:@"df@qq.com" andField3:@"birthday" field3Value:@"12-45-78"];
[self insertRecordIntoTableName:@"members" withField1:@"name" field1Value:@"李2" andField2:@"email" field2Value:@"df@qq.com" andField3:@"birthday" field3Value:@"12-45-78"];
[self insertRecordIntoTableName:@"members" withField1:@"name" field1Value:@"李3" andField2:@"email" field2Value:@"df@qq.com" andField3:@"birthday" field3Value:@"12-45-78"];
[self getAllContacts];
sqlite3_close(db);
[super viewDidLoad];
}
@end