iOS: 學習筆記, 使用FMDatabase操作sqlite3

來源:互聯網
上載者:User

標籤:c   style   class   blog   code   java   

使用FMDatabase操作sqlite3資料庫非常簡單和方便

 1 // 2 //  main.m 3 //  iOSDemo0602_sqlite3 4 // 5 //  Created by yao_yu on 14-6-2. 6 //  Copyright (c) 2014年 yao_yu. All rights reserved. 7 // 8  9 #import <UIKit/UIKit.h>10 #import "FMDatabase.h"11 12 void test01();13 BOOL isfile(NSString *path);14 BOOL isdir(NSString *path);15 16 int main(int argc, char * argv[])17 {18     @autoreleasepool {19         test01();20     }21     return 0;22 }23 24 void test01()25 {26     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);27     NSString *docpath = [paths objectAtIndex:0];28     NSString *dbpath = [docpath stringByAppendingPathComponent:@"YY/Data2"];29 //    NSLog(@"%@", dbpath);30     if (!isdir(dbpath)) {31         NSFileManager *fileManager = [NSFileManager defaultManager];32         [fileManager createDirectoryAtPath:dbpath withIntermediateDirectories:YES attributes:nil error:nil];33     }34     35     NSString *dbfile = [dbpath stringByAppendingPathComponent:@"data.db"];36     NSLog(@"%@", dbfile);37     38     //初始化39     FMDatabase *db = [FMDatabase databaseWithPath:dbfile];40     //開啟資料庫41     [db open];42     43     //刪除表44     [db executeUpdate:@"drop table persons"];45     46     //添加表47     [db executeUpdate:@"create table persons(id, name)"];48     49     //刪除原有資料50     [db executeUpdate:@"delete from persons"];51     52     //使用事務添加資料53     [db beginTransaction];54     for(int i=0; i< 10000; i++)55     {56         [db executeUpdate:@"insert into persons values(?,?)", [NSString stringWithFormat:@"%d", i], [NSString stringWithFormat:@"第%d", i]];57     }58     [db commit];59     60     //資料查詢61     FMResultSet *cursor = [db executeQuery:@"select * from persons"];62     int nCols = cursor.columnCount;63     while ([cursor next]) {64         for (int i=0; i<nCols; i++) {65             printf("%s ", [[cursor stringForColumnIndex:i] UTF8String]);66         }67         printf("\n");68     }69     cursor = nil;70     71     //關閉資料庫72     [db close];73     74     //刪除資料庫測試檔案75     [[NSFileManager defaultManager] removeItemAtPath:dbfile error:nil];76     77 }78 79 BOOL isfile(NSString *path)80 {81     NSFileManager *fileManager = [NSFileManager defaultManager];82     BOOL isdir = NO;83     if ([fileManager fileExistsAtPath:path isDirectory: &isdir]) {84         return isdir == NO;85     }86     return NO;87 }88 89 BOOL isdir(NSString *path)90 {91     NSFileManager *fileManager = [NSFileManager defaultManager];92     BOOL isdir = NO;93     if ([fileManager fileExistsAtPath:path isDirectory: &isdir]) {94         return isdir == YES;95     }96     return NO;97 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.