FMDB的一些基本操作小结

http://blog.csdn.net/iunion/article/details/7204625

仅供自己记录使用,

h文件

[cpp] view plain copy

print?

  1. #import <Foundation/Foundation.h>
  2. #import "FMDatabase.h"
  3. #import "FMDatabaseAdditions.h"
  4. @interface wiDBRoot : NSObject
  5. @property (retain, nonatomic) FMDatabase *DB;
  6. @property (retain, nonatomic) NSString *DBName;
  7. //+ (id)modelWithDBName:(NSString *)dbName;
  8. - (id)initWithDBName:(NSString *)dbName;
  9. // 删除数据库
  10. - (void)deleteDatabse;
  11. // 数据库存储路径
  12. //- (NSString *)getPath:(NSString *)dbName;
  13. // 打开数据库
  14. - (void)readyDatabse;
  15. // 判断是否存在表
  16. - (BOOL) isTableOK:(NSString *)tableName;
  17. // 获得表的数据条数
  18. - (BOOL) getTableItemCount:(NSString *)tableName;
  19. // 创建表
  20. - (BOOL) createTable:(NSString *)tableName withArguments:(NSString *)arguments;
  21. // 删除表-彻底删除表
  22. - (BOOL) deleteTable:(NSString *)tableName;
  23. // 清除表-清数据
  24. - (BOOL) eraseTable:(NSString *)tableName;
  25. // 插入数据
  26. - (BOOL)insertTable:(NSString*)sql, ...;
  27. // 修改数据
  28. - (BOOL)updateTable:(NSString*)sql, ...;
  29. // 整型
  30. - (NSInteger)getDb_Integerdata:(NSString *)tableName withFieldName:(NSString *)fieldName;
  31. // 布尔型
  32. - (BOOL)getDb_Booldata:(NSString *)tableName withFieldName:(NSString *)fieldName;
  33. // 字符串型
  34. - (NSString *)getDb_Stringdata:(NSString *)tableName withFieldName:(NSString *)fieldName;
  35. // 二进制数据型
  36. - (NSData *)getDb_Bolbdata:(NSString *)tableName withFieldName:(NSString *)fieldName;
  37. @end

m文件

[cpp] view plain copy

