android sqlite删除列,SQlite在已创建的表中插入,删除一列

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;

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值