IOS下SQLite的简单使用
本文转载至 http://www.cnblogs.com/cokecoffe/archive/2012/05/31/2537105.html
看着国外网站的教程,写了一个小例子,一个联系人的程序,包括 (姓名、地址、电话)三项内容,通过两个按钮,可以将信息保存或者查询数据库已有的信息。
UI就不说了,比较简单。贴一下关键代码,具体的话还是去看源代码(正想办法传,我这git出点问题)。

1 /*根据路径创建数据库并创建一个表contact(id nametext addresstext phonetext)*/
2
3 - (void)viewDidLoad
4 {
5 [super viewDidLoad];
6 // Do any additional setup after loading the view, typically from a nib.
7
8 NSString *docsDir;
9 NSArray *dirPaths;
10
11 // Get the documents directory
12 dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
13
14 docsDir = [dirPaths objectAtIndex:0];
15
16 // Build the path to the database file
17 databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"contacts.db"]];
18
19 NSFileManager *filemgr = [NSFileManager defaultManager];
20
21 if ([filemgr fileExistsAtPath:databasePath] == NO)
22 {
23 const char *dbpath = [databasePath UTF8String];
24 if (sqlite3_open(dbpath, &contactDB)==SQLITE_OK)
25 {
26 char *errMsg;
27 const char *sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT,PHONE TEXT)";
28 if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg)!=SQLITE_OK)
29 {
30 status.text = @"创建表失败\n";
31 }
32 }
33 else
34 {
35 status.text = @"创建/打开数据库失败";
36 }
37 }
38
39 }
40
41 /*将数据保存只数据库,当按下保存按钮的时候*/
42
43 - (IBAction)SaveToDataBase:(id)sender
44 {
45 sqlite3_stmt *statement;
46
47 const char *dbpath = [databasePath UTF8String];
48
49 if (sqlite3_open(dbpath, &contactDB)==SQLITE_OK) {
50 NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO CONTACTS (name,address,phone) VALUES(\"%@\",\"%@\",\"%@\")",name.text,address.text,phone.text];
51 const char *insert_stmt = [insertSQL UTF8String];
52 sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
53 if (sqlite3_step(statement)==SQLITE_DONE) {
54 status.text = @"已存储到数据库";
55 name.text = @"";
56 address.text = @"";
57 phone.text = @"";
58 }
59 else
60 {
61 status.text = @"保存失败";
62 }
63 sqlite3_finalize(statement);
64 sqlite3_close(contactDB);
65 }
66 }
67
68 /*根据输入的姓名来查询数据*/
69 - (IBAction)SearchFromDataBase:(id)sender
70 {
71 const char *dbpath = [databasePath UTF8String];
72 sqlite3_stmt *statement;
73
74 if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
75 {
76 NSString *querySQL = [NSString stringWithFormat:@"SELECT address,phone from contacts where name=\"%@\"",name.text];
77 const char *query_stmt = [querySQL UTF8String];
78 if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
79 {
80 if (sqlite3_step(statement) == SQLITE_ROW)
81 {
82 NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];
83 address.text = addressField;
84
85 NSString *phoneField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 1 )];
86 phone.text = phoneField;
87
88 status.text = @"已查到结果";
89 [addressField release];
90 [phoneField release];
91 }
92 else {
93 status.text = @"未查到结果";
94 address.text = @"";
95 phone.text = @"";
96 }
97 sqlite3_finalize(statement);
98 }
99
100 sqlite3_close(contactDB);
101 }
102 }

