FMDB的一些基本操作小结
http://blog.csdn.net/iunion/article/details/7204625
仅供自己记录使用,
h文件
- #import <Foundation/Foundation.h>
- #import "FMDatabase.h"
- #import "FMDatabaseAdditions.h"
- @interface wiDBRoot : NSObject
- @property (retain, nonatomic) FMDatabase *DB;
- @property (retain, nonatomic) NSString *DBName;
- //+ (id)modelWithDBName:(NSString *)dbName;
- - (id)initWithDBName:(NSString *)dbName;
- // 删除数据库
- - (void)deleteDatabse;
- // 数据库存储路径
- //- (NSString *)getPath:(NSString *)dbName;
- // 打开数据库
- - (void)readyDatabse;
- // 判断是否存在表
- - (BOOL) isTableOK:(NSString *)tableName;
- // 获得表的数据条数
- - (BOOL) getTableItemCount:(NSString *)tableName;
- // 创建表
- - (BOOL) createTable:(NSString *)tableName withArguments:(NSString *)arguments;
- // 删除表-彻底删除表
- - (BOOL) deleteTable:(NSString *)tableName;
- // 清除表-清数据
- - (BOOL) eraseTable:(NSString *)tableName;
- // 插入数据
- - (BOOL)insertTable:(NSString*)sql, ...;
- // 修改数据
- - (BOOL)updateTable:(NSString*)sql, ...;
- // 整型
- - (NSInteger)getDb_Integerdata:(NSString *)tableName withFieldName:(NSString *)fieldName;
- // 布尔型
- - (BOOL)getDb_Booldata:(NSString *)tableName withFieldName:(NSString *)fieldName;
- // 字符串型
- - (NSString *)getDb_Stringdata:(NSString *)tableName withFieldName:(NSString *)fieldName;
- // 二进制数据型
- - (NSData *)getDb_Bolbdata:(NSString *)tableName withFieldName:(NSString *)fieldName;
- @end
m文件
- #import "wiDBRoot.h"
- @interface wiDBRoot ()
- - (NSString *)getPath:(NSString *)dbName;
- @end
- @implementation wiDBRoot
- @synthesize DB;
- @synthesize DBName;
- /*
- + (id)modelWithDBName:(NSString *)dbName
- {
- [[[self alloc] initWithDBName:dbName] autorelease];
- return self;
- }
- */
- - (id)initWithDBName:(NSString *)dbName
- {
- self = [super init];
- if(nil != self)
- {
- DBName = [self getPath:dbName];
- WILog(@"DBName: %@", DBName);
- }
- return self;
- }
- - (void)dealloc {
- [DB close];
- [DB release];
- [DBName release];
- [super dealloc];
- }
- // 数据库存储路径(内部使用)
- - (NSString *)getPath:(NSString *)dbName
- {
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- return [documentsDirectory stringByAppendingPathComponent:dbName];
- }
- // 打开数据库
- - (void)readyDatabse
- {
- //BOOL success;
- //NSError *error;
- //NSFileManager *fileManager = [NSFileManager defaultManager];
- //success = [fileManager fileExistsAtPath:self.DBName];
- if ([DB databaseExists])
- return;
- //DB = [FMDatabase databaseWithPath:DBName];
- DB = [[FMDatabase alloc] initWithPath:DBName];
- if (![DB open])
- {
- [DB close];
- NSAssert1(0, @"Failed to open database file with message '%@'.", [DB lastErrorMessage]);
- }
- // kind of experimentalish.
- [DB setShouldCacheStatements:YES];
- }
- #pragma mark 删除数据库
- // 删除数据库
- - (void)deleteDatabse
- {
- BOOL success;
- NSError *error;
- NSFileManager *fileManager = [NSFileManager defaultManager];
- // delete the old db.
- if ([fileManager fileExistsAtPath:DBName])
- {
- [DB close];
- success = [fileManager removeItemAtPath:DBName error:&error];
- if (!success) {
- NSAssert1(0, @"Failed to delete old database file with message '%@'.", [error localizedDescription]);
- }
- }
- }
- // 判断是否存在表
- - (BOOL) isTableOK:(NSString *)tableName
- {
- FMResultSet *rs = [DB executeQuery:@"SELECT count(*) as 'count' FROM sqlite_master WHERE type ='table' and name = ?", tableName];
- while ([rs next])
- {
- // just print out what we've got in a number of formats.
- NSInteger count = [rs intForColumn:@"count"];
- WILog(@"isTableOK %d", count);
- if (0 == count)
- {
- return NO;
- }
- else
- {
- return YES;
- }
- }
- return NO;
- }
- // 获得表的数据条数
- - (BOOL) getTableItemCount:(NSString *)tableName
- {
- NSString *sqlstr = [NSString stringWithFormat:@"SELECT count(*) as 'count' FROM %@", tableName];
- FMResultSet *rs = [DB executeQuery:sqlstr];
- while ([rs next])
- {
- // just print out what we've got in a number of formats.
- NSInteger count = [rs intForColumn:@"count"];
- WILog(@"TableItemCount %d", count);
- return count;
- }
- return 0;
- }
- // 创建表
- - (BOOL) createTable:(NSString *)tableName withArguments:(NSString *)arguments
- {
- NSString *sqlstr = [NSString stringWithFormat:@"CREATE TABLE %@ (%@)", tableName, arguments];
- if (![DB executeUpdate:sqlstr])
- //if ([DB executeUpdate:@"create table user (name text, pass text)"] == nil)
- {
- WILog(@"Create db error!");
- return NO;
- }
- return YES;
- }
- // 删除表
- - (BOOL) deleteTable:(NSString *)tableName
- {
- NSString *sqlstr = [NSString stringWithFormat:@"DROP TABLE %@", tableName];
- if (![DB executeUpdate:sqlstr])
- {
- WILog(@"Delete table error!");
- return NO;
- }
- return YES;
- }
- // 清除表
- - (BOOL) eraseTable:(NSString *)tableName
- {
- NSString *sqlstr = [NSString stringWithFormat:@"DELETE FROM %@", tableName];
- if (![DB executeUpdate:sqlstr])
- {
- WILog(@"Erase table error!");
- return NO;
- }
- return YES;
- }
- // 插入数据
- - (BOOL)insertTable:(NSString*)sql, ...
- {
- va_list args;
- va_start(args, sql);
- BOOL result = [DB executeUpdate:sql error:nil withArgumentsInArray:nil orVAList:args];
- va_end(args);
- return result;
- }
- // 修改数据
- - (BOOL)updateTable:(NSString*)sql, ...
- {
- va_list args;
- va_start(args, sql);
- BOOL result = [DB executeUpdate:sql error:nil withArgumentsInArray:nil orVAList:args];
- va_end(args);
- return result;
- }
- // 暂时无用
- #pragma mark 获得单一数据
- // 整型
- - (NSInteger)getDb_Integerdata:(NSString *)tableName withFieldName:(NSString *)fieldName
- {
- NSInteger result = NO;
- NSString *sql = [NSString stringWithFormat:@"SELECT %@ FROM %@", fieldName, tableName];
- FMResultSet *rs = [DB executeQuery:sql];
- if ([rs next])
- result = [rs intForColumnIndex:0];
- [rs close];
- return result;
- }
- // 布尔型
- - (BOOL)getDb_Booldata:(NSString *)tableName withFieldName:(NSString *)fieldName
- {
- BOOL result;
- result = [self getDb_Integerdata:tableName withFieldName:fieldName];
- return result;
- }
- // 字符串型
- - (NSString *)getDb_Stringdata:(NSString *)tableName withFieldName:(NSString *)fieldName
- {
- NSString *result = NO;
- NSString *sql = [NSString stringWithFormat:@"SELECT %@ FROM %@", fieldName, tableName];
- FMResultSet *rs = [DB executeQuery:sql];
- if ([rs next])
- result = [rs stringForColumnIndex:0];
- [rs close];
- return result;
- }
- // 二进制数据型
- - (NSData *)getDb_Bolbdata:(NSString *)tableName withFieldName:(NSString *)fieldName
- {
- NSData *result = NO;
- NSString *sql = [NSString stringWithFormat:@"SELECT %@ FROM %@", fieldName, tableName];
- FMResultSet *rs = [DB executeQuery:sql];
- if ([rs next])
- result = [rs dataForColumnIndex:0];
- [rs close];
- return result;
- }
- @end
FMDB的一些基本操作小结的更多相关文章
- shell 基本操作小结
1.echo和if else fi命令 #!/bin/bash echo hello;echo there filename=demo.sh if [ -e "$filename" ...
- fmdb 数据库的基本操作
/** * 创建表 */ - (void)createTable { //1.初始化数据库对象 并且 2.打开数据库 BOOL isOpenSuccess = [self.database open ...
- [转载] Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
创建列表 sample_list = ['a',1,('a','b')] Python 列表操作 sample_list = ['a','b',0,1,3] 得到列表中的某一个值 value_star ...
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
创建列表 sample_list = ['a',1,('a','b')] Python 列表操作 sample_list = ['a','b',0,1,3] 得到列表中的某一个值 value_star ...
- java 对类型的基本操作小结
1.json 字符串转换成对象 SyncCarriageStatusDTO dto= JSON.parseObject(value,SyncCarriageStatusDTO.class); List ...
- 转:Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
转自:http://blog.csdn.net/business122/article/details/7536991 创建列表 sample_list = ['a',1,('a','b')] Pyt ...
- 一行代码实现FMDB的CURD操作
上次实现FMDB的CURD基本操作后,用在项目里,每个实体类都要写SQL语句来实现创建表和CURD操作,总觉得太麻烦,然后就想着利用反射和kvc来实现一个数据库操作的基类继承一下,子类只需要继承,然后 ...
- Verdi 看波形常用快捷操作
Verdi看波形的基本操作小结: 快捷键:(大写字母=Shift+小写) g get, signlas添加信号,显示波形n next, Search Forward选定信号按指定的值(上升 ...
- python就业班-淘宝-目录.txt
卷 TOSHIBA EXT 的文件夹 PATH 列表卷序列号为 AE86-8E8DF:.│ python就业班-淘宝-目录.txt│ ├─01 网络编程│ ├─01-基本概念│ │ 01-网络通信概述 ...
随机推荐
- robot framework -记录错误
1.注意ie浏览器代理设置,报奇怪的错误 2.注意浏览器的安全设置,保护模式全部不要勾选
- MonkeyRunner 实现自动点击截屏后与本地图库进行对比输出
先说下本人是菜鸟,通过网上资料学习,终于调通了MonkeyRunner 实现自动点击截屏后与本地图库进行对比输出,以后做静态UI测试就不需要眼睛盯着看图了,这一切交给MonkeyRunner了. 首先 ...
- thinkPHP16---伪静态
url伪静态通常是为了 满足更好的SEO效果,thinkPHP支持伪静态url设置,可以通过设置URL_HTML_SUFFIX的参数 随意在URL的最后添加你想要的静态后缀,而不会影响当前操作的正常执 ...
- Mybatis学习(5)高级映射
需求: 一.一对一查询 查询订单信息,关联查询创建订单的用户信息: orders--->user:一个订单只由一个用户创建,一对一 orders表 和 user表: 1)使用resultType ...
- LISTVIEW嵌套GRIDVIEW的一些处理(点击GRIDVIEW的条目,能够显示他在LISTVIEW中的位置)(对这篇文章的优化处理,不每次都new onItemClickListener)
前几天写了点击GRIDVIEW的条目,能够显示他在LISTVIEW中的位置,当时的处理是在ListView的适配器里的GetView方法里每次都new GridView的onItemClickList ...
- java-两个jre目录和三个lib目录-转
lib目录下放置着jar包.程序中的import语句找的就是这些文件!例如:import javax.servlet.RequestDispatcher; 问题在于,在cmd模式下编译,系统会提示:C ...
- 条形图(diagrams)
条形图(diagrams) 题目描述 小 虎刚上了幼儿园,老师让他做一个家庭作业:首先画3行格子,第一行有3个格子,第二行有2个格子,第三行有3个格子.每行的格子从左到右可以放棋子,但要 求除第一行外 ...
- drawRect & layoutSubviews 调用时间
首先两个方法都是异步执行.layoutSubviews方便数据计算,drawRect方便视图重绘. layoutSubviews在以下情况下会被调用: 1.init初始化不会触发layoutSub ...
- Java 六种异常处理的陋习(转)
Java 六种异常处理的陋习 原文链接: http://www.cnblogs.com/Android-and-android/archive/2013/05/02/3054469.html 你觉得 ...
- HDU 1887 Weird Numbers(负数的进制转化)
题目要求有两种情况,第一种from情况,正常输出即可,很简单.第二种是to情况,给一个数字,输出负进制R的原码,这个有点小麻烦...解决方法如下; 首先,把这个数n按正常方式展开,形式如下: .... ...