iOS mysql語句

1.創(chuàng)建數(shù)據(jù)庫

//數(shù)據(jù)庫存放的路徑
NSString *tempPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"temp.db"];
NSLog(@"path: %@", tempPath);
FMDatabase *db = [FMDatabase databaseWithPath:tempPath];

2.創(chuàng)建表

//創(chuàng)建一張名為Demo的表,并設置id為主鍵,字段為name的表
//插入一條name為@"Hello World"的數(shù)據(jù)
NSString *sql = @"create table Demo (id integer primary key autoincrement, name text);"
                 "insert into Demo (name) values ('Hello World');";

執(zhí)行SQL語句

BOOL success = [db executeStatements:sql];

3.對數(shù)據(jù)庫操作

3.1增:

給已存在的表插入一列

sql =   @"alter table Demo add column age integer;"
3.2刪:

給已存在的表刪除一列

由于SQLlite不支持drop方法,所以無法使用下面語句

sql =   @"alter table Demo drop column age;";

所以我們只能曲線救國:
1.根據(jù)原表創(chuàng)建一張新表
2.刪除原表
3.將新表重名為舊表的名稱

    sql = @"create table teacher as select id, name from Demo";
    BOOL success = [db executeStatements:sql];

    if (success) {
        sql = @"drop table if exists Demo";
        success = [db executeStatements:sql];
        
        if (success) {
            sql = @"alter table teacher rename to Demo";
            success = [db executeStatements:sql];
        }
    }
3.3改:

不支持修改字段名;
不支持刪除字段名;

3.3查:
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容