sqlite3增删改查,导入导出

来源:互联网 发布:java项目视频百度云 编辑:程序博客网 时间:2024/05/19 16:33

一、创建表

创建一个 Student 表,其中包含 Id、Name、Age 等字段.
sqlite>
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);
sqlite>

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

二、修改表

下面,将前面的 Students 表的名字改为 Teachers
sqlite>
sqlite> .tables
Students
sqlite> ALTER TABLE Students RENAME TO Teachers;
sqlite> .tables
Teachers
sqlite>

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

改变 Teachers 表的结构,增加一个 Sex 列
sqlite>
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);
sqlite>

三、删除表

将Teachers 表删除
sqlite>
sqlite> .tables
Teachers
sqlite> DROP TABLE Teachers;
sqlite> .tables
sqlite>

删除 Teachers 表后再运行 .tables 命令,发现数据库已经空了

四、导出

sqlite>.output mydb.sql
sqlite>.dump
sqlite>.output stdout

五、导入

sqlite>.read file.sql

0 0