本文摘自PHP中文网,作者不言,侵删。
SQLite是一个轻量级的SQL数据库,它实现了一个独立的、无服务器的、零配置的事务性SQL数据库引擎。除了一些命令外,sqlite使用的命令语法与mysql、oracle使用的类似,本篇文章将介绍如何使用命令行来使用sqlite数据库。
1、创建SQLite数据库
SQLite提供了一个简单的命令来创建数据库。使用以下命令创建sqlite数据库。
基本上,sqlite数据库是在当前工作目录中创建的文件。
2.在SQLite数据库中创建表
创建数据库后,我们创建表。使用以下查询在数据库admin.db中创建两个表(users, posts )。
1 2 3 4 5 | # sqlite3 admin.db
sqlite> create table users(uid integer ,uname varchar (60),category varchar (50));
sqlite> create table posts(postid integer ,postname varchar (50),content varchar (1000));
sqlite> create table tmp(id integer ,tname varchar (50);
sqlite> .quit
|
3.在SQLite中列出或删除表
要仅在SQLite数据库中列出表名,只需使用以下命令。
1 2 | sqlite> .tables
posts tmp users
|
如果需要删除任何表,可以使用以下命令执行此操作,如下所示。
1 2 3 4 | # drop table <tablename>;
# drop table if exists <tablename>;
# drop table tmp;
# drop table if tmp;
|
4.在表格中插入数据
以下命令用于通过SQLite提示在SQLite数据库中插入数据。
1 2 3 4 | sqlite> INSERT INTO posts VALUES (1, 'Post 1' , 'this is demo post 1' );
sqlite> INSERT INTO posts VALUES (2, 'Post 2' , 'this is demo post 2' );
sqlite> INSERT INTO users VALUES (1, 'Harry' , 'staff' );
sqlite> INSERT INTO users VALUES (2, 'Rahul' , 'Admin' );
|
还可以执行文件中包含的一组命令。
1 2 3 4 5 | # vi data.sql
INSERT INTO posts VALUES (10, 'Sample Post 10' , 'this is sample post 10' );
INSERT INTO posts VALUES (11, 'Sample Post 11' , 'this is sample post 11' );
INSERT INTO users VALUES (10, 'Sarah' , 'Support' );
INSERT INTO users VALUES (11, 'Nick' , 'Sales' );
|
以下命令将执行admin.db数据库中data.sql的所有命令。
1 | # sqlite3 admin.db < data.sql
|
5.从表中获取数据
使用SELECT命令查看SQLite数据库中表的数据,如下例所示。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | sqlite> SELECT * FROM users;
1|Harry|staff
2|Rahul|Admin
10|Sarah|Support
11|Nick|Sales
sqlite> SELECT * FROM posts;
1|Post 1|this is demo post 1
2|Post 2|this is demo post 2
10|Sample Post 10|this is sample post 10
11|Sample Post 11|this is sample post 11
sqlite> SELECT * FROM posts WHERE postid = 1;
1|Post 1|this is demo post 1
|
6.更改输出格式
SQLite3以八种不同的格式显示查询结果:“csv”,“column”,“html”,“insert”,“line”,“list”,“tabs”和“tcl”。使用“.mode”命令可以更改输出格式。默认输出格式为“list”。
1 2 3 4 5 6 7 8 9 | sqlite> .mode line
sqlite> select * from users;
uid = 1
uname = Harry
category = staff
uid = 2
uname = Rahul
category = Admin
|
1 2 3 4 5 6 | sqlite> .mode column
sqlite> select * from users;
1 Harry staff
2 Rahul Admin
10 Sarah Support
11 Nick Sales
|
7.将SQLite数据库转换为ASCII文本文件
可以使用“.dump”命令将SQLite数据库简单地转换为纯文本文件。使用以下命令执行。
1 | # sqlite3 admin.db '.dump' > backup.dump
|
要从ASCII文件backup.dump重建SQLite数据库,只需输入:
1 | #cat backup.dump | sqlite3 admin-1.db
|
以上就是SQLite3 sql命令行怎么使用?的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
python执行数据库的查询操作实例讲解
Sqlite教程(十二):锁和并发控制详解
android Sqlite基本用法详解
python实现的人脸识别打卡系统
c#Sqlite数据库的搭建及使用技巧
navicat for Sqlite导入csv中文数据的方法
Sqlite3 api 编程手册
android studio 使用adb 命令传递文件到android 设备的方法
Sqlite教程(四):内置函数
c#连接到sql server2008数据库的实例代码
更多相关阅读请进入《Sqlite》频道 >>
机械工业出版社
本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。
转载请注明出处:木庄网络博客 » SQLite3 sql命令行怎么使用?