一、在Firefox中打开sqlite3(如果没有,选择工具->附加组件,添加即可)新建sqlite3数据库,Contacts,
建立一个members表,字段 id,integer,主键,自增;name,varchar;email,varchar,null;birthday,datetime,null。
向表中添加一些数据:
二、新建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:@"
[email protected]" andField3:@"birthday" field3Value:@"12-45-78"];
[self insertRecordIntoTableName:@"members" withField1:@"name" field1Value:@"李2" andField2:@"email" field2Value:@"
[email protected]" andField3:@"birthday" field3Value:@"12-45-78"];
[self insertRecordIntoTableName:@"members" withField1:@"name" field1Value:@"李3" andField2:@"email" field2Value:@"
[email protected]" andField3:@"birthday" field3Value:@"12-45-78"];
[self getAllContacts];
sqlite3_close(db);
[super viewDidLoad];
}
@end
插入数据后的效果:
查询的效果:
三、小结:
1.数据查询:sqlite3_exec()函数执行sql语句,在没有返回值的情况下(比如创建表格、插入记录、删除记录等操作中)很好用。
也会用到sqlite3_stat结构、sqlite3_prepare_v2()函数、sqlte3_step()函数和sqlite3_finalize()函数。
查询分三个阶段:准备阶段:sqlite3_stat、sqlite3_prepare_v2()
执行阶段:sqlte3_step()
终止阶段: sqlite3_finalize()
2.附表:
sqlite3数据库打开时的返回值及其所代表的含义
返回值
|
描述
|
返回值
|
描述
|
SQLITE_OK=0
|
返回成功
|
SQLITE_FULL=13
|
数据库满,插入失败
|
SQLITE_ERROR=1
|
Sql错误或错误的数据库
|
SQLITE_CANTOPEN=14
|
不能打开数据库文件
|
SQLITE_INTERNAL=2
|
Sqlite的内部逻辑错误
|
SQLITE_PROTOCOL=15
|
数据库锁定协议错误
|
SQLITE_PERM=3
|
拒绝访问
|
SQLITE_EMPTY=16
|
数据库表为空
|
SQLITE_ABORT=4
|
回调函数请求中断
|
SQLITE_SCHEMA=17
|
数据库模式改变
|
SQLITE_BUSY=5
|
数据库文件被锁
|
SQLITE_TOOBIG=18
|
一个表数据行过多
|
SQLITE_LOCKED=6
|
数据库中的一个表被锁
|
SQLITE_CONSTRAINT=19
|
由于约束冲突而中止
|
SQLITE_NOMEN=7
|
内存分配失败
|
SQLITE_MISMATCH=20
|
数据类型不匹配
|
SQLITE_READONLY=8
|
试图对一个只读数据库进行写操作
|
SQLITE_MISUSE=21
|
数据库错误使用
|
SQLITE_INTERRUPT=9
|
由sqlite_interrupt()结束操作
|
SQLITE_NOLFS=22
|
使用主机操作系统不支持的特性
|
SQLITE_IOERR=10
|
磁盘I/O发生错误
|
SQLITE_AUTH=23
|
非法授权
|
SQLITE_CORRUPT=11
|
数据库磁盘镜像畸形
|
SQLITE_FORMAT=24
|
辅助数据库格式错误
|
SQLITE_NOTFOUND=12
|
(Internal Only)表或记录不存在
|
SQLITE_NOTADB=26
|
打开的不是一个数据库文件
|