print?

    1. #import "wiDBRoot.h"
    2. @interface wiDBRoot ()
    3. - (NSString *)getPath:(NSString *)dbName;
    4. @end
    5. @implementation wiDBRoot
    6. @synthesize DB;
    7. @synthesize DBName;
    8. /*
    9. + (id)modelWithDBName:(NSString *)dbName
    10. {
    11. [[[self alloc] initWithDBName:dbName] autorelease];
    12. return self;
    13. }
    14. */
    15. - (id)initWithDBName:(NSString *)dbName
    16. {
    17. self = [super init];
    18. if(nil != self)
    19. {
    20. DBName = [self getPath:dbName];
    21. WILog(@"DBName: %@", DBName);
    22. }
    23. return self;
    24. }
    25. - (void)dealloc {
    26. [DB close];
    27. [DB release];
    28. [DBName release];
    29. [super dealloc];
    30. }
    31. // 数据库存储路径(内部使用)
    32. - (NSString *)getPath:(NSString *)dbName
    33. {
    34. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    35. NSString *documentsDirectory = [paths objectAtIndex:0];
    36. return [documentsDirectory stringByAppendingPathComponent:dbName];
    37. }
    38. // 打开数据库
    39. - (void)readyDatabse
    40. {
    41. //BOOL success;
    42. //NSError *error;
    43. //NSFileManager *fileManager = [NSFileManager defaultManager];
    44. //success = [fileManager fileExistsAtPath:self.DBName];
    45. if ([DB databaseExists])
    46. return;
    47. //DB = [FMDatabase databaseWithPath:DBName];
    48. DB = [[FMDatabase alloc] initWithPath:DBName];
    49. if (![DB open])
    50. {
    51. [DB close];
    52. NSAssert1(0, @"Failed to open database file with message ‘%@‘.", [DB lastErrorMessage]);
    53. }
    54. // kind of experimentalish.
    55. [DB setShouldCacheStatements:YES];
    56. }
    57. #pragma mark 删除数据库
    58. // 删除数据库
    59. - (void)deleteDatabse
    60. {
    61. BOOL success;
    62. NSError *error;
    63. NSFileManager *fileManager = [NSFileManager defaultManager];
    64. // delete the old db.
    65. if ([fileManager fileExistsAtPath:DBName])
    66. {
    67. [DB close];
    68. success = [fileManager removeItemAtPath:DBName error:&error];
    69. if (!success) {
    70. NSAssert1(0, @"Failed to delete old database file with message ‘%@‘.", [error localizedDescription]);
    71. }
    72. }
    73. }
    74. // 判断是否存在表
    75. - (BOOL) isTableOK:(NSString *)tableName
    76. {
    77. FMResultSet *rs = [DB executeQuery:@"SELECT count(*) as ‘count‘ FROM sqlite_master WHERE type =‘table‘ and name = ?", tableName];
    78. while ([rs next])
    79. {
    80. // just print out what we‘ve got in a number of formats.
    81. NSInteger count = [rs intForColumn:@"count"];
    82. WILog(@"isTableOK %d", count);
    83. if (0 == count)
    84. {
    85. return NO;
    86. }
    87. else
    88. {
    89. return YES;
    90. }
    91. }
    92. return NO;
    93. }
    94. // 获得表的数据条数
    95. - (BOOL) getTableItemCount:(NSString *)tableName
    96. {
    97. NSString *sqlstr = [NSString stringWithFormat:@"SELECT count(*) as ‘count‘ FROM %@", tableName];
    98. FMResultSet *rs = [DB executeQuery:sqlstr];
    99. while ([rs next])
    100. {
    101. // just print out what we‘ve got in a number of formats.
    102. NSInteger count = [rs intForColumn:@"count"];
    103. WILog(@"TableItemCount %d", count);
    104. return count;
    105. }
    106. return 0;
    107. }
    108. // 创建表
    109. - (BOOL) createTable:(NSString *)tableName withArguments:(NSString *)arguments
    110. {
    111. NSString *sqlstr = [NSString stringWithFormat:@"CREATE TABLE %@ (%@)", tableName, arguments];
    112. if (![DB executeUpdate:sqlstr])
    113. //if ([DB executeUpdate:@"create table user (name text, pass text)"] == nil)
    114. {
    115. WILog(@"Create db error!");
    116. return NO;
    117. }
    118. return YES;
    119. }
    120. // 删除表
    121. - (BOOL) deleteTable:(NSString *)tableName
    122. {
    123. NSString *sqlstr = [NSString stringWithFormat:@"DROP TABLE %@", tableName];
    124. if (![DB executeUpdate:sqlstr])
    125. {
    126. WILog(@"Delete table error!");
    127. return NO;
    128. }
    129. return YES;
    130. }
    131. // 清除表
    132. - (BOOL) eraseTable:(NSString *)tableName
    133. {
    134. NSString *sqlstr = [NSString stringWithFormat:@"DELETE FROM %@", tableName];
    135. if (![DB executeUpdate:sqlstr])
    136. {
    137. WILog(@"Erase table error!");
    138. return NO;
    139. }
    140. return YES;
    141. }
    142. // 插入数据
    143. - (BOOL)insertTable:(NSString*)sql, ...
    144. {
    145. va_list args;
    146. va_start(args, sql);
    147. BOOL result = [DB executeUpdate:sql error:nil withArgumentsInArray:nil orVAList:args];
    148. va_end(args);
    149. return result;
    150. }
    151. // 修改数据
    152. - (BOOL)updateTable:(NSString*)sql, ...
    153. {
    154. va_list args;
    155. va_start(args, sql);
    156. BOOL result = [DB executeUpdate:sql error:nil withArgumentsInArray:nil orVAList:args];
    157. va_end(args);
    158. return result;
    159. }
    160. // 暂时无用
    161. #pragma mark 获得单一数据
    162. // 整型
    163. - (NSInteger)getDb_Integerdata:(NSString *)tableName withFieldName:(NSString *)fieldName
    164. {
    165. NSInteger result = NO;
    166. NSString *sql = [NSString stringWithFormat:@"SELECT %@ FROM %@", fieldName, tableName];
    167. FMResultSet *rs = [DB executeQuery:sql];
    168. if ([rs next])
    169. result = [rs intForColumnIndex:0];
    170. [rs close];
    171. return result;
    172. }
    173. // 布尔型
    174. - (BOOL)getDb_Booldata:(NSString *)tableName withFieldName:(NSString *)fieldName
    175. {
    176. BOOL result;
    177. result = [self getDb_Integerdata:tableName withFieldName:fieldName];
    178. return result;
    179. }
    180. // 字符串型
    181. - (NSString *)getDb_Stringdata:(NSString *)tableName withFieldName:(NSString *)fieldName
    182. {
    183. NSString *result = NO;
    184. NSString *sql = [NSString stringWithFormat:@"SELECT %@ FROM %@", fieldName, tableName];
    185. FMResultSet *rs = [DB executeQuery:sql];
    186. if ([rs next])
    187. result = [rs stringForColumnIndex:0];
    188. [rs close];
    189. return result;
    190. }
    191. // 二进制数据型
    192. - (NSData *)getDb_Bolbdata:(NSString *)tableName withFieldName:(NSString *)fieldName
    193. {
    194. NSData *result = NO;
    195. NSString *sql = [NSString stringWithFormat:@"SELECT %@ FROM %@", fieldName, tableName];
    196. FMResultSet *rs = [DB executeQuery:sql];
    197. if ([rs next])
    198. result = [rs dataForColumnIndex:0];
    199. [rs close];
    200. return result;
    201. }
    202. @end
