SQLite 入门教程(一)基本控制台(终端)命令
一、基本简介 SQLite 是一个自持的(self-contained)、无服务器的、零配置的、事务型的关系型数据库引擎。因为他很小,所以也可以作为嵌入式数据库内建在你的应用程序中。SQLite 被应用在 Solaris 10操作系统、Mac OS 操作系统、iPhone 和 Skype 中。QT4 、
一、基本简介
SQLite 是一个自持的(self-contained)、无服务器的、零配置的、事务型的关系型数据库引擎。因为他很小,所以也可以作为嵌入式数据库内建在你的应用程序中。SQLite 被应用在 Solaris 10操作系统、Mac OS 操作系统、iPhone 和 Skype 中。QT4 、Python 、 PHP 都默认支持 SQLite ,Firefox Amarok 等流行的应用程序在内部也使用了 SQLite.
SQLite 数据库引擎实现了主要的 SQL-92 标准,引擎本身只有一个文件,大小不到 300k ,但是并不作为一个独立的进程运行,而是动态或者静态的链接到其他应用程序中。它生成的数据库文件是一个普通的磁盘文件,可以放置在任何目录下。SQLite 本身是 C 语言开发的,开源也跨平台,并且被所有的主流编程语言支持。
相关资源
sqlite.org
wikipedia.org
二、下载安装
Windows 版的下载地址为:sqlite-shell-win32-x86-3070701.zip
我们这里下载的是命令行版本,所以是一个可执行文件,还有一个动态链接库版本,如果你的应用程序需要嵌入式数据库,可以下载这个版本。当然,如果你愿意折腾,下载源代码自己编译也是可以的。下载完成,解压出来就一个文件: sqlite3.exe ,可以放置到任意一个路径下,然后把这个路径加入到 PATH 环境变量中,这样我们就可以随时在控制台中运行 SQLite 命令行工具了。
三、基本命令
1、进入命令行环境:sqlite3
打开一个控制台窗口,输入 sqlite3 回车,这时你就进入了 SQLite 命令行环境,如图
它显示了版本号,并告诉你每一条 SQL 语句必须用分号 ; 结尾
2、命令行帮助:.help
在命令行环境下输入 .help 回车,显示所有可使用的命令以及这些命令的帮助。注意:所有的命令开头都是一个点
3、退出命令行环境
.quit 或者 .exit 都可以退出
四、数据库和表的相关命令
1、创建一个新的数据库:sqlite3 文件名
先建立一个 Db 目录,并在 Db 目录中创建一个 test.db 数据库文件,打开控制台窗口,命令如下:
mkdir Db
cd Db
sqlite3 test.db
2、打开一个已经存在的数据库:sqlite3 已经存在的文件名
创建一个新数据库和打开一个已经存在的数据库命令是一模一样的,如果文件在当前目录下不存在,则新建;如果存在,则打开。
3、导入数据:.read 数据文件
打开记事本,并将下列 SQL 语句复制到记事本中,保存为 test.sql 到上面说到的 Db 目录下,在命令行环境中输入
.read test.sql
即将所有的数据导入到 test.db 数据库中。
test.db 导入数据
BEGIN TRANSACTION;
CREATE TABLE Cars(Id integer PRIMARY KEY, Name text, Cost integer);
INSERT INTO Cars VALUES(1,'Audi',52642);
INSERT INTO Cars VALUES(2,'Mercedes',57127);
INSERT INTO Cars VALUES(3,'Skoda',9000);
INSERT INTO Cars VALUES(4,'Volvo',29000);
INSERT INTO Cars VALUES(5,'Bentley',350000);
INSERT INTO Cars VALUES(6,'Citroen',21000);
INSERT INTO Cars VALUES(7,'Hummer',41400);
INSERT INTO Cars VALUES(8,'Volkswagen',21600);
COMMIT;
BEGIN TRANSACTION;
CREATE TABLE Orders(Id integer PRIMARY KEY, OrderPrice integer CHECK(OrderPrice>0),
Customer text);
INSERT INTO Orders(OrderPrice, Customer) VALUES(1200, "Williamson");
INSERT INTO Orders(OrderPrice, Customer) VALUES(200, "Robertson");
INSERT INTO Orders(OrderPrice, Customer) VALUES(40, "Robertson");
INSERT INTO Orders(OrderPrice, Customer) VALUES(1640, "Smith");
INSERT INTO Orders(OrderPrice, Customer) VALUES(100, "Robertson");
INSERT INTO Orders(OrderPrice, Customer) VALUES(50, "Williamson");
INSERT INTO Orders(OrderPrice, Customer) VALUES(150, "Smith");
INSERT INTO Orders(OrderPrice, Customer) VALUES(250, "Smith");
INSERT INTO Orders(OrderPrice, Customer) VALUES(840, "Brown");
INSERT INTO Orders(OrderPrice, Customer) VALUES(440, "Black");
INSERT INTO Orders(OrderPrice, Customer) VALUES(20, "Brown");
COMMIT;
BEGIN TRANSACTION;
CREATE TABLE Friends(Id integer PRIMARY KEY, Name text UNIQUE NOT NULL,
Sex text CHECK(Sex IN ('M', 'F')));
INSERT INTO Friends VALUES(1,'Jane', 'F');
INSERT INTO Friends VALUES(2,'Thomas', 'M');
INSERT INTO Friends VALUES(3,'Franklin', 'M');
INSERT INTO Friends VALUES(4,'Elisabeth', 'F');
INSERT INTO Friends VALUES(5,'Mary', 'F');
INSERT INTO Friends VALUES(6,'Lucy', 'F');
INSERT INTO Friends VALUES(7,'Jack', 'M');
COMMIT;
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS Customers(CustomerId integer PRIMARY KEY, Name text);
INSERT INTO Customers(Name) VALUES('Paul Novak');
INSERT INTO Customers(Name) VALUES('Terry Neils');
INSERT INTO Customers(Name) VALUES('Jack Fonda');
INSERT INTO Customers(Name) VALUES('Tom Willis');
CREATE TABLE IF NOT EXISTS Reservations(Id integer PRIMARY KEY,
CustomerId integer, Day text);
INSERT INTO Reservations(CustomerId, Day) VALUES(1, '2009-22-11');
INSERT INTO Reservations(CustomerId, Day) VALUES(2, '2009-28-11');
INSERT INTO Reservations(CustomerId, Day) VALUES(2, '2009-29-11');
INSERT INTO Reservations(CustomerId, Day) VALUES(1, '2009-29-11');
INSERT INTO Reservations(CustomerId, Day) VALUES(3, '2009-02-12');
COMMIT;
BEGIN TRANSACTION;
CREATE TABLE Names(Id integer, Name text);
INSERT INTO Names VALUES(1,'Tom');
INSERT INTO Names VALUES(2,'Lucy');
INSERT INTO Names VALUES(3,'Frank');
INSERT INTO Names VALUES(4,'Jane');
INSERT INTO Names VALUES(5,'Robert');
COMMIT;
BEGIN TRANSACTION;
CREATE TABLE Books(Id integer PRIMARY KEY, Title text, Author text,
Isbn text default 'not available');
INSERT INTO Books VALUES(1,'War and Peace','Leo Tolstoy','978-0345472403');
INSERT INTO Books VALUES(2,'The Brothers Karamazov',
'Fyodor Dostoyevsky','978-0486437910');
INSERT INTO Books VALUES(3,'Crime and Punishment',
'Fyodor Dostoyevsky','978-1840224306');
COMMIT
4、列出所有的数据表: .tables
完成上面所有的工作以后,,我们就可以列出所有的数据表了
5、显示数据库结构:.schema
其实就是一些 SQL 语句,他们描述了数据库的结构,如图
6、显示表的结构:.schema 表名
7、导出某个表的数据: .dump 表名
这时我们可以看到,整个表以 SQL 语句的形式为导出来了,但是只是显示在终端上,如何把它导出到文件中呢?
8、设置导出目标:
.output 文件名
或者
.output stdout
先运行 .output cars.sql ,然后再运行 .dump 命令试试看?如果要回复成导出到终端(标准输出),则运行 .output stdout
五、数据显示相关命令
1、设置分隔符:.separator 分隔符
我们可以首先运行 SELECT * FROM Names; ,可以看到默认的分隔符是 |
运行.separator : 以后,再 SELECT * FROM Names;,可以看到分隔符已经变成 : 了
2、设置显示模式:.mode 模式
有好几种显示模式,默认的是 list 显示模式,一般我们使用 column 显示模式,还有其他几种显示模式可以 .help 看 mode 相关内容。看看下面的图,和上面是不是显示的不一样了?
3、显示标题栏:.headers on
看看,是不是又不太一样了?
4、设置每一列的显示宽度:.width w1,w2,w3.........
一些内容,默认的宽度显示不下,这个命令就有用了
5、设置 NULL 值显示成什么样子: .nullvalue 你想要的NULL值格式
默认情况下NULL值什么也不显示,你可以设置成你自己想要的样子
6、列出当前显示格式设置情况:.show
7、配置文件 .sqliterc

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



