IPhone数据库操作代码例子

  1. //database operation  
  2.   
  3. 打开数据库   
  4.   
  5.   
  6. -(BOOL) opendatabase{  
  7.   
  8.   
  9. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  10.   
  11. NSString *documentsDirectory = [paths objectAtIndex:0];  
  12.   
  13. NSString *path = [documentsDirectory stringByAppendingPathComponent:@"mydb.sql"];  
  14.   
  15. NSFileManager *fileManager = [NSFileManager defaultManager];  
  16.   
  17. BOOL find = [fileManager fileExistsAtPath:path];  
  18.   
  19. //找到数据库文件mydb.sql  
  20.   
  21. if (find) {  
  22.   
  23.          NSLog(@"Database file have already existed.");  
  24.   
  25.          if(sqlite3_open([path UTF8String], &database_) != SQLITE_OK) {  
  26.   
  27.          sqlite3_close(database_);  
  28.   
  29.          NSLog(@"Error: open database file.");  
  30.   
  31.          return NO;  
  32.   
  33.         }  
  34.   
  35.          return YES;  
  36.   
  37. }  
  38.   
  39. if(sqlite3_open([path UTF8String], &database_) == SQLITE_OK) {  
  40.   
  41. //bFirstCreate_ = YES;  
  42.   
  43.         [self createChannelsTable:database_];//在后面实现函数createChannelsTable  
  44.   
  45.          return YES;  
  46.   
  47. else {  
  48.   
  49.         sqlite3_close(database_);  
  50.   
  51.         NSLog(@"Error: open database file.");  
  52.   
  53.         return NO;  
  54.   
  55. }  
  56.   
  57. return NO;  
  58.   
  59. }  
  60.   
  61.   
  62. 创建表  
  63.   
  64.   
  65. - (BOOL) createChannelsTable:(sqlite3*)db{  
  66.   
  67. char *sql = "CREATE TABLE reports (id integer primary key,stime  text,stitle text,scal   text,sruntime text)";  
  68.   
  69. sqlite3_stmt *statement;  
  70.   
  71. if(sqlite3_prepare_v2(db, sql, -1, &statement, nil) != SQLITE_OK) {  
  72.   
  73.          NSLog(@"Error: failed to prepare statement:create reports table");  
  74.   
  75.          return NO;  
  76.   
  77. }  
  78.   
  79. int success = sqlite3_step(statement);  
  80.   
  81. sqlite3_finalize(statement);  
  82.   
  83. if ( success != SQLITE_DONE) {  
  84.   
  85.         NSLog(@"Error: failed to dehydrate:CREATE TABLE reports");  
  86.   
  87.         return NO;  
  88.   
  89. }  
  90.   
  91. NSLog(@"Create table 'reports' successed.");  
  92.   
  93. return YES;  
  94.   
  95. }  
  96.   
  97.   
  98. 插入表   
  99.   
  100.   
  101. - (BOOL)insertOneChannel:(NSString*)stime mytitle:(NSString*)stitle mycal:(NSString*)scal myruntime:(NSString*)sruntime  
  102.   
  103. {  
  104.   
  105.   
  106. sqlite3_stmt *statement;  
  107.   
  108. static char *sql = "INSERT INTO reports (id,stime,stitle,scal,sruntime) VALUES(NULL,?,?,?,?)";  
  109.   
  110. //问号的个数要和(cid,title,imageData,imageLen)里面字段的个数匹配,代表未知的值,将在下面将值和字段关联。  
  111.   
  112. int success = sqlite3_prepare_v2(database_, sql, -1, &statement, NULL);  
  113.   
  114. if (success != SQLITE_OK) {  
  115.   
  116.         NSLog(@"Error: failed to insert:channels");  
  117.   
  118.          return NO;  
  119.   
  120. }  
  121.   
  122. //这里的数字1,2,3,4代表第几个问号  
  123.   
  124. //sqlite3_bind_text(statement, 1, stime, -1, SQLITE_TRANSIENT);  
  125.   
  126. char *p = [stime cStringUsingEncoding:1];  
  127.   
  128. sqlite3_bind_text(statement, 1, [stime cStringUsingEncoding:1], -1, SQLITE_TRANSIENT);  
  129.   
  130. sqlite3_bind_text(statement, 2, [stitle cStringUsingEncoding:1], -1, SQLITE_TRANSIENT);  
  131.   
  132. sqlite3_bind_text(statement, 3, [scal cStringUsingEncoding:1], -1, SQLITE_TRANSIENT);  
  133.   
  134. sqlite3_bind_text(statement, 4, [sruntime cStringUsingEncoding:1], -1, SQLITE_TRANSIENT);  
  135.   
  136.   
  137. success = sqlite3_step(statement);  
  138.   
  139. sqlite3_finalize(statement);  
  140.   
  141. if (success == SQLITE_ERROR) {  
  142.   
  143.          NSLog(@"Error: failed to insert into the database with message.");  
  144.   
  145.         return NO;  
  146.   
  147. }  
  148.   
  149. NSLog(@"Insert One Channel#############:id = _");  
  150.   
  151. return YES;  
  152.   
  153. }  
  154.   
  155. 查询表  
  156.   
  157. - (void) getChannels:(NSMutableArray*)fChannels{  
  158.   
  159. sqlite3_stmt *statement = nil;  
  160.   
  161. char *sql = "SELECT * FROM reports";  
  162.   
  163. if (sqlite3_prepare_v2(database_, sql, -1, &statement, NULL) != SQLITE_OK) {  
  164.   
  165.         NSLog(@"Error: failed to prepare statement with message:get channels.");  
  166.   
  167. }  
  168.   
  169. //查询结果集中一条一条的遍历所有的记录,这里的数字对应的是列值。  
  170.   
  171. while (sqlite3_step(statement) == SQLITE_ROW) {  
  172.   
  173.        //char* cid = (char*)sqlite3_column_text(statement, 1);  
  174.   
  175.        char* stime = (char*)sqlite3_column_text(statement, 1);  
  176.   
  177.        char* stitle =(char*)sqlite3_column_text(statement, 2);  
  178.   
  179.        char* scal = (char*)sqlite3_column_text(statement, 3);  
  180.   
  181.        char* sruntime= (char*)sqlite3_column_text(statement, 4);  
  182.   
  183.   
  184.        //NSString *tmp = [NSString stringWithCString:stitle encoding:1];  
  185.   
  186.         myreportitem* ri = [[myreportitem alloc] init];  
  187.   
  188.        ri.mytime = [NSString stringWithCString:stime encoding:1];  
  189.   
  190.         ri.mytitle = [NSString stringWithCString:stitle encoding:1];  
  191.   
  192.         ri.mycal = [NSString stringWithCString:scal encoding:1];  
  193.   
  194.        ri.myruntime = [NSString stringWithCString:sruntime encoding:1];  
  195.   
  196.   
  197.        [fChannels addObject:ri];  
  198.   
  199.        [ri release];  
  200.   
  201. }  
  202.   
  203. sqlite3_finalize(statement);  
  204.   
  205. }  
  206.   
  207. 删除记录  
  208.   
  209. - (void)doClearReport: {  
  210.   
  211.   
  212. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  213.   
  214. NSString *documentsDirectory = [paths objectAtIndex:0];  
  215.   
  216. NSString *path = [documentsDirectory stringByAppendingPathComponent:@"mydb.sql"];  
  217.   
  218. NSFileManager *fileManager = [NSFileManager defaultManager];  
  219.   
  220. BOOL find = [fileManager fileExistsAtPath:path];  
  221.   
  222. //找到数据库文件mydb.sql  
  223.   
  224. if (find) {  
  225.   
  226.         NSLog(@"Database file have already existed.");  
  227.   
  228.          if(sqlite3_open([path UTF8String], &database_) != SQLITE_OK) {  
  229.   
  230.         sqlite3_close(database_);  
  231.   
  232.         NSLog(@"Error: open database file.");  
  233.   
  234.         return NO;  
  235.   
  236.       }  
  237.   
  238.       char *sql = "delete from reports";  
  239.   
  240.        sqlite3_stmt *statement;  
  241.   
  242.        if(sqlite3_prepare_v2(database_, sql, -1, &statement, nil) != SQLITE_OK) {  
  243.   
  244.         NSLog(@"Error: failed to prepare statement:create reports table");  
  245.   
  246.        return NO;  
  247.   
  248.      }  
  249.   
  250.      int success = sqlite3_step(statement);  
  251.   
  252.       sqlite3_finalize(statement);  
  253.   
  254.      if ( success != SQLITE_DONE) {  
  255.   
  256.       NSLog(@"Error: failed to dehydrate:delete TABLE reports");  
  257.   
  258.       return NO;  
  259.   
  260.       }  
  261.   
  262.       NSLog(@"Create table 'reports' successed.");  
  263.   
  264.   
  265.      sqlite3_close(database_);  
  266.   
  267.   
  268.    }  
  269.   
  270.   
  271. }  

你可能感兴趣的:(IOS开发)