IOS下SQLite的简单使用的更多相关文章
- Win7下SQLite的简单使用
前言 SQLite 是一个软件库,实现了自给自足的.无服务器的.零配置的.事务性的 SQL 数据库引擎.SQLite 是在世界上最广泛部署的 SQL 数据库引擎.SQLite 源代码不受版权限制. 简 ...
- ios下最简单的正则,RegexKitLite
ios下最简单的正则,RegexKitLite 1.去RegexKitLite下载类库,解压出来会有一个例子包及2个文件,其实用到的就这2个文件,添加到工程中.备用地址:http://www.coco ...
- Visual Studio下SQLite数据库开发环境设置
由于我们介绍的内容都是基于微软的Visual Studio下开发的Win32平台,所以下边我们介绍Visual Studio下SQLite数据库开发环境设置.具体而言我们有两种方式可以在Visual ...
- opencv直线检测在c#、Android和ios下的实现方法
opencv直线检测在c#.Android和ios下的实现方法 本文为作者原创,未经允许,不得转载 :原文由作者发表在博客园:http://www.cnblogs.com/panxiaochun/p/ ...
- iframe在ios下无故扩大的问题探究
移动端页面内嵌了个 iframe,在 ios 下打开却发现页面怪异.比如 demo.代码如下: <!DOCTYPE html> <html lang="zh-CN" ...
- iOS下的 Fixed + Input 调用键盘的时候fixed无效问题解决方案
做touchweb开发的时候,做头疼的是,电脑上面时候好的,有些手机上面也是好的,个别手机和浏览器出现问题,对于这些,只能慢慢调试,找问题. 今天说一下比较老的IOS的问题,那就是"iOS下 ...
- iOS开发UI篇—简单介绍静态单元格的使用
iOS开发UI篇—简单介绍静态单元格的使用 一.实现效果与说明 说明:观察上面的展示效果,可以发现整个界面是由一个tableview来展示的,上面的数据都是固定的,且几乎不会改变. 要完成上面的效果, ...
- iOS下使用SHA1WithRSA算法加签源码
首先了解一下几个相关概念,以方便后面遇到的问题的解决: RSA算法:1977年由Ron Rivest.Adi Shamirh和LenAdleman发明的,RSA就是取自他们三个人的名字.算法基于一个数 ...
- 解决ios下的微信打开的页面背景音乐无法自动播放
后面的项目发现,还有两个坑,需要注意下: ·本文的解决方案的核心是利用了 微信/易信 在ready的时候会有个 WeixinJSBridgeReady/YixinJSBridgeReady事件,通过监 ...
随机推荐
- 谷歌Chrome 27测试版已经发布 更快的浏览速度
谷歌已经发布了Chrome 27测试版浏览器,为普通用户带来了大约提升为5%的网页浏览速度.用户已经可以在Chrome测试网页下载到最新的更新了.我们已经对这版更新做了一个全面的快速测试,让我们看看究 ...
- net1:DateTime,Application与Session,
原文发布时间为:2008-07-29 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- vue.js源码学习分享(九)
/* */ var arrayKeys = Object.getOwnPropertyNames(arrayMethods);//获取arrayMethods的属性名称 /** * By defaul ...
- gitlab 安装遇到 fatal:does not appear to be a git repository fatal: Could not read from remote repository. 问题
Cloning into 'door_lock_bsp'... git@192.168.1.5's password: fatal: 'door_lock/door_lock_bsp.git' do ...
- pexpect模块
pexpect用来启动子程序,使用正则表达式对程序输出做出特定响应,以此实现与其自动交互的python模块,当然我们可以使用他来做ssh登陆,ssh模块登陆还有一个基于python实现远程连接,用于s ...
- WEB学习 -相对定位、绝对定位、固定定位、z-index
相对定位 相对定位,就是微调元素位置的.让元素相对自己原来的位置,进行位置调整. .不脱标,老家留坑,形影分离 也就是说相对定位不脱标,真实位置是在老家,只不过影子出去了,可以到处飘. 3.相对定位用 ...
- Windows下php环境变量的配置
1.找到php的路径比如"E:\php_env\PHP". 2.需要保证该目录下php.ini的配置是正确的,如果是刚下载的php包,则可能需要修改相应的一些配置:将php目 ...
- 洛谷——P2912 [USACO08OCT]牧场散步Pasture Walking(lca)
题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...
- Codeforces Gym 100203I I WIN 最大流
原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 首先寻找每个I,然后枚举形状,如果匹 ...
- idea的快捷键和操作
IntelliJ Idea 常用快捷键列表 修改方法如下: 点击 文件菜单(File) –> 点击 设置(Settings… Ctrl+Alt+S), –> 打开设置对话框. 在左侧的 ...