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-网络通信概述 ...
随机推荐
- android上的JAVA8:使用retrolambda
android上的JAVA8:使用retrolambda posted by juuda 2015年6月3日 下午7:33 under Android Java8引入了lambda表达式,让许多开发者 ...
- UVALive 2035 The Monocycle(BFS状态处理+优先队列)
这道题目真是非常坎坷啊,WA了很多次,但所有的思路都是奔着广搜去想的,一开始出现了比答案大的数据,才想到了应该是优先队列,再说加上也肯定不会错.一开始我读错了题意,以为旋转并且前行需要的时间跟其他一样 ...
- Mesos架构
Mesos Architecture 上图显示了 Mesos 的主要组成部分. Mesos 由一个 master daemon 来管理 slave daemon 在每个集群节点上的运行, mesos ...
- andorid 自定义view属性declare-styleable
1. reference:参考某一资源ID. (1)属性定义: <declare-styleable name = "名称"> <attr name = &quo ...
- Windows Batch Scripts
Some simple examples: simple.bat @echo off set _var1=3 set _var2=5 set /a _result=%_var1%+%_var2% ec ...
- Dev之ChartControl控件(一)
ChartControl控件主要包括Chart Title,Legend,Annotations,Diagram,Series五部分:如图: 1. 用RangeControl控件控制ChartCon ...
- IndentationError: unexpected indent python
都知道python是对格式要求很严格的,写了一些python但是也没发现他严格在哪里,今天遇到了IndentationError: unexpected indent错误我才知道他是多么的严格. 以后 ...
- ubuntu 解压 windows 生成的 zip 文件乱码问题
在windows上压缩的文件,是以系统默认编码中文来压缩文件.由于zip文件中没有声明其编码,所以linux上的unzip一般以默认编码解压,中文文件名会出现乱码. 有两种方式解决问题:(建议采用方法 ...
- 零成本实现Android/iOS自动化测试:基于Appium和Test Perfect
https://item.taobao.com/item.htm?spm=a230r.1.14.14.42KJ3L&id=527677900735&ns=1&abbucket= ...
- 教你用CSS代码写出的各种形状图形
做网页设计时经常要用到各种形状的图形,对于规则的图形很简单,但是对于不规则的图形,一般我们都是用图片,今天就在这里教大家怎样用css代码写出各种规则不同的图形 1.正方形 #square {width ...