时间: 2024-07-30 06:51:28

FMDB的一些基本操作小结的相关文章

mongodb的基本操作-小结

mongodb的基本操作-小结目录1.启动2.访问3.数据库命令4.集合命令(对于表的命令)5.数据的增删改==================================================================正文1.启动服务端(1)Linuxservice mongod start (2)windowsmongod --port 27017 --dbpath d:/mongodb_data --auth 2.访问客户端(1)windows.Linuxmongo -

shell 基本操作小结

1.echo和if else fi命令 #!/bin/bash echo hello;echo there filename=demo.sh if [ -e "$filename" ]; then echo "$filename already exists!";cp $filename $filename.bak else echo "$filename does not exist!"; fi; echo "File test co

fmdb 数据库的基本操作

/** *  创建表 */ - (void)createTable { //1.初始化数据库对象 并且 2.打开数据库 BOOL isOpenSuccess = [self.database open]; NSLog(@"数据库打开%@",[email protected]"成功":@"失败"); //3.创建表 NSString *sql = @"create table if not exists User (id integer

[转载] Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结

创建列表 sample_list = ['a',1,('a','b')] Python 列表操作 sample_list = ['a','b',0,1,3] 得到列表中的某一个值 value_start = sample_list[0] end_value = sample_list[-1] 删除列表的第一个值 del sample_list[0] 在列表中插入一个值 sample_list[0:0] = ['sample value'] 得到列表的长度 list_length = len(sa

Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结

[python] view plaincopy 创建列表 sample_list = ['a',1,('a','b')] Python 列表操作 sample_list = ['a','b',0,1,3] 得到列表中的某一个值 value_start = sample_list[0] end_value = sample_list[-1] 删除列表的第一个值 del sample_list[0] 在列表中插入一个值 sample_list[0:0] = ['sample value'] 得到列表

Linux下mysql基本操作小结

shell> mysql -uroot -p       //以root用户连接mysql,默认密码为空 注意:mysql的大部分命令都需以";"(分号)结束:对大小写不敏感! mysql> show databases;    //显示数据库列表mysql> use 数据库名          //选中指定的数据库mysql> show tables;           //显示选中数据库中的数据表列表mysql> select * from 表名; 

Masonry 布局 基本操作 小结

呐,Masonry 是用来布局的!CocoaPods上下载! 呐,来吧!开始鸟语......下面是从View+MASAdditions.h中抽出来的方法. 1 /** 2 * Creates a MASConstraintMaker with the callee view. 3 * Any constraints defined are added to the view or the appropriate superview once the block has finished exec

一行代码实现FMDB的CURD操作

上次实现FMDB的CURD基本操作后,用在项目里,每个实体类都要写SQL语句来实现创建表和CURD操作,总觉得太麻烦,然后就想着利用反射和kvc来实现一个数据库操作的基类继承一下,子类只需要继承,然后添加自己的属性就好,这里做一个总结. 第一个难点:获取子类的所有属性以及类型 OC中有提供获取所有property的方法,需要用到objc_property_t和class_copyPropertyList. objc_property_t *properties =class_copyProper

Linux下python玩转MySQLdb

(0)目录 VMware 下安装Ubuntu的吐血经历 零基础学习Shell编程 Linux下的makefile的妙用 Linux调试神器 -- gdb 十分钟学会Python的基本类型 分布式版本管理神器--GIT GIT文件的三种状态 & Git SSH秘钥问题 十分钟学习Python的进阶语法 配置SSH无密码访问及Linux热键.重启.kill进程 Java的不定长参数和Python的不定长参数对比 Linux下python玩转MySQLdb 一:起因 (1)Linux下安装python