SQlite在已建立的表中插入一列 db.execSQL("ALTER TABLE " + table_name + " ADD COLUMN " + column_name + column_type);
SQlite在已建立的表中刪除一列 alter table student drop column name // 該行在SQlite中不能用,SQlite不支持drop
qlite中是不支持刪除列操做的,因此網上alter table table_name drop column col_name這個語句在sqlite中是無效的,而替代的方法能夠以下:1.根據原表建立一張新表 2.刪除原表 3.將新表重名爲舊錶的名稱 示例例子以下 1.建立一張舊錶Student,包含id(主碼),name, tel create table student ( id integer primary key, name text, tel text ) 2.給舊錶插入兩個值 insert into student(id,name,tel) values(101,"Jack","110") insert into student(id,name,tel) values(102,"Rose","119") 結果如圖 3.接下來咱們刪除電話這個列,首先根據student表建立一張新表teacher create table teacher as select id,name from student 結果如圖 能夠看到tel這一列已經沒有了 4.而後咱們刪除student這個表 drop table if exists student 5.將teacher這個表重命名爲student alter table teacher rename to student 結果演示: select * from student order by name desc(desc降序, asc升序) 這樣就能夠獲得咱們想要的結果了。 另外:給本身一個提示,在android sqlite中的查詢語句若是是text類型的別忘了給他加上」」來指明是String類型的,例如: Cursor c = mSQLiteDatabase.query(TABLE_NAME, null, NAME + "=" + "/"" + name + "/"", null, null,null,null); 方法二: http://www.sqlite.org/faq.html#q11 [java] view plain copy 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;