iOS sqlite3 的基本使用(增 删 改 查)
2017-04-17 15:59
148 查看
//
// ViewController.m
// test_sqlite
//
// Created by 郑玉洋 on 2017/4/13.
// Copyright © 2017年
郑玉洋. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)addSql:(id)sender{
//为了测试这里利用随机数与for循环进行多次插入不同数据
for (NSInteger i =
0; i < 10; i++) {
NSString * sql = [NSString
stringWithFormat:@"insert into t_text ('%@') values ('%@');",
@"name", [NSString
stringWithFormat:@"测试--%zd",arc4random_uniform(99)
]];
char * errmsg;
sqlite3_exec(db, sql.UTF8String,
NULL, NULL, &errmsg);
if (errmsg) {
NSLog(@"插入失败--%s",errmsg);
}else{
NSLog(@"插入成功");
}
}
}
- (void)delSql:(id)sender{
//这里删除随机id
大于3 小于6的
//操作代码(sql)
NSString * sql =
@"delete from t_text where id > 3 and id < 6
4000
;";
char * errmsg;
sqlite3_exec(db, sql.UTF8String,
NULL, NULL, &errmsg);
if (errmsg) {
NSLog(@"删除失败--%s",errmsg);
}else{
NSLog(@"删除成功");
}
}
- (void)updateSql:(id)sender{
//这里吧id为9的 name更改为
hello-world
//操作代码(sql)
NSString * sql =
@"update t_text set name = 'hello-world' where id = 9;";
char * errmsg;
sqlite3_exec(db, sql.UTF8String,
NULL, NULL, &errmsg);
if (errmsg) {
NSLog(@"修改失败--%s",errmsg);
}else{
NSLog(@"修改成功");
}
}
- (void)selectSql:(id)sender{
//查询所有信息
//操作代码(sql)
NSString * sql =
@"select * from t_text;";
//查询的句柄,游标
sqlite3_stmt * stmt;
if (sqlite3_prepare(db, sql.UTF8String,
-1, &stmt, NULL) ==
SQLITE_OK) {
//查询数据
while (sqlite3_step(stmt) ==
SQLITE_ROW) {
//获取表数据的内容
//sqlite3_column_text('句柄',字段索引值)
NSString * name = [NSString
stringWithCString:(const
char *)sqlite3_column_text(stmt,
1) encoding:NSUTF8StringEncoding];
NSLog(@"name = %@",name);
}
}
}
- (void)viewDidLoad {
[super
viewDidLoad];
UIButton *addBtn = [[UIButton
alloc] initWithFrame:CGRectMake(100,
100, 30,
15)];
addBtn.backgroundColor = [UIColor
blueColor];
[addBtn addTarget:self
action:@selector(addSql:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:addBtn];
UIButton *delBtn = [[UIButton
alloc] initWithFrame:CGRectMake(100,
200, 30,
15)];
delBtn.backgroundColor = [UIColor
blueColor];
[delBtn addTarget:self
action:@selector(delSql:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:delBtn];
UIButton *updateBtn = [[UIButton
alloc] initWithFrame:CGRectMake(100,
300, 30,
15)];
updateBtn.backgroundColor = [UIColor
blueColor];
[updateBtn addTarget:self
action:@selector(updateSql:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:updateBtn];
UIButton *selectBtn = [[UIButton
alloc] initWithFrame:CGRectMake(100,
600, 30,
15)];
selectBtn.backgroundColor = [UIColor
blueColor];
[selectBtn addTarget:self
action:@selector(selectSql:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:selectBtn];
//设置数据库的路径
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)
firstObject];
NSString *fileName = [path
stringByAppendingPathComponent:@"text.sqlite"];
NSLog(@"fileName = %@",fileName);
//打开数据库
如果没有打开的数据库就建立一个
//第一个参数是数据库的路径
注意要转换为c的字符串
if (sqlite3_open(fileName.UTF8String, &db)
== SQLITE_OK) {
NSLog(@"打开数据库成功");
//打开数据库成功后建立数据库内的表
//操作命令的字符串
//注意字符串的结束处有 ;
号
NSString * sql =
@"create table if not exists t_text (id integer primary key autoincrement,name text);";
char * errmsg;
9461
sqlite3_exec(db, sql.UTF8String,
NULL, NULL, &errmsg);
if (errmsg) {
NSLog(@"建表失败 -- %s",errmsg);
}else{
NSLog(@"建表成功");
}
}else{
NSLog(@"打开数据库失败");
}
}
@end
// ViewController.m
// test_sqlite
//
// Created by 郑玉洋 on 2017/4/13.
// Copyright © 2017年
郑玉洋. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)addSql:(id)sender{
//为了测试这里利用随机数与for循环进行多次插入不同数据
for (NSInteger i =
0; i < 10; i++) {
NSString * sql = [NSString
stringWithFormat:@"insert into t_text ('%@') values ('%@');",
@"name", [NSString
stringWithFormat:@"测试--%zd",arc4random_uniform(99)
]];
char * errmsg;
sqlite3_exec(db, sql.UTF8String,
NULL, NULL, &errmsg);
if (errmsg) {
NSLog(@"插入失败--%s",errmsg);
}else{
NSLog(@"插入成功");
}
}
}
- (void)delSql:(id)sender{
//这里删除随机id
大于3 小于6的
//操作代码(sql)
NSString * sql =
@"delete from t_text where id > 3 and id < 6
4000
;";
char * errmsg;
sqlite3_exec(db, sql.UTF8String,
NULL, NULL, &errmsg);
if (errmsg) {
NSLog(@"删除失败--%s",errmsg);
}else{
NSLog(@"删除成功");
}
}
- (void)updateSql:(id)sender{
//这里吧id为9的 name更改为
hello-world
//操作代码(sql)
NSString * sql =
@"update t_text set name = 'hello-world' where id = 9;";
char * errmsg;
sqlite3_exec(db, sql.UTF8String,
NULL, NULL, &errmsg);
if (errmsg) {
NSLog(@"修改失败--%s",errmsg);
}else{
NSLog(@"修改成功");
}
}
- (void)selectSql:(id)sender{
//查询所有信息
//操作代码(sql)
NSString * sql =
@"select * from t_text;";
//查询的句柄,游标
sqlite3_stmt * stmt;
if (sqlite3_prepare(db, sql.UTF8String,
-1, &stmt, NULL) ==
SQLITE_OK) {
//查询数据
while (sqlite3_step(stmt) ==
SQLITE_ROW) {
//获取表数据的内容
//sqlite3_column_text('句柄',字段索引值)
NSString * name = [NSString
stringWithCString:(const
char *)sqlite3_column_text(stmt,
1) encoding:NSUTF8StringEncoding];
NSLog(@"name = %@",name);
}
}
}
- (void)viewDidLoad {
[super
viewDidLoad];
UIButton *addBtn = [[UIButton
alloc] initWithFrame:CGRectMake(100,
100, 30,
15)];
addBtn.backgroundColor = [UIColor
blueColor];
[addBtn addTarget:self
action:@selector(addSql:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:addBtn];
UIButton *delBtn = [[UIButton
alloc] initWithFrame:CGRectMake(100,
200, 30,
15)];
delBtn.backgroundColor = [UIColor
blueColor];
[delBtn addTarget:self
action:@selector(delSql:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:delBtn];
UIButton *updateBtn = [[UIButton
alloc] initWithFrame:CGRectMake(100,
300, 30,
15)];
updateBtn.backgroundColor = [UIColor
blueColor];
[updateBtn addTarget:self
action:@selector(updateSql:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:updateBtn];
UIButton *selectBtn = [[UIButton
alloc] initWithFrame:CGRectMake(100,
600, 30,
15)];
selectBtn.backgroundColor = [UIColor
blueColor];
[selectBtn addTarget:self
action:@selector(selectSql:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:selectBtn];
//设置数据库的路径
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)
firstObject];
NSString *fileName = [path
stringByAppendingPathComponent:@"text.sqlite"];
NSLog(@"fileName = %@",fileName);
//打开数据库
如果没有打开的数据库就建立一个
//第一个参数是数据库的路径
注意要转换为c的字符串
if (sqlite3_open(fileName.UTF8String, &db)
== SQLITE_OK) {
NSLog(@"打开数据库成功");
//打开数据库成功后建立数据库内的表
//操作命令的字符串
//注意字符串的结束处有 ;
号
NSString * sql =
@"create table if not exists t_text (id integer primary key autoincrement,name text);";
char * errmsg;
9461
sqlite3_exec(db, sql.UTF8String,
NULL, NULL, &errmsg);
if (errmsg) {
NSLog(@"建表失败 -- %s",errmsg);
}else{
NSLog(@"建表成功");
}
}else{
NSLog(@"打开数据库失败");
}
}
@end
相关文章推荐
- 第四十二篇:IOS 的 sqlite3 一些基本使用
- iOS海哥开发笔记 (海哥原创,数据存储篇之sqlite3的基本使用)
- iOS- SQLite3的基本使用
- iOS之SQLite基本使用
- iOS App使用SQLite之句柄的定义及数据库的基本操作
- iOS- 简单说说iOS移动客户端SQLite3的基本使用
- iOS sqlite3 的基本使用(增 删 改 查)
- iOS- 简单说说iOS移动客户端SQLite3的基本使用
- ios-SQLite基本使用创建多表
- iOS sqlite基本使用
- iOS之SQLite基本使用
- iOS小明开发笔记(一) (数据存储篇之sqlite3的基本使用)
- iOS中sqlite的基本使用
- ios Sqlite 的基本使用
- iOS开发 - 滑动控制屏幕亮度和系统音量(附加AVAudioPlayer基本用法和Masonry简单使用)
- iOS -- UITableView基本使用
- IOS线程数据篇10之Sqlite3数据库的使用
- SQLite3在iOS的使用
- iOS开发UI篇—UITableview控件基本使用
- iOS NSOperation基本使用