第三篇 FMDB的使用

来源:互联网 发布:淘宝直通车尺寸 编辑:程序博客网 时间:2024/06/05 05:57

使用第三方FMDB 应用内嵌sqlite

首先初始化数据库

#define DATABASENAME  @"*******.sqlite3"

static NSString * dataBaseFilePath = nil;
static FMDatabase *db = nil;
{
    if (!dataBaseFilePath)
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString * documentDirectory = [paths objectAtIndex:0];
        dataBaseFilePath = [documentDirectory stringByAppendingPathComponent:DATABASENAME];
    }
    return dataBaseFilePath;
}

然后建表

    if(![db tableExists:@"orderTable"])
    {
        [db executeUpdate:@"CREATE TABLE IF NOT EXISTS orderTable(_id INTEGER PRIMARY KEY AUTOINCREMENT,productGuid TEXT,  goldCount TEXT ,theUsd TEXT , theState TEXT );"];
    }

然后 操作

{
    if (!db)
    {
        db = [self getDataBase];
    }
    [db open];
    [self createTable];
    [db beginTransaction]; //与commit 成对出现。查询时候不需要

//增

//  [db executeUpdate:@"INSERT INTO orderTable(productGuid, goldCount,theUsd,theState) VALUES(?,?,?,?)",prductGuid,goldCount,usd,state];


//删

//       [db executeUpdate:@"DELETE from orderTable WHERE productGuid = ? ",pGuid];

//改

//   [db executeUpdate:@"UPDATE orderTable SET goldCount = ? WHERE theUsd = ?",gold,usd];

//查

//    FMResultSet *rs=[db executeQuery:@"select * from orderTable"];
//    while ([rs next])
//    {
                   //查处结果以后的操作
//    }

//  [rs close];


    [db commit];
    [db close];

}

0 0