现在网站开发和软件开发,数据库的支持是少不了的;在iPhone开发中,作为数据持久化的解决方案中,SQLite是不错的选择,它既轻量占用资源少,又可以方便嵌入到程序中,在一些嵌入式设备中有着广泛使用。

SQLite提供了命令行工具sqlite3,创建创建库。

cjdx@~/Desktop$ sqlite3 school.sqlite3
SQLite version 3.6.12
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>

创建student表

sqlite> create table student (key integer primary key, name text, age integer, birth date);

插入三条数据

sqlite> insert into student (name, age, birth) values ('zhangsan', 18, '1980-01-09');
sqlite> insert into student (name, age, birth) values ('lisi', 20, '1980-10-05');
sqlite> insert into student (name, age, birth) values ('wangwu', 28, '1985-12-20');

查询刚刚插入的数据

sqlite> select * from student;1|zhangsan|18|1980-01-092|lisi|20|1980-10-053|wangwu|28|1985-12-20

SQLite函数知识准备

sqlite3_open 打开数据库
sqlite3_close 关闭数据库
sqlite3_prepare 预编译SQL语句
sqlite3_step 执行预编译后的SQL
sqlite3_finalize 释放资源

打开Xcode,创建iPhone项目,基于“Single View Application”,并命名为TestSQLite,把刚才创建的school.sqlite3添加项目“Supporting Files”目录。

先写些测试代码,看看sqlite能不能正常工作,在ViewController.m中添加如下代码:

//  ViewController.m  #import "ViewController.h" #import "/usr/include/sqlite3.h"  @implementation ViewController...#pragma mark - View lifecycle  - (void)viewDidLoad{     [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.     sqlite3 *db;char *szError = NULL;sqlite3_stmt *dbps;NSString *dbFile = [[NSBundle mainBundle] pathForResource:@"school" ofType:@"sqlite3"];if (sqlite3_open([dbFile UTF8String], &db) != SQLITE_OK) {         NSLog(@"failed to open db.");}  NSString *sql_insert = @"insert into student (name, age, birth) values ('testdata', 16, '1987-09-18')";if (sqlite3_exec(db, [sql_insert UTF8String], 0, 0, &szError) == SQLITE_OK) {         NSLog(@"%d", sqlite3_changes(db));}  NSString *sql_select = @"SELECT * FROM student";sqlite3_prepare_v2(db, [sql_select UTF8String], -1, &dbps, NULL);int nResult = sqlite3_step(dbps);for (int fld = 0; fld < sqlite3_column_count(dbps); fld++) {         NSLog(@"%s", sqlite3_column_name(dbps, fld));}  while (nResult != SQLITE_DONE) {         NSLog(@"%s|%s|%s|%s", sqlite3_column_text(dbps, 0),sqlite3_column_text(dbps, 1),sqlite3_column_text(dbps, 2),sqlite3_column_text(dbps, 3));nResult = sqlite3_step(dbps);}  sqlite3_close(db);} ...@end 

把sqlite3连接库添加到项目

输出结果:

2012-02-06 18:59:33.372 TestSQLite[4011:207] 1
2012-02-06 18:59:33.375 TestSQLite[4011:207] key
2012-02-06 18:59:33.377 TestSQLite[4011:207] name
2012-02-06 18:59:33.379 TestSQLite[4011:207] age
2012-02-06 18:59:33.380 TestSQLite[4011:207] birth
2012-02-06 18:59:33.384 TestSQLite[4011:207] 1|zhangsan|18|1980-01-09
2012-02-06 18:59:33.386 TestSQLite[4011:207] 2|lisi|20|1980-10-05
2012-02-06 18:59:33.387 TestSQLite[4011:207] 3|wangwu|28|1985-12-20
2012-02-06 18:59:33.405 TestSQLite[4011:207] 4|testdata|16|1987-09-18

在Objc中直接用SQLite的C API写,如果每个查询都这样,太繁琐了,而且也不OO,所以还是要封装一下的,这样就可以把先前学过的知识串起来,写个小程序,用TableView来显示数据,支持数据的输入、删除。

  • ♥ 1.创建Student模型类
  • ♥ 2.创建StudentDB类
  • ♥ 3.创建TableViewController用来显示数据
  • ♥ 4.创建添加数据界面和代码的实现
  • ♥ 5.删除代码的实现

接下来就按照这个步骤,一步步实现这些功能。

创建Student模型类

//  Student.h  #import <Foundation/Foundation.h>  @interface  Student : NSObject {     int uniqueId;NSString *name;int age;NSDate *birth;}  @property (nonatomic, assign) int uniqueId;@property (nonatomic, retain) NSString *name;@property (nonatomic, assign) int age;@property (nonatomic, retain) NSDate *birth;- (id)initWithUniqueId:(int)uniqueId name:(NSString *)name age:(int)age birth:(NSDate *)birth;@end 