It is a very common problem these days that Ubuntu does not allow its users to open the terminal. If you receive a similar issue and don’t know what to do next, learn about five fixes on how to resolve this “Ubuntu cannot open terminal” issue on your Linux device. Without further ado, let’s dive into what causes it and the solutions available to it. Why can't Ubuntu open the terminal on it? This mainly happens when you install some defective software or modify the terminal configuration. In addition to this, new applications or games that interact with locales and corrupt them can cause similar problems. Some users reported a fix for this issue when searching for Terminal in Ubuntu's activity menu. This shows that

The sudo command allows users to run commands in elevated privilege mode without switching to superuser mode. This article will introduce how to simulate functions similar to sudo commands in Windows systems. What is the Shudao Command? Sudo (short for "superuser do") is a command-line tool that allows users of Unix-based operating systems such as Linux and MacOS to execute commands with elevated privileges typically held by administrators. Running SUDO commands in Windows 11/10 However, with the launch of the latest Windows 11 Insider preview version, Windows users can now experience this feature. This new feature enables users to
![Xbox System Error E200 [Fixed]](https://img.php.cn/upload/article/000/465/014/170832475129577.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
This article will explain how to resolve system error E200 on your Xbox console. Typically, this error occurs when your Xbox console gets interrupted while trying to install the latest console operating system update. This error may also occur if the system update is interrupted due to a power outage or network problem. Fix Xbox System Error E200 Use the following fix to fix System Error E200 on your Xbox console: Turn your Xbox console off and on again Perform a system update factory reset your console Let’s get started. 1] Turning your Xbox console off and on again Resetting the power cycle of your Xbox console can effectively eliminate potential temporary glitches and resolve some issues. Follow these steps to turn off and reopen Xbox Control

