先为数据库建立一个数据库操作类
数据库操作类 只是为我们提供两个功能 打开数据库和关闭数据库
想要使用SQLite需要进行两步操作:
1:导入动态连接库, libsqlite3.0.dylib
2: 引⼊
#import "DataBaseHandle.h"
static sqlite3 *sqlite = nil;
@implementation DataBaseHandle
//对于应用程序来说 一个数据库 足以
//打开数据库
+(sqlite3 *)openDataBase{
//@synchronized
if (sqlite) {
return sqlite;
}
//获取documents文件路径
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
NSLog(@"%@",documentsPath);
//文件拼接
NSString *dbPath = [documentsPath stringByAppendingPathComponent:@"DB.sqlite"];
//打开
//通过路径查询到文件 如果文件存在 打开 如果不存在先进行创建 再进行打开,
int result = sqlite3_open([dbPath UTF8String], &sqlite);
if (result == SQLITE_OK) {
NSLog(@"打开数据库操作");
//创建表单
//创建SQL语句
NSString *sql = @"CREATE TABLE Student (stu_ID INTEGER PRIMARY KEY NOT NULL , name TEXT NOT NULL , sex TEXT NOT NULL DEFAULT 妖怪, age INTEGER NOT NULL DEFAULT 18,score FLOAT NOT NULL)";
//执行sql语句
sqlite3_exec(sqlite, [sql UTF8String], NULL, NULL, NULL);
}
return sqlite;
}
//关闭数据库
+(void)closeDataBase{
int result = sqlite3_close(sqlite);
if (result == SQLITE_OK) {
sqlite = nil;
NSLog(@"关闭成功");
}
}
@end
ViewController.m文件
#import "ViewController.h"
@interface ViewController ()
@property (retain, nonatomic) IBOutlet UITextField *numberTF;
@property (retain, nonatomic) IBOutlet UITextField *scoreTF;
@property (retain, nonatomic) IBOutlet UITextField *nameTF;
@property (retain, nonatomic) IBOutlet UITextField *sexTF;
@property (retain, nonatomic) IBOutlet UITextField *ageTF;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view endEditing:YES];
}
插入
- (IBAction)insertAction:(id)sender {
//1:打开数据库
sqlite3 *db = [DataBaseHandle openDataBase];
//2:创建指令集(伴随指针)
sqlite3_stmt *stmt = nil;
//3:创建sql语句
NSString *sql = @"INSERT INTO Student(name,sex,age,score)VALUES(?,?,?,?)";
//4:语法检查
//sqlite3 *db 数据库指针
//const char *zSql C语言SQL语句
//int nByte 给定SQL语句的长度,一般我们给-1 表示无穷大,不限制长度
//sqlite3_stmt **ppStmt 伴随指针的地址 存放SQL语句和数据库地址,
//const char **pzTail 预留参数
//语法检查的作用:不会执行sql语句.知识检查数据库是否正确打开,sql语句是否正确.如果正确,将数据库的信息和sql语句的信息放入伴随指针stmt中..
int flag = sqlite3_prepare_v2(db, [sql UTF8String], -, &stmt, nil);
//5:判断语法 检查是否正确.
if (flag == SQLITE_OK) {
//6 参数绑定
sqlite3_bind_text(stmt, , [_nameTF.text UTF8String], -, nil);
sqlite3_bind_text(stmt, , [_sexTF.text UTF8String], -, nil);
sqlite3_bind_int(stmt, , [_ageTF.text intValue]);
sqlite3_bind_double(stmt, , [_scoreTF.text doubleValue]);
//7:执行
sqlite3_step(stmt);
}
//8:释放所有权
sqlite3_finalize(stmt);
//9 关闭数据库
[DataBaseHandle closeDataBase];
}
查询
- (IBAction)selectAction:(id)sender {
//1 打开数据库
sqlite3 *sqlite = [DataBaseHandle openDataBase];
//创建指令集
sqlite3_stmt *stmt = nil;
//3创建sql语句
NSString *sql = @"SELECT *FROM Student WHERE stu_ID = ?";
//4语法检查
int flag = sqlite3_prepare_v2(sqlite, [sql UTF8String], -, &stmt, nil);
//5 判断语法检查是否正确
if (flag == SQLITE_OK) {
//6 参数绑定
sqlite3_bind_int(stmt, , [_numberTF.text intValue]);
//7 执行
//对于sqlite3_step() 在查询时候,可能会有多个符合条件的数据条 当查询完一条数据时候 sqlite3_step() 会往下看有没有下一条数据, 如果有返回SQLITE_ROW,如果没有返回SQLITE_DONE
//所以我们在进行查询时候 以sqlite3_step() 是否等于SQLITE_ROW作为循环获取数据的条件
while (sqlite3_step(stmt) == SQLITE_ROW) {
//获取数据
char *nameC = (char *)sqlite3_column_text(stmt, );
char *sexC= (char *)sqlite3_column_text(stmt, );
int ageC = sqlite3_column_int(stmt, );
double scoreC = sqlite3_column_double(stmt, );
//转化为OC中的类型
NSString *name = [NSString stringWithUTF8String:nameC];
NSString *sex = [NSString stringWithUTF8String:sexC];
NSString *age = [NSString stringWithFormat:@"%d",ageC];
NSString *score = [NSString stringWithFormat:@"%f",scoreC];
_nameTF.text = name;
_sexTF.text = sex;
_ageTF.text = age;
_scoreTF.text = score;
}
}
//8 释放所有权
sqlite3_finalize(stmt);
//9关闭数据库
[DataBaseHandle closeDataBase];
}
更改
- (IBAction)updateAction:(id)sender {
//1 打开数据库
sqlite3 *db = [DataBaseHandle openDataBase];
//创建指令集
sqlite3_stmt *stmt = nil;
//3创建sql语句
NSString *sql = @"UPDATE Student SET sex = ? WHERE name = ?";
//4语法检查
int flag = sqlite3_prepare_v2(db, [sql UTF8String], -, &stmt, nil);
//5 判断语法检查是否正确
if (flag == SQLITE_OK) {
//6 参数绑定
sqlite3_bind_text(stmt, , [_sexTF.text UTF8String], -, nil);
sqlite3_bind_text(stmt, , [_nameTF.text UTF8String], -, nil);
//7执行 绑定参数
sqlite3_step(stmt);
}
//8 释放所有权
sqlite3_finalize(stmt);
//9关闭数据库
[DataBaseHandle closeDataBase];
}
删除
- (IBAction)deleteAction:(id)sender {
//1 打开数据库
sqlite3 *db = [DataBaseHandle openDataBase];
//创建指令集
sqlite3_stmt *stmt = nil;
//3创建sql语句
NSString *sql = @"DELETE FROM Student WHERE name = ?";
//4语法检查
int flag = sqlite3_prepare_v2(db, [sql UTF8String], -, &stmt, nil);
//5 判断语法检查是否正确
if (flag == SQLITE_OK) {
//6 参数绑定
sqlite3_bind_text(stmt, , [_nameTF.text UTF8String], -, nil);
//7执行 绑定参数
sqlite3_step(stmt);
}
//8 释放所有权
sqlite3_finalize(stmt);
//9关闭数据库
[DataBaseHandle closeDataBase];
}
@end