//  Student.m  #import "Student.h"  @implementation Student@synthesize uniqueId, name, age, birth;- (id)initWithUniqueId:(int)uniqueId name:(NSString *)name age:(int)age birth:(NSDate *)birth{     self = [super init];if (self) {         self.uniqueId = uniqueId;self.name = name;self.age = age;self.birth = birth;}  return self;}  - (void)dealloc{     name = nil;birth = nil;[super dealloc];}  @end 

创建StudentDB类

StudentDB类是sqlite的简单封装,获取的数据被包装到Student类中,有点ORM的感觉,代码长的就不贴代码,只贴一些关键代码。

// StudentDB.m  #import "StudentDB.h" #import "Student.h"  @implementation StudentDB@synthesize db;- (id)init{     self = [super init];if (self) {         NSString *dbFile = [[NSBundle mainBundle] pathForResource:@"school" ofType:@"sqlite3"];if (sqlite3_open([dbFile UTF8String], &db) != SQLITE_OK) {             NSLog(@"failed to open db.");}     }  return self;}  -(int)getStudentsCount{ ...}  - (NSMutableArray *)getAllStudent{     sqlite3_stmt *pStmt;NSMutableArray *studentArray = [[NSMutableArray alloc] init];NSString *sql = @"SELECT * FROM student;";sqlite3_prepare_v2(db, [sql UTF8String], -1, &pStmt, nil);while (SQLITE_ROW == sqlite3_step(pStmt)) {         int uniqueId = sqlite3_column_int(pStmt, 0);NSString *name = [[NSString alloc] initWithUTF8String:(const char*)sqlite3_column_text(pStmt, 1)];int age = sqlite3_column_int(pStmt, 2);NSDateFormatter *formate = [[NSDateFormatter alloc] init];[formate setDateFormat:@"yyyy-MM-dd"];NSDate *birth= [formate dateFromString:[[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(pStmt, 3)]];Student *student = [[Student alloc] initWithUniqueId:uniqueId name:name age:age birth:birth];[studentArray addObject:student];[formate release];[name release];[student release];}  sqlite3_finalize(pStmt);return studentArray;}  - (void)removeStudent:(Student *)person{ ...}  - (void)addStudent:(Student *)person{ ...}  - (void)dealloc{     sqlite3_close(db);[super dealloc];}  @end 

创建TableViewController用来显示数据

通过StudentDB类的getAllStudent方法获取所有学生的数组作为数据源

// StudentListViewController.h  #import <UIKit/UIKit.h>  @class  StudentDB;@interface  StudentListViewController : UITableViewController{     StudentDB *db;NSMutableArray *students;}  @end 

#import "StudentListViewController.h" #import "StudentDB.h" #import "Student.h" #import "AddStudentViewController.h"  @implementation StudentListViewController- (void)viewDidLoad{     // 初始化db     db = [[StudentDB alloc] init];[super viewDidLoad];}  - (void)viewWillAppear:(BOOL)animated{  // 获取所有学生数据保存到students数组中     students = [db getAllStudent];[self.tableView reloadData];[super viewWillAppear:animated];}  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{     return 1;}  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    // 返回行数     return students.count;}  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{     static NSString *CellIdentifier = @"Cell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];if (cell == nil) {         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];}  // Configure the cell...     Student *student = [students objectAtIndex:indexPath.row];cell.textLabel.text = student.name;NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];[dateFormatter setDateFormat:@"yyyy-MM-dd"];NSString *strBirth = [dateFormatter stringFromDate:student.birth];NSString *strDescription = [[NSString alloc] initWithFormat:@"年龄:%d 生日:%s", student.age,[strBirth UTF8String]];[cell.detailTextLabel setText:strDescription];[dateFormatter release];[strDescription release];return cell;}  - (void)dealloc{     [students release];[db release];[super dealloc];}  @end 

数据显示效果图:

创建添加数据界面和代码的实现

通过点击导航右边的的“+”号按钮来显示,录入学生资料界面,界面通过纯代码创建,点击导航栏右边的”Done”来完成录入数据工作,然后返回学生列表界面执行reloadData操作,这样新录入的数据就能显示出来了

// AddStudentViewController.m  // 执行添加数据到数据库中的操作,没有验证性操作 - (void)doneButtonPushed:(id)sender{     StudentDB *db = [[StudentDB alloc] init];NSString *strName = txtName.text;int age = [txtAge.text intValue];NSString *strBirth = txtBirth.text;NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];[dateFormatter setDateFormat:@"yyyy-MM-dd"];Student *student = [[Student alloc] initWithUniqueId:0 name:strName age:age birth:[dateFormatter dateFromString:strBirth]];[db addStudent:student];[student release];[db release];[dateFormatter release];[self.navigationController popViewControllerAnimated:YES];}

