android 中关于Sqlite 表的创建 修改 删除

正好我要用
一、数据库定义语言 DDL

在关系型数据库中,数据库中的表 Table、视图 View、索引 Index、关系 Relationship 和触发器 Trigger 等等,构成了数据库的架构 Schema。 在 SQL 语句中,专门有一些语句用来定义数据库架构,这些语句被称为“数据库定义语言”,即 DDL。

SQLite 数据库引擎支持下列三种 DDL 语句:

       CREATE
       ALTER TABLE
       DROP

其中,CREATE 语句用来创建表 Table、视图 View、索引 Index、关系 Relationship 和触发器 Trigger, DROP语句用来删除表 Table、视图 View、索引 Index、关系 Relationship 和触发器 Trigger, ALTER TABLE 语句用来改变表的结构。

今天这一篇只涉及到表的相关内容,视图、触发器等到后面再讲。
二、SQLite 中的数据类型

SQLite 数据库中的数据一般由以下几种常用的数据类型组成:

       NULL - 空值
       INTEGER - 有符号整数
       REAL - 浮点数
       TEXT - 文本字符串
       BLOB - 二进制数据,如图片、声音等等

SQLite 也可以接受其他数据类型。
三、创建表 CREATE TABLE

首先,创建一个 test.db 数据库并进入 SQLite 命令行环境,还记得怎么做吗?

[xiazhujie@localhost]:~$ sqlite3 test.db
-- Loading resources from /home/xiazhujie
/.sqliterc
SQLite version 3.7.4
Enter ".help" for instructions
Enter SQL statements terminated with a ";"

sqlite> .tables

sqlite>

向上面这样,我们就在终端中创建了一个 test.db 数据库,并且通过 .tables 命令查询数据库中的表,结果没有任何返回,因为数据库本来就是空的嘛。

下面我们创建一个 Student 表,其中包含 Id、Name、Age 等字段.

sqlite> CREATE TABLE Students(Id integer,Name text,age integer);

sqlite> .tables
Students

sqlite> .schema Students
CREATE TABLE Students(Id integer,Name text,age integer);

向上面这样,一个 Students 表就被建立了,这回再运行 .tables 命令就有响应了,系统告诉我们数据库中现在有一个 Students 表, 运行 .schema 命令,返回了我们创建这个表的 SQL 命令。
四、修改表 ALTER TABLE

SQLite 仅仅支持 ALTER TABLE 语句的一部分功能,我们可以用 ALTER TABLE 语句来更改一个表的名字,也可向表中增加一个字段(列),但是我们不能删除一个已经存在的字段,或者更改一个已经存在的字段的名称、数据类型、限定符等等。

       改变表名 - ALTER TABLE 旧表名 RENAME TO 新表名
       增加一列 - ALTER TABLE 表名 ADD COLUMN 列名 数据类型 限定符

下面我们来演示一下,将前面的 Students 表的名字改为 Teachers

sqlite>
sqlite> .tables
Students

sqlite> ALTER TABLE Students RENAME TO Teachers;

sqlite> .tables
Teachers
sqlite>

原来数据库中只有一个 Students 表,改名以后再运行 .tables 命令,发现 Students 表已经没了,现在变成了 Teachers 表。

下面改变 Teachers 表的结构,增加一个 Sex 列


sqlite> .schema Teachers
CREATE TABLE "Teachers"(Id integer,Name text,age integer);

sqlite> ALTER TABLE Teachers ADD COLUMN Sex text;

sqlite> .schema Teachers
CREATE TABLE "Teachers"(Id integer,Name text,age integer, Sex text);


五、删除表 DROP TABLE

删除一个表很简单,只要给出表名即可

       删除表 - DROP TABLE 表名

下面,我们将 test.db 中的 Teachers 表删除


sqlite 中判断某个表是否存在的方法,贴出来供大家参考(java 、android) //这里不能删除不存在的表 所以要提前判断

      public boolean tabbleIsExist(String tableName){
                    boolean result = false;
                    if(tableName == null){
                                  return false;
                    }
                    SQLiteDatabase db = null;
                    Cursor cursor = null;
                    try {
                                  db = this.getReadableDatabase();
                                  String sql = "select count(*) as c from "+AppConstant.DataBaseName+" where type ='table' and name ='"+tableName.trim()+"' ";
                                  cursor = db.rawQuery(sql, null);
                                  if(cursor.moveToNext()){
                                              int count = cursor.getInt(0);
                                              if(count>0){
                                                            result = true;
                                              }
                                  }
                                 
                    } catch (Exception e) {
                                  // TODO: handle exception
                                          
                    return result;
      }

sqlite> .tables
Teachers

sqlite> DROP TABLE Teachers;

sqlite> .tables

删除 Teachers 表后再运行 .tables 命令,发现数据库已经空了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值