当前位置:Gxlcms > 数据库问题 > 数据存储_FMDB

数据存储_FMDB

时间:2021-07-01 10:21:17 帮助过:24人阅读

 1 //
 2 //  YYViewController.m
 3 //  04-FMDB基本使用
 4 //
 5 //  Created by apple on 14-7-27.
 6 //  Copyright (c) 2014年 wendingding. All rights reserved.
 7 //
 8 
 9 #import "YYViewController.h"
10 #import "FMDB.h"
11 
12 @interface YYViewController ()
13 @property(nonatomic,strong)FMDatabase *db;
14 @end
15 
16 @implementation YYViewController
17 
18 - (void)viewDidLoad
19 {
20     [super viewDidLoad];
21     //1.获得数据库文件的路径
22     NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
23     NSString *fileName=[doc stringByAppendingPathComponent:@"student.sqlite"];
24     
25     //2.获得数据库
26     FMDatabase *db=[FMDatabase databaseWithPath:fileName];
27     
28     //3.打开数据库
29     if ([db open]) {
30         //4.创表
31         BOOL result=[db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL);"];
32         if (result) {
33             NSLog(@"创表成功");
34         }else
35         {
36             NSLog(@"创表失败");
37         }
38     }
39     self.db=db;
40 
41 }
42 
43 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
44 {
45     [self delete];
46     [self insert];
47     [self query];
48 }
49 
50 //插入数据
51 -(void)insert
52 {
53     for (int i = 0; i<10; i++) {
54         NSString *name = [NSString stringWithFormat:@"jack-%d", arc4random_uniform(100)];
55         // executeUpdate : 不确定的参数用?来占位
56         [self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?, ?);", name, @(arc4random_uniform(40))];
57         //        [self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?, ?);" withArgumentsInArray:@[name, @(arc4random_uniform(40))]];
58         
59         // executeUpdateWithFormat : 不确定的参数用%@、%d等来占位
60         //        [self.db executeUpdateWithFormat:@"INSERT INTO t_student (name, age) VALUES (%@, %d);", name, arc4random_uniform(40)];
61     }
62 }
63 
64 //删除数据
65 -(void)delete
66 {
67     //    [self.db executeUpdate:@"DELETE FROM t_student;"];
68     [self.db executeUpdate:@"DROP TABLE IF EXISTS t_student;"];
69     [self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL);"];
70 }
71 
72 //查询
73 - (void)query
74 {
75     // 1.执行查询语句
76     FMResultSet *resultSet = [self.db executeQuery:@"SELECT * FROM t_student"];
77     
78     // 2.遍历结果
79     while ([resultSet next]) {
80         int ID = [resultSet intForColumn:@"id"];
81         NSString *name = [resultSet stringForColumn:@"name"];
82         int age = [resultSet intForColumn:@"age"];
83         NSLog(@"%d %@ %d", ID, name, age);
84     }
85 }
86 
87 @end
技术分享

打印查看结果:

技术分享

提示:

如果ID设置为逐渐,且设置为自动增长的话,那么把表中的数据删除后,重新插入新的数据,ID的编号不是从0开始,而是接着之前的ID进行编号。

注意:

  不要写成下面的形式,不要加‘‘,直接使用%@,它会自动认为这是一个字符串。

技术分享

 

 

 

原文链接: http://www.cnblogs.com/wendingding/p/3871848.html  

数据存储_FMDB

标签:2014年   存储   ace   res   domain   查询   执行sql   tle   多线程   

人气教程排行