我想在sqlite数据库中删除或添加列
我正在使用以下查询来删除列.
ALTER TABLE TABLENAME DROP COLUMN COLUMNNAME
但它给出了错误
System.Data.SQLite.SQLiteException: SQLite error near "DROP": syntax error
Michał Powag.. 331
ALTER TABLE SQLite
SQLite支持ALTER TABLE的有限子集.SQLite中的ALTER TABLE命令允许用户重命名表或向现有表添加新列.无法重命名列,删除列,或从表中添加或删除约束.
您可以:
创建新表作为您要更改的表,
复制所有数据,
放下旧桌子,
重命名新的.
http://stackoverflow.com/a/5987838/1578528给出了执行任务的基本示例. (44认同)
在执行此序列之前,如果有外部表引用此表,则必须调用`PRAGMA foreign_keys = OFF`.在这种情况下,在执行此序列之后,必须调用`PRAGMA foreign_keys = ON`以重新启用外键. (3认同)
Udinic.. 45
我已经编写了一个基于Sqlite推荐方法的Java实现:
private void dropColumn(SQLiteDatabase db, ConnectionSource connectionSource, String createTableCmd, String tableName, String[] colsToRemove) throws java.sql.SQLException { ListupdatedTableColumns = getTableColumns(tableName); // Remove the columns we don't want anymore from the table's list of columns updatedTableColumns.removeAll(Arrays.asList(colsToRemove)); String columnsSeperated = TextUtils.join(",", updatedTableColumns); db.execSQL("ALTER TABLE " + tableName + " RENAME TO " + tableName + "_old;"); // Creating the table on its new format (no redundant columns) db.execSQL(createTableCmd); // Populating the table with the data db.execSQL("INSERT INTO " + tableName + "(" + columnsSeperated + ") SELECT " + columnsSeperated + " FROM " + tableName + "_old;"); db.execSQL("DROP TABLE " + tableName + "_old;"); }
要获取表的列,我使用了"PRAGMA table_info":
public ListgetTableColumns(String tableName) { ArrayList columns = new ArrayList (); String cmd = "pragma table_info(" + tableName + ");"; Cursor cur = getDB().rawQuery(cmd, null); while (cur.moveToNext()) { columns.add(cur.getString(cur.getColumnIndex("name"))); } cur.close(); return columns; }
我实际上在我的博客上写过它,你可以在那里看到更多的解释:
http://udinic.wordpress.com/2012/05/09/sqlite-drop-column-support/
ALTER TABLE SQLite
SQLite支持ALTER TABLE的有限子集.SQLite中的ALTER TABLE命令允许用户重命名表或向现有表添加新列.无法重命名列,删除列,或从表中添加或删除约束.
您可以:
创建新表作为您要更改的表,
复制所有数据,
放下旧桌子,
重命名新的.
我已经编写了一个基于Sqlite推荐方法的Java实现:
private void dropColumn(SQLiteDatabase db, ConnectionSource connectionSource, String createTableCmd, String tableName, String[] colsToRemove) throws java.sql.SQLException { ListupdatedTableColumns = getTableColumns(tableName); // Remove the columns we don't want anymore from the table's list of columns updatedTableColumns.removeAll(Arrays.asList(colsToRemove)); String columnsSeperated = TextUtils.join(",", updatedTableColumns); db.execSQL("ALTER TABLE " + tableName + " RENAME TO " + tableName + "_old;"); // Creating the table on its new format (no redundant columns) db.execSQL(createTableCmd); // Populating the table with the data db.execSQL("INSERT INTO " + tableName + "(" + columnsSeperated + ") SELECT " + columnsSeperated + " FROM " + tableName + "_old;"); db.execSQL("DROP TABLE " + tableName + "_old;"); }
要获取表的列,我使用了"PRAGMA table_info":
public ListgetTableColumns(String tableName) { ArrayList columns = new ArrayList (); String cmd = "pragma table_info(" + tableName + ");"; Cursor cur = getDB().rawQuery(cmd, null); while (cur.moveToNext()) { columns.add(cur.getString(cur.getColumnIndex("name"))); } cur.close(); return columns; }
我实际上在我的博客上写过它,你可以在那里看到更多的解释:
http://udinic.wordpress.com/2012/05/09/sqlite-drop-column-support/
正如其他人所指出的那样
无法重命名列,删除列,或从表中添加或删除约束.
来源:http://www.sqlite.org/lang_altertable.html
虽然您可以随时创建一个新表,然后删除旧表.我将尝试用一个例子来解释这个解决方法.
sqlite> .schema CREATE TABLE person( id INTEGER PRIMARY KEY, first_name TEXT, last_name TEXT, age INTEGER, height INTEGER ); sqlite> select * from person ; id first_name last_name age height ---------- ---------- ---------- ---------- ---------- 0 john doe 20 170 1 foo bar 25 171
现在您要height
从此表中删除该列.
创建另一个名为的表 new_person
sqlite> CREATE TABLE new_person( ...> id INTEGER PRIMARY KEY, ...> first_name TEXT, ...> last_name TEXT, ...> age INTEGER ...> ) ; sqlite>
现在复制旧表中的数据
sqlite> INSERT INTO new_person ...> SELECT id, first_name, last_name, age FROM person ; sqlite> select * from new_person ; id first_name last_name age ---------- ---------- ---------- ---------- 0 john doe 20 1 foo bar 25 sqlite>
现在删除person
表并重命名new_person
为person
sqlite> DROP TABLE IF EXISTS person ; sqlite> ALTER TABLE new_person RENAME TO person ; sqlite>
所以现在如果你做了.schema
,你会看到
sqlite>.schema CREATE TABLE "person"( id INTEGER PRIMARY KEY, first_name TEXT, last_name TEXT, age INTEGER );
http://www.sqlite.org/lang_altertable.html
如图所示,仅支持ADD COLUMN.但是有一种(有点重)解决方法:http://www.sqlite.org/faq.html#q11