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


    m文件

    [cpp] view plain copy
     
     print?
      1. #import "wiDBRoot.h"  
      2.   
      3. @interface wiDBRoot ()  
      4. - (NSString *)getPath:(NSString *)dbName;  
      5. @end  
      6.   
      7. @implementation wiDBRoot  
      8. @synthesize DB;  
      9. @synthesize DBName;  
      10.   
      11. /* 
      12. + (id)modelWithDBName:(NSString *)dbName 
      13.     [[[self alloc] initWithDBName:dbName] autorelease]; 
      14.     return self; 
      15. */  
      16.   
      17. - (id)initWithDBName:(NSString *)dbName  
      18. {  
      19.   
      20.     self = [super init];  
      21.       
      22.     if(nil != self)  
      23.     {  
      24.         DBName = [self getPath:dbName];  
      25.         WILog(@"DBName: %@", DBName);  
      26.     }  
      27.       
      28.     return self;  
      29. }  
      30.   
      31. - (void)dealloc {  
      32.     [DB close];  
      33.     [DB release];  
      34.     [DBName release];  
      35.     [super dealloc];  
      36. }  
      37.   
      38. // 数据库存储路径(内部使用)  
      39. - (NSString *)getPath:(NSString *)dbName  
      40. {  
      41.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
      42.     NSString *documentsDirectory = [paths objectAtIndex:0];  
      43.     return [documentsDirectory stringByAppendingPathComponent:dbName];  
      44. }  
      45.   
      46. // 打开数据库  
      47. - (void)readyDatabse  
      48. {  
      49.     //BOOL success;  
      50.     //NSError *error;  
      51.       
      52.     //NSFileManager *fileManager = [NSFileManager defaultManager];  
      53.     //success = [fileManager fileExistsAtPath:self.DBName];  
      54.       
      55.     if ([DB databaseExists])  
      56.         return;  
      57.       
      58.     //DB = [FMDatabase databaseWithPath:DBName];  
      59.     DB = [[FMDatabase alloc] initWithPath:DBName];  
      60.       
      61.     if (![DB open])  
      62.     {  
      63.         [DB close];  
      64.         NSAssert1(0, @"Failed to open database file with message '%@'.", [DB lastErrorMessage]);  
      65.     }  
      66.   
      67.     // kind of experimentalish.  
      68.     [DB setShouldCacheStatements:YES];  
      69. }  
      70.   
      71. #pragma mark 删除数据库  
      72. // 删除数据库  
      73. - (void)deleteDatabse  
      74. {  
      75.     BOOL success;  
      76.     NSError *error;  
      77.       
      78.     NSFileManager *fileManager = [NSFileManager defaultManager];  
      79.       
      80.     // delete the old db.  
      81.     if ([fileManager fileExistsAtPath:DBName])  
      82.     {  
      83.         [DB close];  
      84.         success = [fileManager removeItemAtPath:DBName error:&error];  
      85.         if (!success) {  
      86.             NSAssert1(0, @"Failed to delete old database file with message '%@'.", [error localizedDescription]);  
      87.         }  
      88.     }      
      89. }  
      90.   
      91. // 判断是否存在表  
      92. - (BOOL) isTableOK:(NSString *)tableName  
      93. {  
      94.     FMResultSet *rs = [DB executeQuery:@"SELECT count(*) as 'count' FROM sqlite_master WHERE type ='table' and name = ?", tableName];  
      95.     while ([rs next])  
      96.     {  
      97.         // just print out what we've got in a number of formats.  
      98.         NSInteger count = [rs intForColumn:@"count"];  
      99.         WILog(@"isTableOK %d", count);  
      100.           
      101.         if (0 == count)  
      102.         {  
      103.             return NO;  
      104.         }  
      105.         else  
      106.         {  
      107.             return YES;  
      108.         }  
      109.     }  
      110.   
      111.     return NO;  
      112. }  
      113.   
      114. // 获得表的数据条数  
      115. - (BOOL) getTableItemCount:(NSString *)tableName  
      116. {  
      117.     NSString *sqlstr = [NSString stringWithFormat:@"SELECT count(*) as 'count' FROM %@", tableName];  
      118.     FMResultSet *rs = [DB executeQuery:sqlstr];  
      119.     while ([rs next])  
      120.     {  
      121.         // just print out what we've got in a number of formats.  
      122.         NSInteger count = [rs intForColumn:@"count"];  
      123.         WILog(@"TableItemCount %d", count);  
      124.           
      125.         return count;  
      126.     }  
      127.       
      128.     return 0;  
      129. }  
      130.   
      131. // 创建表  
      132. - (BOOL) createTable:(NSString *)tableName withArguments:(NSString *)arguments  
      133. {  
      134.     NSString *sqlstr = [NSString stringWithFormat:@"CREATE TABLE %@ (%@)", tableName, arguments];  
      135.     if (![DB executeUpdate:sqlstr])  
      136.     //if ([DB executeUpdate:@"create table user (name text, pass text)"] == nil)  
      137.     {  
      138.         WILog(@"Create db error!");  
      139.         return NO;  
      140.     }  
      141.   
      142.     return YES;  
      143. }  
      144.   
      145. // 删除表  
      146. - (BOOL) deleteTable:(NSString *)tableName  
      147. {  
      148.     NSString *sqlstr = [NSString stringWithFormat:@"DROP TABLE %@", tableName];  
      149.     if (![DB executeUpdate:sqlstr])  
      150.     {  
      151.         WILog(@"Delete table error!");  
      152.         return NO;  
      153.     }  
      154.       
      155.     return YES;  
      156. }  
      157.       
      158. // 清除表  
      159. - (BOOL) eraseTable:(NSString *)tableName  
      160. {  
      161.     NSString *sqlstr = [NSString stringWithFormat:@"DELETE FROM %@", tableName];  
      162.     if (![DB executeUpdate:sqlstr])  
      163.     {  
      164.         WILog(@"Erase table error!");  
      165.         return NO;  
      166.         }  
      167.       
      168.     return YES;  
      169.     }      
      170.   
      171. // 插入数据  
      172. - (BOOL)insertTable:(NSString*)sql, ...  
      173. {  
      174.     va_list args;  
      175.     va_start(args, sql);  
      176.       
      177.     BOOL result = [DB executeUpdate:sql error:nil withArgumentsInArray:nil orVAList:args];  
      178.       
      179.     va_end(args);  
      180.     return result;  
      181. }  
      182.   
      183. // 修改数据  
      184. - (BOOL)updateTable:(NSString*)sql, ...  
      185. {  
      186.     va_list args;  
      187.     va_start(args, sql);  
      188.       
      189.     BOOL result = [DB executeUpdate:sql error:nil withArgumentsInArray:nil orVAList:args];  
      190.       
      191.     va_end(args);  
      192.     return result;  
      193. }  
      194.   
      195. // 暂时无用  
      196. #pragma mark 获得单一数据  
      197.   
      198. // 整型  
      199. - (NSInteger)getDb_Integerdata:(NSString *)tableName withFieldName:(NSString *)fieldName  
      200. {  
      201.     NSInteger result = NO;  
      202.       
      203.     NSString *sql = [NSString stringWithFormat:@"SELECT %@ FROM %@", fieldName, tableName];  
      204.     FMResultSet *rs = [DB executeQuery:sql];  
      205.     if ([rs next])  
      206.         result = [rs intForColumnIndex:0];  
      207.     [rs close];  
      208.       
      209.     return result;  
      210. }  
      211.   
      212. // 布尔型  
      213. - (BOOL)getDb_Booldata:(NSString *)tableName withFieldName:(NSString *)fieldName  
      214. {  
      215.     BOOL result;  
      216.   
      217.     result = [self getDb_Integerdata:tableName withFieldName:fieldName];  
      218.   
      219.     return result;  
      220. }  
      221.   
      222. // 字符串型  
      223. - (NSString *)getDb_Stringdata:(NSString *)tableName withFieldName:(NSString *)fieldName  
      224. {  
      225.     NSString *result = NO;  
      226.       
      227.     NSString *sql = [NSString stringWithFormat:@"SELECT %@ FROM %@", fieldName, tableName];  
      228.     FMResultSet *rs = [DB executeQuery:sql];  
      229.     if ([rs next])  
      230.         result = [rs stringForColumnIndex:0];  
      231.     [rs close];  
      232.   
      233.     return result;  
      234. }  
      235.   
      236. // 二进制数据型  
      237. - (NSData *)getDb_Bolbdata:(NSString *)tableName withFieldName:(NSString *)fieldName  
      238. {  
      239.     NSData *result = NO;  
      240.       
      241.     NSString *sql = [NSString stringWithFormat:@"SELECT %@ FROM %@", fieldName, tableName];  
      242.     FMResultSet *rs = [DB executeQuery:sql];  
      243.     if ([rs next])  
      244.         result = [rs dataForColumnIndex:0];  
      245.     [rs close];  
      246.       
      247.     return result;  
      248. }  
      249.   
      250. @end  
  • 相关阅读:
    第十篇 数据类型总结
    第九篇 字典类型的内置方法
    第二篇 输入与输出以及基础运算符
    <爬虫实战>糗事百科
    <读书笔记>如何入门爬虫?
    <读书笔记>001-以解决问题为导向的python编程实践
    <小白学技术>将python脚本导出为exe可执行程序
    <Django> 第三方扩展
    <Django> 高级(其他知识点)
    <Django> MVT三大块之Template(模板)
  • 原文地址:https://www.cnblogs.com/zxykit/p/6164042.html
Copyright © 2011-2022 走看看