iOS: Sqlite数据库的功能:建表,增加,删除,修改,查找
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了iOS: Sqlite数据库的功能:建表,增加,删除,修改,查找,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3438字,纯文字阅读大概需要5分钟。
内容图文
![iOS: Sqlite数据库的功能:建表,增加,删除,修改,查找](/upload/InfoBanner/zyjiaocheng/1149/b86dc5f1c9434aa9a03c44c683f25ea3.jpg)
本篇主要介绍Sqlite数据库的功能:建表,增加,删除,修改,查找。
采用封装的方法写的,继承于NSObject。
需向工程中添加libsqlite3.tbd库。
#import "DataBaseHandle.h"
//引入头文件
#import <sqlite3.h>
@interface DataBaseHandle()
//用来存放数据库的路径
@property (nonatomic,strong) NSString *filePath;
@end
@implementation DataBaseHandle
//数据库指针
static sqlite3 *DB = nil;
//懒加载
- (NSString *)filePath{
if (!_filePath){
//拼接文件路径文件只能以DB和sqlite结尾
_filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"Student.sqlite"];
}
return _filePath;
}
//单例
+(instancetype)sharedDataBase{
static DataBaseHandle *dataBas = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dataBas = [[DataBaseHandle alloc]init];
});
return dataBas;
}
//创建表格
- (void)createTable{
//准备sql语句
NSString *sqlString = @"create table if not exists Student (name text,age integer)";
//执行sql语句
int result = sqlite3_exec(DB, sqlString.UTF8String, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"建表成功");
}else{
NSLog (@"建表失败");
}
}
//打开数据库
- (void)openDataBase{
// 如果数据存在就打开,如果不存在就创建一个再打开
int result = sqlite3_open(self.filePath.UTF8String, &DB);
if (result == SQLITE_OK) {
NSLog(@"数据库打开成功");
}else{
NSLog(@"打开失败%d",result);
}
}
//关闭数据库
- (void) closeDataBase{
int result = sqlite3_close(DB);
if (result == SQLITE_OK) {
NSLog(@"关闭成功");
}else{
NSLog(@"关闭失败%d",result);
}
}
//增
- (void)insertStudentWithName:(NSString *)name age:(NSInteger)age{
//准备sql语句
NSString *sqlString = @"insert into Student (name,age) Values (?,?)";
//创建伴随指针(用来绑定参数,和获取数据)
sqlite3_stmt *stmt = NULL;
//预执行
int result = sqlite3_prepare(DB, sqlString.UTF8String, -1, &stmt, NULL);
if (result == SQLITE_OK) {
//参数绑定
sqlite3_bind_text(stmt, 1, name.UTF8String, -1, NULL);
sqlite3_bind_int64(stmt, 2, age);
//开始执行
if (sqlite3_step(stmt) == SQLITE_DONE) {
NSLog(@"插入成功");
}else{
NSLog(@"插入失败");
}
}else{
NSLog(@"语句错误");
}
//关闭伴随指针
sqlite3_finalize(stmt);
}
//改
- (void)updateWithAge:(NSInteger)age{
//准备sql语句
NSString *sqlString = @"update Student set age = 20 where age = ?";
//创建伴随指针(用来绑定参数,和获取数据)
sqlite3_stmt *stmt = NULL;
//预执行
int result = sqlite3_prepare(DB, sqlString.UTF8String, -1, &stmt, NULL);
if (result == SQLITE_OK) {
//参数绑定
sqlite3_bind_int64(stmt, 1, age);
//开始执行
if (sqlite3_step(stmt) == SQLITE_DONE) {
NSLog(@"更新成功");
}else{
NSLog(@"更新失败");
}
}else{
NSLog(@"语句错误");
}
//关闭伴随指针
sqlite3_finalize(stmt);
}
//查
- (void)selectWithName:(NSString *)name{
//准备sql语句
NSString *sqlString = @"select name,age from Student where name = ?";
//创建伴随指针
sqlite3_stmt *stmt = nil;
//预执行
int result = sqlite3_prepare(DB, sqlString.UTF8String, -1, &stmt, NULL);
if (result == SQLITE_OK) {
//参数绑定
sqlite3_bind_text(stmt, 1, name.UTF8String, -1, NULL);
//开始执行
while (sqlite3_step(stmt) == SQLITE_ROW){
//从伴随指针获取数据
NSString *readName = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 0)];
NSInteger readAge = sqlite3_column_int64(stmt , 1);
NSLog(@"%@,%ld",readName,readAge);
}
}else{
NSLog(@"语句错误");
}
//关闭伴随指针
sqlite3_finalize(stmt);
}
//删
- (void)deleteStudentWithAge:(NSInteger)age{
//准备sql语句
NSString *sqlString = [NSString stringWithFormat:@"delete from Student where age = %ld",age];
//执行
int result = sqlite3_exec(DB, sqlString.UTF8String, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"删除成功");
}else{
NSLog(@"删除失败%d",result);
}
}
@end
原文:http://www.cnblogs.com/zsl9855soley/p/5615133.html
内容总结
以上是互联网集市为您收集整理的iOS: Sqlite数据库的功能:建表,增加,删除,修改,查找全部内容,希望文章能够帮你解决iOS: Sqlite数据库的功能:建表,增加,删除,修改,查找所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。