数据库sqlite的简单应用

数据库的基本概念性的问题,你们自己百度!

这里实际操作代码操作:

学习数据库,首先对数据库语言要了解,我们先了解一些基本的数据库的操作语言

 1 /*
 2      SQLite
 3      1.创建表、删除表
 4      2.添加、删除、修改、查找----数据
 5      3.常用的SQL语句
 6      4.创建表格 : create table student ( name text,  sex text, age integer)
 7      (创建名为Student的表格 内容为 name ***)
 8      
 9      1** text:字符串  2** integer : int   3** real :float   4** blob : data
10      
11      
12      创建表格的时候指定主键(唯一标识一条记录的字段,不能重复):默认行数为主键(不 指定)
13      create table boss (name text , age integer , number integer primary key)
14      
15      1.添加数据(往表里面) (所有的列)
16      各个值之间要用 , 隔开   text类型要用 ''
17      insert into student values ('hehe','m',18)
18      
19      
20      2.给指定的列添加数据 :  insert into student (name,sex) values ('haha','f')
21      
22      3.删除数据,如果根据主键来删除,最多只能删除一条
23      delete from boss where number = 1
24      
25      
26      4.如果根据某个字段删除数据,如果不是根据主键,所有这个字段一样的记录都会被删掉
27      delete from boss where name = 'ppp'
28      
29      
30      修改student 表中 name = 'ppp'的数据 将它的 sex 改为 f|m
31      update  student set sex = 'f|m' where name = 'ppp'
32      
33      
34      5.查找【字段】(从student查找年龄为19所有人得名字)
35      select name from student where age = 19
36      
37      查找一条【记录】的信息
38      select * from student where age = 19
39      
40      查到表格的所有【记录】
41      select * from student
42      
43      6.查找sex字段中 以f开头的模糊  (模糊查找)
44      select * from student where sex like 'f%'
45      
46      
47      7.删除表(boss)
48      drop table boss
49      
50      
51      要在工程使用SQLite,需要导入 libsqlite3.0.dylib 动态库
52      
53      */

我们接下来创建一个简单的数据库单例:.h文件如下

 1 #import <Foundation/Foundation.h>
 2 #import <sqlite3.h>
 3 @class Student ;
 4 @interface DataBaseManager : NSObject
 5 
 6 
 7 //实例变量
 8 {
 9     //数据指针,通过指针可以操作对应的数据库
10     sqlite3 *dbPoint ;
11     
12 }
13 
14 //单例方法
15 +(instancetype)shareInstance;
16 
17 //多线程下保证数据库安全的单例写法
18 +(instancetype)shareInstanceAnotherWay;
19 
20 //GCD保证
21 +(instancetype)shareInstanceGCD;
22 
23 //打开数据库
24 -(void)openDb;
25 
26 
27 //关闭数据库
28 -(void)closeDb;
29 
30 
31 //创建表
32 -(void)createTable;
33 
34 //插入列
35 
36 -(void)createAlterTable;
37 
38 //删除表
39 -(void)dropTable;
40 
41 
42 //添加数据(插入学生)
43 
44 -(void)insertStudent:(Student *)stu ;
45 
46 //写入二进制数据
47 -(void)insertStudentAnotherPose:(Student *)stu ;
48 
49 
50 
51 //删除数据(删除学生)
52 
53 -(void)deleteStudent:(Student *)stu ;
54 
55 //修改信息
56 -(void)updateStudent:(Student *)stu withName:(NSString *)name ;
57 
58 //查找所有学生
59 -(NSMutableArray *)selectAllStudent;

