我现在要使用SQLite3.0创建一个数据库,然后在数据库中创建一个表格。

首先要引入SQLite3.0的lib库。然后包含头文件#import <sqlite3.h>

【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】向表格中插入一条记录

假设channle是一个数据结构体,保存了一条记录的内容。
- (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;
}
 
【4】数据库查询
这里获取表格中所有的记录,放到数组fChannels中。
- (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数据库使用的更多相关文章

  1. windows phone 8.1开发SQlite数据库引用安装

    原文出自:http://www.bcmeng.com/windows-phone-sqlite/ windows phone 8.1开发SQlite数据库引用安装 第一步: 安装SQlite forw ...

  2. windows phone 8.1开发SQlite数据库操作详解

    原文出自:http://www.bcmeng.com/windows-phone-sqlite1/ 本文小梦将和大家分享WP8.1中SQlite数据库的基本操作:(最后有整个示例的源码)(希望能通过本 ...

  3. android开发--sqlite数据库

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

  4. WP7开发 Sqlite数据库的使用 解决Unable open the database

    WP7本身不支持Sqlite数据库,但我们可以添加第三方组件让它支持Sqlite. 首先在项目中添加引用Community.CsharpSqlite.WP.dll,我会放后面让大家下载,我下了有几天了 ...

  5. Android开发 ---SQLite数据库,lock文件,结果集游标,适配器,安全退出,给连接设置下划线,编辑器,投影,ContentValues存储,DbHelper,activity栈

    目录截图: 1.activity_main.xml 主界面效果: <?xml version="1.0" encoding="utf-8"?> &l ...

  6. Android开发SQLite数据库的创建

    package com.example.db; import android.content.Context; import android.database.sqlite.SQLiteDatabas ...

  7. Android开发 SQLite数据库应用笔记(一)

    注意: 1.public Cursor rawQuery(String sql, String[] selectionArgs) Cursor游标是查询后返回的结果集合,游标的意思是指向集合中的某行. ...

  8. Qt5 开发 iOS 应用之访问 SQLite 数据库

    开发环境: macOS 10.12.1 Xcode 8.1 Qt 5.8 iPhone 6S+iOS 10.1.1   源代码: 我在 Qt 程序里指定了数据库的名称来创建数据库,在 Win10.An ...

  9. Android开发-之SQLite数据库

    之前我们讲了如何将数据存储在文件中,那么除了这种方式呢,就是我们常见的大家都知道的将数据存储在数据库当中了. 将数据存储在数据库中的优势: 1)存储在数据库中的数据更加方便操作,比如增.删.改.查等 ...

随机推荐

  1. Python框架之Django学习笔记(十二)

    Django站点管理 十一转眼结束,说好的充电没能顺利开展,反而悠闲的看了电视剧以及去影院看了新上映的<心花路放>.<亲爱的>以及<黄金时代>,说好的劳逸结合现在回 ...

  2. Oracle 分析函数--Row_Number()

    row_number() over ([partition by col1] order by col2) ) as 别名 表示根据col1分组,在分组内部根据 col2排序 而这个“别名”的值就表示 ...

  3. Leetcode 611.有效三角形的个数

    有效三角形的个数 给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数. 示例 1: 输入: [2,2,3,4] 输出: 3 解释: 有效的组合是: 2,3,4 (使用第一个 ...

  4. spring IOC注解方式详解

    本文分为三个部分:概述.使用注解进行属性注入.使用注解进行Bean的自动定义. 一,概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以 ...

  5. MFC录制音频和播放音频

    一.录制音频 在windows中提供了相应的API函数(waveIn这个族的函数)实现录音功能:在使用这些函数时,一定要引入相应的头文件 #include <windows.h> #inc ...

  6. 6个超实用的PHP代码片段

    一.黑名单过滤 function is_spam($text, $file, $split = ':', $regex = false){ $handle = fopen($file, 'rb'); ...

  7. eslint规范项目代码

    安装一系列eslint插件后,填写eslint配置,配置如下 .editorconfig root = true [*] charset = utf-8 indent_style = space in ...

  8. htmilunit-- 针对抓取js生成的数据

    public static String  getHtml(String html){        // 模拟一个浏览器          @SuppressWarnings("resou ...

  9. ZOJ 3781 Paint the Grid Reloaded(BFS+缩点思想)

    Paint the Grid Reloaded Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N rows ...

  10. HDU 5687 Problem C(Trie+坑)

    Problem C Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Tota ...