iphone开发-SQLite数据库使用
我现在要使用SQLite3.0创建一个数据库,然后在数据库中创建一个表格。
【1】打开数据库,如果没有,那么创建一个

sqlite3* database_;
-(BOOL) open{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"mydb.sql"];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL find = [fileManager fileExistsAtPath:path];
//找到数据库文件mydb.sql
if (find) {
NSLog(@"Database file have already existed.");
if(sqlite3_open([path UTF8String], &database_) != SQLITE_OK) {
sqlite3_close(database_);
NSLog(@"Error: open database file.");
return NO;
}
return YES;
}
if(sqlite3_open([path UTF8String], &database_) == SQLITE_OK) {
bFirstCreate_ = YES;
[self createChannelsTable:database_];//在后面实现函数createChannelsTable
return YES;
} else {
sqlite3_close(database_);
NSLog(@"Error: open database file.");
return NO;
}
return NO;
}

【2】创建表格

//创建表格,假设有五个字段,(id,cid,title,imageData ,imageLen )
//说明一下,id为表格的主键,必须有。
//cid,和title都是字符串,imageData是二进制数据,imageLen 是该二进制数据的长度。 - (BOOL) createChannelsTable:(sqlite3*)db
{
char *sql = "CREATE TABLE channels (id integer primary key, \
cid text, \
title text, \
imageData BLOB, \
imageLen integer)";
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(db, sql, -, &statement, nil) != SQLITE_OK) {
NSLog(@"Error: failed to prepare statement:create channels table");
return NO;
}
int success = sqlite3_step(statement);
sqlite3_finalize(statement);
if ( success != SQLITE_DONE) {
NSLog(@"Error: failed to dehydrate:CREATE TABLE channels");
return NO;
}
NSLog(@"Create table 'channels' successed.");
return YES;
}

【3】向表格中插入一条记录

- (BOOL) insertOneChannel:(Channel*)channel
{
NSData* ImageData = UIImagePNGRepresentation( channel.image_);
NSInteger Imagelen = [ImageData length];
sqlite3_stmt *statement;
static char *sql = "INSERT INTO channels (cid,title,imageData,imageLen)\
VALUES(?,?,?,?)";
//问号的个数要和(cid,title,imageData,imageLen)里面字段的个数匹配,代表未知的值,将在下面将值和字段关联。
int success = sqlite3_prepare_v2(database_, sql, -, &statement, NULL);
if (success != SQLITE_OK)
{
NSLog(@"Error: failed to insert:channels");
return NO;
} //这里的数字1,2,3,4代表第几个问号
sqlite3_bind_text(statement, , [channel.id_ UTF8String], -, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, , [channel.title_ UTF8String], -, SQLITE_TRANSIENT);
sqlite3_bind_blob(statement, , [ImageData bytes], Imagelen, SQLITE_TRANSIENT);
sqlite3_bind_int(statement, , Imagelen); success = sqlite3_step(statement);
sqlite3_finalize(statement); if (success == SQLITE_ERROR) {
NSLog(@"Error: failed to insert into the database with message.");
return NO;
} NSLog(@"Insert One Channel#############:id = %@",channel.id_);
return YES;
}