.m文件

  1 #import "DataBaseManager.h"
  2 #import "Student.h"
  3 @implementation DataBaseManager
  4 
  5 
  6 /*
  7   单例对象:
  8          1.在不同类中通过单例方法获取的都是一个对象
  9          2.保证多线程开发中数据库的安全
 10          3.可以使用单例来传值
 11          4.
 12  */
 13 
 14 +(instancetype)shareInstance{
 15     //单例方法的实现
 16     
 17     //创建一个空对象
 18     static DataBaseManager *dbManger = nil ;
 19     
 20     //如果为空,则创建一个对象
 21     if (nil == dbManger) {
 22         dbManger = [[DataBaseManager alloc]init];
 23     }
 24     
 25     
 26     
 27     return dbManger ;
 28 }
 29 
 30 
 31 
 32 //安全锁 单例
 33 
 34 +(instancetype)shareInstanceAnotherWay{
 35     
 36     static DataBaseManager *dbManger = nil ;
 37     
 38     @synchronized(self){ //加线程锁,线程锁中的代码会受到保护,保证此时没有其它线程访问self对象
 39         
 40         if (dbManger == nil) {
 41             
 42             dbManger = [[DataBaseManager alloc]init];
 43             
 44         }
 45         
 46     }
 47     
 48     return dbManger ;
 49 }
 50 
 51 
 52 //GCD
 53 +(instancetype)shareInstanceGCD{
 54     
 55     static DataBaseManager *dbManger = nil ;
 56     
 57     //声明一个只执行一次的多线程
 58     static dispatch_once_t once ;
 59     
 60     dispatch_once(&once, ^{
 61        
 62         dbManger = [[DataBaseManager alloc]init];
 63     });
 64     
 65     
 66     
 67     return dbManger ;
 68 }
 69 
 70 
 71 
 72 
 73 
 74 
 75 
 76 
 77 //打开数据库
 78 
 79 -(void)openDb{
 80     
 81     //想要打开的数据可路径
 82     NSString *dbPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"student.db"];
 83     
 84      NSLog(@"%@",dbPath);
 85     
 86     /*
 87      参数1:想要打开的数据库的路径,需要的是C语言的字符串
 88      参数2:将dbPoint 指针和数据库绑定,通过dbPoint可以访问该路径下的数据库
 89      
 90      如果该路径下不存在对应的数据库,系统会自动创建一个数据库
 91      */
 92     int result = sqlite3_open(dbPath.UTF8String, &dbPoint);
 93     if (SQLITE_OK == result ) {
 94         NSLog(@"数据库打开成功");
 95     }
 96     else
 97     {
 98         NSLog(@"数据库打开失败");
 99     }
