lyfengzi

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1、在已存在的表中新增字段时并给字段设置默认值,该默认值必须是确切的值。

比如:

alter table TableName add column ColumnName text default null;--这句执行是OK的

alter table TableName add column ColumnName datetime default (datetime('now', 'localtime'));--这句执行的时候会抛出(Cannot add a column with non-constant default)

2、SQLite不支持对列的改名和修改类型等操作,想要操作官方给出的方法是先备份原表数据到临时表,然后删除原表,再创建新的表结构,然后导入临时表的数据,官方解释如下:(11) How do I add or delete columns from an existing table in SQLite.

SQLite has limited ALTER TABLE support that you can use to add a column to the end of a table or to change the name of a table. If you want to make more complex changes in the structure of a table, you will have to recreate the table. You can save existing data to a temporary table, drop the old table, create the new table, then copy the data back in from the temporary table.

For example, suppose you have a table named “t1″ with columns names “a”, “b”, and “c” and that you want to delete column “c” from this table. The following steps illustrate how this could be done:

BEGIN TRANSACTION;
CREATE TEMPORARY TABLE t1_backup(a,b);
INSERT INTO t1_backup SELECT a,b FROM t1;
DROP TABLE t1;
CREATE TABLE t1(a,b);
INSERT INTO t1 SELECT a,b FROM t1_backup;
DROP TABLE t1_backup;
COMMIT;

posted on 2012-05-17 00:19  lyfengzi  阅读(600)  评论(0)    收藏  举报