sqlite中只支持 ALTER TABLE 命令的 RENAME TABLE 和 ADD COLUMN。 其他類型的 ALTER TABLE 操作如 DROP COLUMN,ALTER COLUMN,ADD CONSTRAINT 等等均被忽略。
重命名表名:
alter table tableName rename to newTableName
添加列
alter table tableName add columnName columnType
這兩個(gè)和sql server基本一致,用起來挺方便,但是后面就有點(diǎn)藍(lán)瘦了
刪除列
create table copyTableName (fields) insert into copyTableName (fields) select fields from tableName dorp table tableName alter table copyTableName rename to tableName
首先創(chuàng)建個(gè)新表,然后將原表數(shù)據(jù)轉(zhuǎn)移到新表,當(dāng)然這時(shí)候新表的fields只保留了不刪除的字段,然后將原表刪除,再將新表的名字改回原來的表名
修改列
實(shí)現(xiàn)方法和刪除列思路相同,只是fields里面不止要?jiǎng)h除原來的列,還要添加新的列名和類型
其他的暫時(shí)沒有遇到,下面記錄一下,中間用到的關(guān)于sqlite的一些知識(shí)
獲取表的創(chuàng)建sql
select sql from sqlite_master where name='tableName' and type='table'
獲取表的所有字段
PRAGMA table_info('tableName')
判斷表是否存在
select count(*) from sqlite_master where type='table' and name = 'tableName'
嗯,,,中規(guī)中矩的一篇記錄文
聯(lián)系客服