100     
101 }
102 
103 
104 
105 
106 
107 //关闭数据库
108 -(void)closeDb{
109     
110     int result = sqlite3_close(dbPoint) ;
111 
112     
113     
114     [self judgeWithResult:result action:@"关闭数据库"];
115     
116     
117 }
118 
119 
120 -(void)judgeWithResult:(int)result action:(NSString *)actionStr{
121     
122     if (result == SQLITE_OK) {
123         NSLog(@"%@成功",actionStr);
124     }
125     else
126     {
127         NSLog(@"%@失败",actionStr);
128     }
129     
130     
131     
132 }
133 
134 
135 
136 
137 
138 //创建表
139 -(void)createTable{
140     [self openDb];
141     NSString *sqlStr = @"create table students (name text , sex text , number integer primary key)";
142     
143      /**
144      *  参数1:要使用的是哪一个数据库
145      *  参数2:想对数据做什么操作 SQL语句
146      *  参数3/4:系统预留的参数
147      *  参数5:错误信息
148      *
149      *  @return <#return value description#>
150      */
151     
152     char *error ;
153     int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, &error);
154     NSLog(@"%s",error);
155     
156     [self judgeWithResult:result action:@"创建表"];
157     
158     //销毁指针
159     sqlite3_free(error) ;
160     
161     [self closeDb];
162     
163 }
164 
165 
166 //插入列
167 -(void)createAlterTable{
168     
169     [self openDb];
170     
171     NSString *string = @"alter table students add (image blob) ";
172     
173     char *error ;
174     
175     int result = sqlite3_exec(dbPoint, string.UTF8String, NULL, NULL, &error);
176     
177     [self judgeWithResult:result action:@"插入列"];
178     
179     sqlite3_free(error);
180     
181     [self closeDb];
182     
183     
184     
185 }
186 
187 
188 
189 //删除表
190 
191 -(void)dropTable{
192     [self openDb];
193     
194     NSString *sqlStr = @"drop table students " ;
195     
196     char *error1 ;
197     int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, &error1);
198     NSLog(@"%s",error1);
199     
200     [self judgeWithResult:result action:@"删除表"];
201     
202     //销毁指针
203     sqlite3_free(error1) ;
204     
205     [self closeDb];
206 
207     
208 }
209 
210 
211 //添加学生
212 
213 -(void)insertStudent:(Student *)stu{
214     
215     [self openDb];
216     
217     NSString *sqlStr = [NSString stringWithFormat:@"insert into students values ('%@','%@',%ld)",stu.name,stu.sex,stu.number];
218     
219     char *error ;
220     
221     int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, &error);
222     
223     [self judgeWithResult:result action:@"插入学生"];
224     
225     sqlite3_free(error);
226     
227     [self closeDb];
228     
229 }
230 
231 
232 -(void)insertStudentAnotherPose:(Student *)stu{
233     
234     [self openDb];
235     
236     //跟随指针
237     sqlite3_stmt *stmt = nil ;
238     
239     int result = sqlite3_prepare(dbPoint, "insert into students (name,sex,number,image) values (?,?,?,?)", -1, &stmt, NULL);
240     
241     if (result == SQLITE_OK) {
242         /*
243          如果SQL没有问题,则绑定插入的数据 (写入)
244          参数1:把柄 stmt
245          参数2:给SQL语句中的第几个 ?赋值
246          参数3:写入的内容
247          参数4:写入数据的长度
248          参数5:系统预留的参数
249          */
250         
251         sqlite3_bind_text(stmt, 1, stu.name.UTF8String, -1, NULL);
252         
253         sqlite3_bind_text(stmt, 2, stu.sex.UTF8String, -1, NULL);
254         
255         sqlite3_bind_int(stmt, 3, (int)stu.number);
256         
257         /*
258          UIImage需要转成NSData才能写入数据库
259          参数1:想要转得那张图片
260          参数2:压缩程度(0~1),压缩数值越大,图片质量越低(压缩完之后再取出来的图片)
261          */
262         NSData *data = UIImageJPEGRepresentation(stu.image, 0.6);
263         
264         //参数3:首字节起始位置的指针
265         //参数4:二进制数据的长度,就是有多少个字节
266         sqlite3_bind_blob(stmt, 4, [data bytes], (int)data.length, NULL);
267         
268         int addResult = sqlite3_step(stmt);
269         
270         if (addResult == SQLITE_OK) {
271             
272             NSLog(@"插入学生图片成功");
273         }
274         
275         
276     }
277     
278     sqlite3_finalize(stmt);
279     
280     [self closeDb];
281     
282 
283     
284     
285 }
286 
287 
288 
289 
290 //删除学生
291 
292 -(void)deleteStudent:(Student *)stu{
293     
294     [self openDb];
295     
296     NSString *sqlStr = [NSString stringWithFormat:@"delete from students where number = %ld ",stu.number];
297     char *error ;
298     
299     int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, &error);
300     
301     [self judgeWithResult:result action:@"删除学生"];
302     
303     sqlite3_free(error);
304     
305     [self closeDb];
306     
307 }
308 
309 //修改学生
310 
311 -(void)updateStudent:(Student *)stu withName:(NSString *)name{
312     
313     [self openDb];
314     
315     NSString *sqlStr = [NSString stringWithFormat:@"update students set name = '%@' where number = %ld ",stu.name ,stu.number];
316     char *error ;
317     
318     int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, &error);
319     
320     [self judgeWithResult:result action:@"修改学生"];
321     
322     sqlite3_free(error);
323     
324     [self closeDb];
325     
326 }
327 
328 
329 //查找所有学生
330 -(NSMutableArray *)selectAllStudent{
331     
332     [self openDb];
333     
334     NSMutableArray *stuArray = [NSMutableArray array];
335     
336     NSString *sqlStr = @"select * from students" ;
337     
338     //创建指针(数据库的状态指针,数据库执行语句的所有结果都保存在这个指针里面)
339     sqlite3_stmt *stmt= nil ;
340     /*
341      *执行SQL语句,并且将执行结果保存在stmt中
342      参数1:数据库指针
343      参数2:要执行的SQL语句
344      参数3:限制SQL语句的长度,-1就是不限制
345      参数4:stmt指针
346      参数5:
347      */
348     int result = sqlite3_prepare(dbPoint, sqlStr.UTF8String, -1, &stmt, NULL);
349     
350     if (result == SQLITE_OK) {
351         
352         //遍历stmt中的数据,一行一行的遍历
353         while (sqlite3_step(stmt) == SQLITE_ROW) {
354             
355             Student *stu = [[Student alloc]init];
356             
357             /*
358              参数1:状态指针
359              参数2:去第几列的值(从0开始计数)
360              */
361             
362             const unsigned char *nameChar = sqlite3_column_text(stmt, 0);
363             
364             //将C语言的字符串转化为OC字符串
365             NSString *name = [NSString stringWithUTF8String:(const char *)nameChar];
366             
367             stu.name = name ;
368             
369             stu.sex = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
370             
371             stu.number = sqlite3_column_int(stmt, 2) ;
372             
373             //数据库取出的  sqlite3_column_blob(stmt, 3)的二进制数据 是 const void * 类型 要转化为NSData类型
374             stu.image = [UIImage imageWithData:[NSData dataWithBytes:sqlite3_column_blob(stmt, 3) length:1]];
375             
376             [stuArray addObject:stu];
377             
378             [stu release];
379         }
380         
381     }
382     //保证同一时间只有一个
383     sqlite3_finalize(stmt) ;
384     
385     [self closeDb];
386     
387     return stuArray ;
388     
389     
390 }

 动动手调用看看效果:

 1 -(void)initSQLite{
 2     
 3     //初始化一个单例对象
 4     DataBaseManager *dbManager = [DataBaseManager shareInstance];
 5     
 6     //打开数据库
 7 //    [dbManager openDb];
 8     
 9     //关闭数据库
10 //    [dbManager closeDb];
11     
12     //创建表格
13     [dbManager createTable];
14     
15     //删除表格
16 //    [dbManager dropTable];
17     
18     //插入列
19     [dbManager createAlterTable];
20     
21     //初始化对象
22 //    Student *stu = [[Student alloc]initWithName:@"小明" Sex:@"男" Number:18];
23 //    [dbManager insertStudent:stu];
24 //    
25 //    Student *stu1 = [[Student alloc]initWithName:@"小强" Sex:@"女" Number:20];
26     
27     Student *stu = [[Student alloc]initWithName:@"小明" Sex:@"" Number:28 Image:nil];
28     
29     stu.image = [UIImage imageNamed:@"06.jpg"];
30     
31     //插入学生
32     [dbManager insertStudent:stu];
33     
34     //写入二进制数据
35     [dbManager insertStudentAnotherPose:stu];
36     
37     //删除学生
38 //    [dbManager deleteStudent:stu];
39     
40     //更新
41 //    [dbManager updateStudent:stu withName:@"小明"];
42     
43     //查询
44 //    NSArray *array = [dbManager selectAllStudent];
45 //    
46 //    for (Student *stu in array) {
47 //        NSLog(@"%@ %@ %ld",stu.name,stu.sex,stu.number);
48 //    }
49     
50 }

 

posted @ 2015-10-14 21:19  YX祥  阅读(289)  评论(0编辑  收藏  举报