FMDB的簡單使用,FMDB簡單使用

來源:互聯網
上載者:User

FMDB的簡單使用,FMDB簡單使用
FMDB常用類:

FMDatabase : 一個單一的SQLite資料庫,用於執行SQL語句。
FMResultSet :執行查詢一個FMDatabase結果集,這個和android的Cursor類似。
FMDatabaseQueue :在多個線程來執行查詢和更新時會使用這個類。

建立資料庫:

db = [FMDatabase databaseWithPath:database_path];  

1、當資料庫檔案不存在時,fmdb會自己建立一個。

2、 如果你傳入的參數是空串:@"" ,則fmdb會在臨時檔案目錄下建立這個資料庫,資料庫中斷連線時,資料庫檔案被刪除。

3、如果你傳入的參數是 NULL,則它會建立一個在記憶體中的資料庫,資料庫中斷連線時,資料庫檔案被刪除。

 

開啟資料庫:

[db open]  

返回BOOL型。

關閉資料庫:

[db close] 

建立表:

if ([db open]) 

{  

   NSString *sqlCreateTable =  [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' INTEGER   PRIMARY KEY AUTOINCREMENT, '%@' TEXT, '%@' INTEGER, '%@' TEXT)",TABLENAME,ID,NAME,AGE,ADDRESS];    BOOL res = [db executeUpdate:sqlCreateTable];  

  if (!res) {  

      NSLog(@"error when creating db table");  

    } else {  

       NSLog(@"success to creating db table");  

        }  

        [db close];  

}  

 

添加資料: 

if ([db open]){

  NSString *insertSql1= [NSString stringWithFormat:  

                              @"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')",  

                              TABLENAME, NAME, AGE, ADDRESS, @"張三", @"13", @"濟南"];  

  BOOL res = [db executeUpdate:insertSql1]; 

  if (!res) {  

           NSLog(@"error when insert db table");  

       } else {  

           NSLog(@"success to insert db table");  

       }  

       [db close];  

}

修改資料:

if ([db open]){

NSString *updateSql = [NSString stringWithFormat:  

                               @"UPDATE '%@' SET '%@' = '%@' WHERE '%@' = '%@'",  

                               TABLENAME,   AGE,  @"15" ,AGE,  @"13"];  

BOOL res = [db executeUpdate:updateSql];  

 if (!res) {  

            NSLog(@"error when update db table");  

        } else {  

            NSLog(@"success to update db table");  

        }  

        [db close];  

}

刪除資料:

if ([db open]){

 NSString *deleteSql = [NSString stringWithFormat:  

                               @"delete from %@ where %@ = '%@'",  

                               TABLENAME, NAME, @"張三"];  

BOOL res = [db executeUpdate:deleteSql];  

if (!res) {  

            NSLog(@"error when delete db table");  

        } else {  

            NSLog(@"success to delete db table");  

        } 

        [db close];  

}

 

資料庫查詢操作:

查詢操作使用了executeQuery,並涉及到FMResultSet。

if ([db open]){

 NSString * sql = [NSString stringWithFormat:  

                          @"SELECT * FROM %@",TABLENAME];  

        FMResultSet * rs = [db executeQuery:sql];  

while ([rs next]) {  

    int Id = [rs intForColumn:ID];  

            NSString * name = [rs stringForColumn:NAME];  

            NSString * age = [rs stringForColumn:AGE];  

            NSString * address = [rs stringForColumn:ADDRESS];  

            NSLog(@"id = %d, name = %@, age = %@  address = %@", Id, name, age, address);  

        }  

        [db close];  

}

 

FMDB的FMResultSet提供了多個方法來擷取不同類型的資料:

資料庫多線程操作:

如果應用中使用了多線程操作資料庫,那麼就需要使用FMDatabaseQueue來保證安全執行緒了。 應用中不可在多個線程中共同使用一個FMDatabase對象操作資料庫,這樣會引起資料庫資料混亂。 為了多線程操作資料庫安全,FMDB使用了FMDatabaseQueue,使用FMDatabaseQueue很簡單,首先用一個資料庫檔案地址來初使化FMDatabaseQueue,然後就可以將一個閉包(block)傳入inDatabase方法中。 在閉包中操作資料庫,而不直接參与FMDatabase的管理。

 

FMDatabaseQueue * queue = [FMDatabaseQueue databaseQueueWithPath:database_path];  

   dispatch_queue_t q1 = dispatch_queue_create("queue1", NULL);  

   dispatch_queue_t q2 = dispatch_queue_create("queue2", NULL);  

   dispatch_async(q1, ^{  

for (int i = 0; i < 50; ++i) {  

           [queue inDatabase:^(FMDatabase *db2) {  

               NSString *insertSql1= [NSString stringWithFormat:  

                                      @"INSERT INTO '%@' ('%@', '%@', '%@') VALUES (?, ?, ?)",  

                                      TABLENAME, NAME, AGE, ADDRESS];  

              NSString * name = [NSString stringWithFormat:@"jack %d", i];  

              NSString * age = [NSString stringWithFormat:@"%d", 10+i];  

     BOOL res = [db2 executeUpdate:insertSql1, name, age,@"濟南"];    

     if (!res) {  

                   NSLog(@"error to inster data: %@", name);  

               } else {  

                   NSLog(@"succ to inster data: %@", name);  

               }  

           }];  

       }  

 });  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.