删除代码的实现

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{     if (editingStyle == UITableViewCellEditingStyleDelete) {         // Delete the row from the data source         Student *student = [students objectAtIndex:indexPath.row];//删除数据库中的数据         [db removeStudent:student];//删除数组中的数据         [students removeObjectAtIndex:indexPath.row];//删除TableView中的数据         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];}   else if (editingStyle == UITableViewCellEditingStyleInsert) {         // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view     }   }

完整代码

下载

转载于:https://www.cnblogs.com/jackyyang7/articles/2618629.html

iPhone开发之SQLite相关推荐

  1. iPhone开发之SQLite 实现中文排序的教程

    Sqlite是一个用C语言实现的小型SQL数据库引擎.它体积小巧但功能强大,对硬件资源要求很低而且性能表现卓越,非常适合于嵌入式应用环境.最近发现sqlite并不支持中文(拼音/笔画)排序,而这个功能 ...

  2. 详解iPhone开发之Objective-C和 C 混编

    详解iPhone开发之Objective-C和 C 混编 2011-07-29 15:47 佚名 互联网 字号:T | T 本文介绍的是详解iPhone开发之Objective-C和C混编,介绍了ip ...

  3. 【无限互联】iOS开发视频教程— 2.8 iPhone开发之swtch语句

    核心内容 1. switch语句语法 2. 防止case穿透,与break结合使用 视频地址:iPhone开发之swtch语句

  4. iphone开发之Google地图实现…

    原文地址:iphone开发之Google地图实现 学习随笔 作者:若水一叶 摘自博文:http://tergol.blog.163.com/blog/static/170695028201081961 ...

  5. ANDROID开发之SQLite详解

    SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级 使用 SQLit ...

  6. IOS开发之sqlite封装

    上一节实现了最基本的增删改查,所有操作数据库的方法都写在控制器里,这样会有一个问题,如果修改CURD(增删改查)操作方法会非常麻烦,这一节我们对CURD进行封装,在控制器里直接调用封装好的工具. 下面 ...

  7. android开发之 SQLite(数据库)

    SQLite数据库存储:SQLite是一款轻量级的关系型数据库,它的运算速度非常快, 占用资源很少,通常只需要几百 K的内存就足够了,因而特别适合在移动设备上使用. 第一: 创建一个数据库.(Andr ...

  8. iphone开发之C++和Objective-C混编

    C++和Objective-C混编(官方文档翻译) 原文网址: http://developer.apple.com/iphone/library/documentation/Cocoa/Concep ...

  9. iPhone开发之Rotation

    iPhone或iPad可以支持4种朝向     UIInterfaceOrientationPortrait     UIInterfaceOrientationPortraitUpsideDown ...

最新文章

  1. 1.9 池化层-深度学习第四课《卷积神经网络》-Stanford吴恩达教授
  2. Oracle的存储过程和存储函数
  3. AAAI 2020 开源论文 | 语义感知BERT(SemBERT)
  4. 软件开发的MVC构架
  5. C#中的volatile关键字
  6. reverse函数中的begin和end迭代器
  7. C#照片合成PDF_ PDF合成或拆分PDF_PDF获取页数
  8. 将数据与OpenLayers结合在一起
  9. 美国硕士计算机机械专业排名,工科“三巨头”之一-机械工程的美国硕士申请全解答...
  10. 少儿编程scratch -- 提高篇
  11. AirServer最新Win64位个人版投屏软件
  12. 艺赛旗(RPA)Python:遍历输出某路径下的所有文件和文件夹
  13. 仿照jetty的nio原理写了个例子
  14. Matlab矩阵论矩阵分析计算实现(四)求史密斯标准型和约当标准型
  15. java main 方法使用 HttpClients发送请求 不打印debug日志
  16. 导航上显示某个地点已关闭什么意思_你的手机地图APP只用来导航?这些隐藏功能不用就太可惜了!...
  17. 超级表格超好用的4个功能,你知道其中几个?
  18. Python3抓取Bing每日图片做桌面背景,并设置为开机更新背景
  19. 高德离线数据api_使用离线地图-创建地图-开发指南-iOS 地图SDK | 高德地图API
  20. windows计算机没有网络适配器,Win10 1909专业版中没有网络适配器解决方法

热门文章

  1. 第八周项目3-顺序串算法
  2. 本福特定律和齐夫定律是一回事吗
  3. DS18B20程序代码图片
  4. iOS屏幕旋转,强制横竖屏
  5. 简单解析表格table标签的用法
  6. CDGA认证|一文浅析数据治理与数据管理的区别
  7. CheckBox设置Enabled为False后,无法修改ForeColor
  8. scp、rsync与集群分发
  9. bmp文件格式详细解析(转载)
  10. dgl-01 deepwalk