在ios中使用sqlite来处理数据。如果你已经了解了sql,那你可以很容易的掌握sqlite数据库的操作。ios对于数据库的操作:增加、删除、查找、修改具体介绍如下所示:
首先需要创建一个数据库:本程序的数据库是在火狐浏览器里的插件里写的微量型数据库
火狐找查找sqlite manager的步骤:
第一步:在工具栏找到附加组件,点击进入
第二步:搜索 sqp,找到并下载,安装完成之后需要重启浏览器
第三步:在工具只乐观找到sqlite manager,点击打开
sqlite manager界面如图所示
注:sqlite manager是微量型的数据库编程软件,所以一次只能执行一句代码!!!
•创建数据库
1
2
3
|
--数据库的建立 create table team ( -- 创建名字为team的table stu_id integer primary key autoincrement, stu_name varchar(100), stu_password varchar(100), stu_login varchar(100) )--添加信息 insert into team(stu_name,stu_password,stu_login) values( 'xiaming' , '123456' , 'xm' ) insert into team(stu_name,stu_password,stu_login) values( 'zhangsan' , '123456' , ' zs' ) --查询信息select *from team --删除信息 delete from team where stu_id=3 |
工程目录文件如下:
这里需要导入一个系统自带的文件libsqlite3.0.tbd
步骤如图:
•实现工程
viewcontroller.h
1
2
3
4
5
6
7
8
|
#import <uikit/uikit.h> #import <sqlite3.h> @interface viewcontroller : uiviewcontroller @property(strong,nonatomic)uibutton *showbtn; @property(strong,nonatomic)uibutton *insertbtn; @property(strong,nonatomic)uibutton *updatebtn; @property(strong,nonatomic)uibutton *deletebtn; @end |
viewcontroller.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
#import "viewcontroller.h" #define path [[nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) lastobject] stringbyappendingpathcomponent:@"team.sqlite"] @interface viewcontroller () @end @implementation viewcontroller - ( void )viewdidload { [super viewdidload]; // 获取沙盒 documents文件路径 nslog(@ "%@" ,[nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) lastobject]); [self button]; } -( void )button { self.showbtn=[uibutton buttonwithtype:uibuttontypesystem]; self.showbtn.frame=cgrectmake(100, 50, 200, 50); [self.showbtn settitle:@ "数据库显示" forstate:uicontrolstatenormal]; [self.showbtn addtarget:self action:@selector(showsql) forcontrolevents:uicontroleventtouchupinside]; [self.view addsubview:self.showbtn]; self.insertbtn=[uibutton buttonwithtype:uibuttontypesystem]; self.insertbtn.frame=cgrectmake(100, 100, 200, 50); [self.insertbtn settitle:@ "数据库添加" forstate:uicontrolstatenormal]; [self.insertbtn addtarget:self action:@selector(insertsql) forcontrolevents:uicontroleventtouchupinside]; [self.view addsubview:self.insertbtn]; self.updatebtn=[uibutton buttonwithtype:uibuttontypesystem]; self.updatebtn.frame=cgrectmake(100, 150, 200, 50); [self.updatebtn settitle:@ "数据库修改" forstate:uicontrolstatenormal]; [self.updatebtn addtarget:self action:@selector(updatesql) forcontrolevents:uicontroleventtouchupinside]; [self.view addsubview:self.updatebtn]; self.deletebtn=[uibutton buttonwithtype:uibuttontypesystem]; self.deletebtn.frame=cgrectmake(100, 200, 200, 50); [self.deletebtn settitle:@ "数据库删除" forstate:uicontrolstatenormal]; [self.deletebtn addtarget:self action:@selector(deletesql) forcontrolevents:uicontroleventtouchupinside]; [self.view addsubview:self.deletebtn]; } #pragma mark - 显示数据表中的所有信息 -( void )showsql { nslog(@ "显示数据库信息" ); // 数据库 sqlite3 *db; // 根据指定的数据库文件存储路径打开数据库 int result=sqlite3_open([path utf8string], &db); // 创建执行命令对象 sqlite3_stmt *stmt; // 打开数据库成功 if (result==sqlite_ok) { nslog(@ "连接成功" ); // 执行预处理命令 int res=sqlite3_prepare_v2(db, "select *from team" , -1, &stmt, nil); if (res==sqlite_ok) { // 循环遍历数据表的行信息 while (sqlite3_step(stmt)==sqlite_row) { // 获取数据表中整型列的信息 int stu_id=sqlite3_column_int(stmt, 0); nslog(@ "stu_id is %d" ,stu_id); // 获取数据表中字符型的列的信息 nslog(@ "%@" ,[nsstring stringwithformat:@ "%s" ,sqlite3_column_text(stmt, 1)]); nslog(@ "%@" ,[nsstring stringwithformat:@ "%s" ,sqlite3_column_text(stmt, 2)] ); nslog(@ "%@" ,[nsstring stringwithformat:@ "%s" ,sqlite3_column_text(stmt, 3)] ); } } } } #pragma mark -增加信息 -( void )insertsql { sqlite3 *db; sqlite3_stmt *stmt; sqlite3_open([path utf8string], &db); int rst=sqlite3_prepare_v2(db, "insert into team(stu_name,stu_password,stu_login) values(?,?,?)" , -1, &stmt, nil); sqlite3_bind_text(stmt, 1, "wangwu" , -1, nil); sqlite3_bind_text(stmt, 2, "123456" , -1, nil); sqlite3_bind_text(stmt, 3, "ww" , -1, nil); // 判断是否增加成功 if (rst==sqlite_ok) { if (sqlite_done==sqlite3_step(stmt)) { nslog(@ "add ok" ); } else { nslog(@ "add fail" ); } } } #pragma mark - 修改数据库 -( void )updatesql { sqlite3 *db; sqlite3_stmt *stmt; sqlite3_open([path utf8string], &db); // 方法一 /* int res = sqlite3_prepare_v2(db, "update team set stu_name=(?),stu_password=(?),stu_login=(?) where stu_id=2" , -1, &stmt, nil); sqlite3_bind_text(stmt, 1, "xiaoming", -1, nil); sqlite3_bind_text(stmt, 2, "123456", -1, nil); sqlite3_bind_text(stmt, 3, "xm", -1, nil); */ // 方法二 int rst=sqlite3_prepare_v2(db, "update team setstu_name='zl',stu_password='zl123',stu_login='zhangsan' where stu_id=4" , -1, &stmt, nil); // 判断是否修改成功 if (rst==sqlite_ok) { if (sqlite_done == sqlite3_step(stmt)) { nslog(@ " update ok" ); } else { nslog(@ "update fail" ); } } } -( void )deletesql { sqlite3 *db; sqlite3_stmt *stmt; sqlite3_open([path utf8string], &db); int rst=sqlite3_prepare_v2(db, "delete from team where stu_id=9" , -1, &stmt, nil); // 判断是否删除成功 if (rst==sqlite_ok) { if (sqlite_done==sqlite3_step(stmt)) { nslog(@ " delete ok" ); } else { nslog(@ "delete fail" ); } } } - ( void )didreceivememorywarning { [super didreceivememorywarning]; // dispose of any resources that can be recreated. } @end |
以上内容是小编给大家日常收集整理的ios sqlite对数据库的各种操作,希望对大家有所帮助,如果大家想了解更多有关ios sqlite相关知识请登录服务器之家网站了解详情,同时也非常感谢大家一直以来对服务器之家网站的支持!