- (void) getChannels:(NSMutableArray*)fChannels
{
sqlite3_stmt *statement = nil;
char *sql = "SELECT * FROM channels";
if (sqlite3_prepare_v2(database_, sql, -, &statement, NULL) != SQLITE_OK)
{
NSLog(@"Error: failed to prepare statement with message:get channels.");
}
//查询结 果集中一条一条的遍历所有的记录,这里的数字对应的是列值。
while (sqlite3_step(statement) == SQLITE_ROW)
{
char* cid = (char*)sqlite3_column_text(statement, );
char* title = (char*)sqlite3_column_text(statement, );
Byte* imageData = (Byte*)sqlite3_column_blob(statement, );
int imageLen = sqlite3_column_int(statement, );
Channel* channel = [[Channel alloc] init];
if(cid)
channel.id_ = [NSString stringWithUTF8String:cid];
if(title)
channel.title_ = [NSString stringWithUTF8String:title];
if(imageData)
{
UIImage* image = [UIImage imageWithData:[NSData dataWithBytes:imageData length:imageLen]];
channel.image_ = image;
}
[fChannels addObject:channel];
[channel release];
}
sqlite3_finalize(statement);
}
iphone开发-SQLite数据库使用的更多相关文章
- windows phone 8.1开发SQlite数据库引用安装
原文出自:http://www.bcmeng.com/windows-phone-sqlite/ windows phone 8.1开发SQlite数据库引用安装 第一步: 安装SQlite forw ...
- windows phone 8.1开发SQlite数据库操作详解
原文出自:http://www.bcmeng.com/windows-phone-sqlite1/ 本文小梦将和大家分享WP8.1中SQlite数据库的基本操作:(最后有整个示例的源码)(希望能通过本 ...
- android开发--sqlite数据库
一.SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级 使用 SQL ...
- WP7开发 Sqlite数据库的使用 解决Unable open the database
WP7本身不支持Sqlite数据库,但我们可以添加第三方组件让它支持Sqlite. 首先在项目中添加引用Community.CsharpSqlite.WP.dll,我会放后面让大家下载,我下了有几天了 ...
- Android开发 ---SQLite数据库,lock文件,结果集游标,适配器,安全退出,给连接设置下划线,编辑器,投影,ContentValues存储,DbHelper,activity栈
目录截图: 1.activity_main.xml 主界面效果: <?xml version="1.0" encoding="utf-8"?> &l ...
- Android开发SQLite数据库的创建
package com.example.db; import android.content.Context; import android.database.sqlite.SQLiteDatabas ...
- Android开发 SQLite数据库应用笔记(一)
注意: 1.public Cursor rawQuery(String sql, String[] selectionArgs) Cursor游标是查询后返回的结果集合,游标的意思是指向集合中的某行. ...
- Qt5 开发 iOS 应用之访问 SQLite 数据库
开发环境: macOS 10.12.1 Xcode 8.1 Qt 5.8 iPhone 6S+iOS 10.1.1 源代码: 我在 Qt 程序里指定了数据库的名称来创建数据库,在 Win10.An ...
- Android开发-之SQLite数据库
之前我们讲了如何将数据存储在文件中,那么除了这种方式呢,就是我们常见的大家都知道的将数据存储在数据库当中了. 将数据存储在数据库中的优势: 1)存储在数据库中的数据更加方便操作,比如增.删.改.查等 ...
随机推荐
- 了解Windows Server以及Hyper-V许可模式
在2015年11月,微软宣布对Windows Server 2016以及Hyper-V的许可模式进行重大变更,并于2016年第三季度正式生效,Windows Server 2016标准版及数据中心版的 ...
- 数据库路由中间件MyCat - 使用篇(1)
此文已由作者张镐薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 基本概念 直接介绍概念太枯燥了,还是拿个和背景篇相似的例子介绍 业务场景:客户完成下单,快递员接受并更新运单 ...
- 设计模式之第8章-策略模式(Java实现)
设计模式之第8章-策略模式(Java实现) “年前大酬宾了啊,现在理发冲500送300,冲1000送500了.鱼哥赶紧充钱啊,理发这事基本一个月一回,挺实惠的啊.不过话说那个理发店的老板好傻啊,冲10 ...
- 50、转自知乎上android开发相见恨晚的接口
原文链接:http://www.zhihu.com/question/33636939 程序员软件开发Android 开发JavaAndroid修改 Android开发中,有哪些让你觉得相 ...
- [netty4][netty-transpot]Channel体系分析
Channel体系分析 接口与类结构体系 -- [I]AttributeMap, ChannelOutboundInvoker, Comparable -- [I]AttributeMap ---- ...
- python - 接口自动化测试 - HttpRequest - 接口测试类封装
# -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: http_request.py @ide: PyCharm ...
- [Python]Pandas简单入门(转)
本篇文章转自 https://colab.research.google.com/notebooks/mlcc/intro_to_pandas.ipynb?hl=zh-cn#scrollTo=zCOn ...
- 【Luogu】P3320寻宝游戏(Splay)
题目链接 其实这题用Set就完事了但我不会Set 智商-=inf 求虚树上所有边权和的两倍. 具体方式就是splay把所有在虚树上的点存一下,(按照DFS序排序的)每次插入/删除会更新前驱和它.后继和 ...
- [SDOI2008][luogu2463] Sandy的卡片 [kmp]
题面 传送门 思路 这道题里面有三个主要问题: 1.由"数值相等"变成了"加上一个整数以后数值相等"(减去等价于加负数) 2.由"最多匹配多少位(从第 ...
- Python之时间:time模块
import time 对于时间,使用最频繁的模块 1.获取当前时间 (1)时间戳 time.time() 时间戳:从1970年1月1日0点开始到现在按秒计算的偏移量 (2)时间元组 time.l ...