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的表,并設(shè)置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改:
不支持修改字段名挪哄;
不支持刪除字段名;