sqlite的增删改查
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sqlite的增删改查相关的知识,希望对你有一定的参考价值。
SQLite是使用C
语言写的开源库,实现了一个自包含的SQL关系型数据库引擎
,可以使用SQLite
存储操作大量的数据,作为关系型数据库我们可以在一个数据库中建立多张相关联的表来解决大量数据重复的问题。而且SQLite
库也针对移动设备上的使用进行了优化。
因为
SQLite
的接口使用C
写的,而且Objective-C
是C
的超集所以可以直接在项目中使用SQLite
。写了一个小demo,git下载地址:https://github.com/yangchengzh/PachagingSqlite
下面是关键的部分代码
static sqlite3 *db;
- (sqlite3 *)openDB
{
- (sqlite3 *)openDB
{
// 说明已经打开数据库
if (db != nil) {
return db;
}
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *fileName = [doc stringByAppendingPathComponent:@"perple.sqlite"];
//将OC字符串转换为c语言的字符串
const char *cfileName = fileName.UTF8String;
//打开数据库文件(如果数据库文件不存在,该函数就会自动创建数据库文件)
int result = sqlite3_open(cfileName, &db);
if (result == SQLITE_OK) {
NSLog(@"数据库已打开");
} else {
NSLog(@"数据库打开失败");
}
return db;
}
- (void)closeDB
{
int result = sqlite3_close(db);
if (result == SQLITE_OK) {
NSLog(@"数据库已关闭");
db = nil;
} else {
NSLog(@"数据库关闭失败");
}
}
- (void)creatTable
{
db = [self openDB];
NSString *sql = @"create table IF NOT EXISTS perpleTable(number integer primary key not NULL, name text not NULL, gender text not NULL, age integer not NULL)";
int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"创建表成功");
} else {
NSLog(@"创建表失败");
}
[self closeDB];
}
- (void)insertWithModel:(PersonModel *)model
{
db = [self openDB];
NSString *sql = [NSString stringWithFormat:@"insert into perpleTable(number, name, gender, age) values(‘%ld‘, ‘%@‘, ‘%@‘, ‘%ld‘)", model.number, model.name, model.gender, model.age];
int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"添加成功");
} else {
NSLog(@"添加失败");
}
[self closeDB];
}
- (void)delateWithAge:(NSInteger)age
{
db = [self openDB];
NSString *sql = [NSString stringWithFormat:@"delete from perpleTable where age = ‘%ld‘", age];
int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"删除成功");
} else {
NSLog(@"删除表失败");
}
[self closeDB];
}
- (void)delateWithName:(NSString *)name
{
db = [self openDB];
NSString *sql = [NSString stringWithFormat:@"delete from perpleTable where name = ‘%@‘", name];
int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"删除成功");
} else {
NSLog(@"删除表失败");
}
[self closeDB];
}
- (void)updateWithName:(NSString *)name byAge:(NSInteger)age
{
db = [self openDB];
NSString *sql = [NSString stringWithFormat:@"update perpleTable set name = ‘%@‘ where age = ‘%ld‘", name, age];
int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"更新数据成功");
} else {
NSLog(@"更新数据失败");
}
[self closeDB];
}
- (void)selertAll
{
//1.打开数据库
db = [self openDB];
//2.写sql语句
NSString *sql = @"select * from perpleTable";
//3.创建跟随指针
sqlite3_stmt *stmt = nil;
//4.执行语句
int result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, NULL);
//5.判断语句是否正确
if (result == SQLITE_OK) {
NSLog(@"查询成功");
//6.执行查询
while (sqlite3_step(stmt) == SQLITE_ROW) {
//7.满足条件 读取数据
int number = sqlite3_column_int(stmt, 0);
const unsigned char *name = sqlite3_column_text(stmt, 1);
const unsigned char *gender = sqlite3_column_text(stmt, 2);
int age = sqlite3_column_int(stmt, 3);
NSLog(@"name = %s, number = %d, gender = %s, age = %d", name, number, gender, age);
}
} else {
NSLog(@"查询失败");
}
//8.释放指针
sqlite3_finalize(stmt);
//9.关闭数据库
[self closeDB];
}
- (void)selertWithAge:(NSInteger)age
{
db = [self openDB];
NSString *sql = [NSString stringWithFormat:@"select * from perpleTable where age = %ld", age];
sqlite3_stmt *stmt = nil;
int result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, NULL);
if (result == SQLITE_OK) {
NSLog(@"查询成功");
while (sqlite3_step(stmt) == SQLITE_ROW) {
const unsigned char *name = sqlite3_column_text(stmt, 1);
const unsigned char *gender = sqlite3_column_text(stmt, 2);
int number = sqlite3_column_int(stmt, 0);
int age = sqlite3_column_int(stmt, 3);
NSLog(@"name = %s, number = %d, gender = %s, age = %d", name, number, gender, age);
}
} else {
NSLog(@"查询失败");
}
sqlite3_finalize(stmt);
[self closeDB];
}
if (db != nil) {
return db;
}
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *fileName = [doc stringByAppendingPathComponent:@"perple.sqlite"];
//将OC字符串转换为c语言的字符串
const char *cfileName = fileName.UTF8String;
//打开数据库文件(如果数据库文件不存在,该函数就会自动创建数据库文件)
int result = sqlite3_open(cfileName, &db);
if (result == SQLITE_OK) {
NSLog(@"数据库已打开");
} else {
NSLog(@"数据库打开失败");
}
return db;
}
- (void)closeDB
{
int result = sqlite3_close(db);
if (result == SQLITE_OK) {
NSLog(@"数据库已关闭");
db = nil;
} else {
NSLog(@"数据库关闭失败");
}
}
- (void)creatTable
{
db = [self openDB];
NSString *sql = @"create table IF NOT EXISTS perpleTable(number integer primary key not NULL, name text not NULL, gender text not NULL, age integer not NULL)";
int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"创建表成功");
} else {
NSLog(@"创建表失败");
}
[self closeDB];
}
- (void)insertWithModel:(PersonModel *)model
{
db = [self openDB];
NSString *sql = [NSString stringWithFormat:@"insert into perpleTable(number, name, gender, age) values(‘%ld‘, ‘%@‘, ‘%@‘, ‘%ld‘)", model.number, model.name, model.gender, model.age];
int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"添加成功");
} else {
NSLog(@"添加失败");
}
[self closeDB];
}
- (void)delateWithAge:(NSInteger)age
{
db = [self openDB];
NSString *sql = [NSString stringWithFormat:@"delete from perpleTable where age = ‘%ld‘", age];
int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"删除成功");
} else {
NSLog(@"删除表失败");
}
[self closeDB];
}
- (void)delateWithName:(NSString *)name
{
db = [self openDB];
NSString *sql = [NSString stringWithFormat:@"delete from perpleTable where name = ‘%@‘", name];
int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"删除成功");
} else {
NSLog(@"删除表失败");
}
[self closeDB];
}
- (void)updateWithName:(NSString *)name byAge:(NSInteger)age
{
db = [self openDB];
NSString *sql = [NSString stringWithFormat:@"update perpleTable set name = ‘%@‘ where age = ‘%ld‘", name, age];
int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"更新数据成功");
} else {
NSLog(@"更新数据失败");
}
[self closeDB];
}
- (void)selertAll
{
//1.打开数据库
db = [self openDB];
//2.写sql语句
NSString *sql = @"select * from perpleTable";
//3.创建跟随指针
sqlite3_stmt *stmt = nil;
//4.执行语句
int result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, NULL);
//5.判断语句是否正确
if (result == SQLITE_OK) {
NSLog(@"查询成功");
//6.执行查询
while (sqlite3_step(stmt) == SQLITE_ROW) {
//7.满足条件 读取数据
int number = sqlite3_column_int(stmt, 0);
const unsigned char *name = sqlite3_column_text(stmt, 1);
const unsigned char *gender = sqlite3_column_text(stmt, 2);
int age = sqlite3_column_int(stmt, 3);
NSLog(@"name = %s, number = %d, gender = %s, age = %d", name, number, gender, age);
}
} else {
NSLog(@"查询失败");
}
//8.释放指针
sqlite3_finalize(stmt);
//9.关闭数据库
[self closeDB];
}
- (void)selertWithAge:(NSInteger)age
{
db = [self openDB];
NSString *sql = [NSString stringWithFormat:@"select * from perpleTable where age = %ld", age];
sqlite3_stmt *stmt = nil;
int result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, NULL);
if (result == SQLITE_OK) {
NSLog(@"查询成功");
while (sqlite3_step(stmt) == SQLITE_ROW) {
const unsigned char *name = sqlite3_column_text(stmt, 1);
const unsigned char *gender = sqlite3_column_text(stmt, 2);
int number = sqlite3_column_int(stmt, 0);
int age = sqlite3_column_int(stmt, 3);
NSLog(@"name = %s, number = %d, gender = %s, age = %d", name, number, gender, age);
}
} else {
NSLog(@"查询失败");
}
sqlite3_finalize(stmt);
[self closeDB];
}
以上是关于sqlite的增删改查的主要内容,如果未能解决你的问题,请参考以下文章