This article will introduce readers to how to use the command prompt (CommandPrompt) to find the physical address (MAC address) of the network adapter in Win11 system. A MAC address is a unique identifier for a network interface card (NIC), which plays an important role in network communications. Through the command prompt, users can easily obtain the MAC address information of all network adapters on the current computer, which is very helpful for network troubleshooting, configuring network settings and other tasks. Method 1: Use "Command Prompt" 1. Press the [Win+X] key combination, or [right-click] click the [Windows logo] on the taskbar, and in the menu item that opens, select [Run]; 2. Run the window , enter the [cmd] command, and then

In Win11 system, you can enable or disable Hyper-V enhanced session mode through commands. This article will introduce how to use commands to operate and help users better manage and control Hyper-V functions in the system. Hyper-V is a virtualization technology provided by Microsoft. It is built into Windows Server and Windows 10 and 11 (except Home Edition), allowing users to run virtual operating systems in Windows systems. Although virtual machines are isolated from the host operating system, they can still use the host's resources, such as sound cards and storage devices, through settings. One of the key settings is to enable Enhanced Session Mode. Enhanced session mode is Hyper

1. Overview The sar command displays system usage reports through data collected from system activities. These reports are made up of different sections, each containing the type of data and when the data was collected. The default mode of the sar command displays the CPU usage at different time increments for various resources accessing the CPU (such as users, systems, I/O schedulers, etc.). Additionally, it displays the percentage of idle CPU for a given time period. The average value for each data point is listed at the bottom of the report. sar reports collected data every 10 minutes by default, but you can use various options to filter and adjust these reports. Similar to the uptime command, the sar command can also help you monitor the CPU load. Through sar, you can understand the occurrence of excessive load

Linux is a powerful operating system that provides many efficient inter-process communication mechanisms, such as pipes, signals, message queues, shared memory, etc. But is there a simpler, more flexible, and more efficient way to communicate? The answer is yes, that is eventfd. eventfd is a system call introduced in Linux version 2.6. It can be used to implement event notification, that is, to deliver events through a file descriptor. eventfd contains a 64-bit unsigned integer counter maintained by the kernel. The process can read/change the counter value by reading/writing this file descriptor to achieve inter-process communication. What are the advantages of eventfd? It has the following features

Widgets are a new feature of the Win11 system. They are turned on by default. However, it is inevitable that some users do not use widgets very much and want to disable them because they take up space. So how should they do this? The editor below will teach you how to operate it, and you can try it out. What are widgets? Widgets are small cards that display dynamic content from your favorite apps and services on your Windows desktop. They appear on the widget board, where you can discover, pin, unpin, arrange, resize, and customize widgets to reflect your interests. The widget board is optimized to display relevant widgets and personalized content based on usage. Open the widget panel from the left corner of the taskbar, where you can see live weather
