[数据库]Sqlite使用入门
官网的文档结构十分恶劣,大概翻了一下,提供入门指引。
0. sqlite的安装
根据自身情况,在官网下载32位/64位的dll文件以及sqlite-tools-win32-x86-3240000.zip 。
sqlite无需安装,本体是下载的dll文件(def文件是什么鬼我也不懂), tool比如sqlite3.exe只是操作sqlite的工具。
两者解压到同一个文件夹下即可。 如C:\sqlite.
通过cmd来到解压路径,输入sqlite3,如果看到相应的进入shell窗口的提示,则表示一切顺利。
如果想要在全局cmd环境下启用sqlite3命令,将文件夹路径加入PATH环境变量即可。
由于sqlite太轻量,也有将sqlite3.exe和sqlite.dll放到C:\Windows\System32下的,无需修改PATH变量。
1. Getting Started
使用sqlite十分简单。如上做好安装之后,在cmd输入sqlite3 xxx.db 即可创建(进入)名为xxx的数据库。
sqlite并没有账户控制,所以也不用纠结用户名和密码的设置。
刚进入shell,sqlite还没有创建xxx.db的文件,当用户做出有效操作后,sqlite才会在当前路径下创建一个名为xxx.db的文件。这个文件即是传统意义上的一个库了。
如果一开始不指定dbname,直接在cmd输入sqlite,也可以进入shell。此时sqlite会隐式创建一个临时数据库,用户依然可以在shell中执行创建表等操作,但是当用户退出shell时,这个隐式的数据库也会跟着销毁。若想要保存这期间的操作,退出之前保存到一个文件中即可。命令为: .save xxx.db
Quick Reference:
创建(或打开)数据库(名为name.db):
> sqlite3 name.db
退出数据库:
>>.quit
查看数据库:
>> .databases
查看创建表:
>> .tables
查看表结构:
>> .schema tb1
CURD操作:(都是常规的SQL语句)
>> SELECT * FROM tb1;
>> UPDATE tb1 SET ...;
>> 略
2. sqlite 的dot commands
sqlite的dot(即".")操作通常用于修改查询的输出或执行特定的预打包的查询语句,属于sqlite的自身的操作指令。
以下是dot命令列表,可以在shell中输入.help查看。个人常用的命令标红显示。
sqlite> .help
.archive ... Manage SQL archives: ".archive --help" for details
.auth ON|OFF Show authorizer callbacks
.backup ?DB? FILE Backup DB (default "main") to FILE
Add "--append" to open using appendvfs.
.bail on|off Stop after hitting an error. Default OFF
.binary on|off Turn binary output on or off. Default OFF
.cd DIRECTORY Change the working directory to DIRECTORY
.changes on|off Show number of rows changed by SQL
.check GLOB Fail if output since .testcase does not match
.clone NEWDB Clone data into NEWDB from the existing database
.databases List names and files of attached databases
.dbconfig ?op? ?val? List or change sqlite3_db_config() options
.dbinfo ?DB? Show status information about the database
.dump ?TABLE? ... Dump the database in an SQL text format
If TABLE specified, only dump tables matching
LIKE pattern TABLE.
.echo on|off Turn command echo on or off
.eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN
.excel Display the output of next command in a spreadsheet
.exit Exit this program
.expert EXPERIMENTAL. Suggest indexes for specified queries
.fullschema ?--indent? Show schema and the content of sqlite_stat tables
.headers on|off Turn display of headers on or off
.help Show this message
.import FILE TABLE Import data from FILE into TABLE
.imposter INDEX TABLE Create imposter table TABLE on index INDEX
.indexes ?TABLE? Show names of all indexes
If TABLE specified, only show indexes for tables
matching LIKE pattern TABLE.
.iotrace FILE Enable I/O diagnostic logging to FILE
.limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT
.lint OPTIONS Report potential schema issues. Options:
fkey-indexes Find missing foreign key indexes
.load FILE ?ENTRY? Load an extension library
.log FILE|off Turn logging on or off. FILE can be stderr/stdout
.mode MODE ?TABLE? Set output mode where MODE is one of:
ascii Columns/rows delimited by 0x1F and 0x1E
csv Comma-separated values
column Left-aligned columns. (See .width)
html HTML <table> code
insert SQL insert statements for TABLE
line One value per line
list Values delimited by "|"
quote Escape answers as for SQL
tabs Tab-separated values
tcl TCL list elements
.nullvalue STRING Use STRING in place of NULL values
.once (-e|-x|FILE) Output for the next SQL command only to FILE
or invoke system text editor (-e) or spreadsheet (-x)
on the output.
.open ?OPTIONS? ?FILE? Close existing database and reopen FILE
The --new option starts with an empty file
Other options: --readonly --append --zip
.output ?FILE? Send output to FILE or stdout
.print STRING... Print literal STRING
.prompt MAIN CONTINUE Replace the standard prompts
.quit Exit this program
.read FILENAME Execute SQL in FILENAME
.restore ?DB? FILE Restore content of DB (default "main") from FILE
.save FILE Write in-memory database into FILE
.scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off
.schema ?PATTERN? Show the CREATE statements matching PATTERN
Add --indent for pretty-printing
.selftest ?--init? Run tests defined in the SELFTEST table
.separator COL ?ROW? Change the column separator and optionally the row
separator for both the output mode and .import
.session CMD ... Create or control sessions
.sha3sum ?OPTIONS...? Compute a SHA3 hash of database content
.shell CMD ARGS... Run CMD ARGS... in a system shell
.show Show the current values for various settings
.stats ?on|off? Show stats or turn stats on or off
.system CMD ARGS... Run CMD ARGS... in a system shell
.tables ?TABLE? List names of tables
If TABLE specified, only list tables matching
LIKE pattern TABLE.
.testcase NAME Begin redirecting output to 'testcase-out.txt'
.timeout MS Try opening locked tables for MS milliseconds
.timer on|off Turn SQL timer on or off
.trace FILE|off Output each SQL statement as it is run
.vfsinfo ?AUX? Information about the top-level VFS
.vfslist List all available VFSes
.vfsname ?AUX? Print the name of the VFS stack
.width NUM1 NUM2 ... Set column widths for "column" mode
Negative values right-justify
sqlite>
shell中的SQL语句比较随意, 只有输入分号的时候才会执行。 但是dot命令必须以.开头,回车即执行。
改变输出格式的几个操作:
sqlite> .mode list
sqlite> select * from tbl1;
hello|10
goodbye|20
sqlite> .mode line
sqlite> select * from tbl1;
one = hello
two = 10 one = goodbye
two = 20
sqlite> .mode column
sqlite> select * from tbl1;
one two
---------- ----------
hello 10
goodbye 20
sqlite>
sqlite> .header off
sqlite> select * from tbl1;
hello 10
goodbye 20
sqlite>
其他常见操作:
- 将结果写入文件
- CSV import
- CSV Export
- Excel Export
....
操作太杂,如果有更多需求如Index等请直接查看文档。
https://sqlite.org/cli.html
[数据库]Sqlite使用入门的更多相关文章
- Android学习---如何创建数据库,SQLite(onCreate,onUpgrade方法)和SQLiteStudio的使用
一.android中使用什么数据库? SQLite是遵守ACID的关系数据库管理系统,它包含在一个相对小的C程式庫中.它是D.RichardHipp建立的公有领域项目.SQLite 是一个软件库,实现 ...
- python 学习笔记6(数据库 sqlite)
26. SQLite 轻量级的关系型数据库 SQLite是python自带的数据库,可以搭配python存储数据,开发网站等. 标准库中的 sqlite3 提供该数据库的接口. 1. 基本语法如下 c ...
- 数据库SQLite在Qt5+VS2012使用规则总结---中文乱码
VS2012默认格式为 "GB2312-80",而有时我们用到字符串需要显示中文时,就会出现乱码.下面仅就Qt5和VS2012中使用数据库SQLite时,做一个简单的备忘录 #in ...
- Python信息采集器使用轻量级关系型数据库SQLite
1,引言Python自带一个轻量级的关系型数据库SQLite.这一数据库使用SQL语言.SQLite作为后端数据库,可以搭配Python建网站,或者为python网络爬虫存储数据.SQLite还在其它 ...
- (转)轻量级数据库 SQLite
SQLite Expert – Personal Edition SQLite Expert 提供两个版本,分别是个人版和专业版.其中个人版是免费的,提供了大多数基本的管理功能. SQLite Exp ...
- iOS基础 - 数据库-SQLite
一.iOS应用数据存取的常用方式 XML属性列表 —— PList NSKeyedArchiver 归档 Preference(偏好设置) SQLite3 Core Data(以面向对象的方式操作数据 ...
- python数据库操作 - MySQL入门【转】
python数据库操作 - MySQL入门 python学院 2017-02-05 16:22 PyMySQL是Python中操作MySQL的模块,和之前使用的MySQLdb模块基本功能一致,PyMy ...
- SQLite使用入门
什么是SQLite SQLite是一款非常轻量级的关系数据库系统,支持多数SQL92标准.SQLite在使用前不需要安装设置,不需要进程来启动.停止或配置,而其他大多数SQL数据库引擎是作为一个单独的 ...
- MySQL数据库应用 从入门到精通 学习笔记
以下内容是学习<MySQL数据库应用 从入门到精通>过程中总结的一些内容提要,供以后自己复现使用. 一:数据库查看所有数据库: SHOW DATABASES创建数据库: CREATE DA ...
随机推荐
- Win#password;;processon #clone;;disassemble;;source find
1.密码学思维导图 源地址:https://www.processon.com/view/5a61d825e4b0c090524f5b8b 在这之前给大家分享 如何在 processon上搜索公开克隆 ...
- java常用的中间件
tomcatWeblogicJBOSSColdfusionWebsphereGlassFish 一般本地开发的话建议使用tomcat. linux系统建议使用jetty或apache hpptd 大型 ...
- netty 学习(1)
Netty使用:通过BootStrap来启动.而BootStrap主要分为两类:1.面向连接(TCP)的(ClientBootStrap和ServerBootStrap);2. 非面向连接(UDP)的 ...
- python爬虫-淘宝商品密码(图文教程附源码)
今天闲着没事,不想像书上介绍的那样,我相信所有的数据都是有规律可以寻找的,然后去分析了一下淘宝的商品数据的规律和加密方式,用了最简单的知识去解析了需要的数据. 这个也让我学到了,解决问题的方法不止一个 ...
- Python(列表操作应用实战)
# 输入一个数据,删除一个列表中的所有指定元素# 给定的列表数据data = [1,2,3,4,5,6,7,8,9,0,5,4,3,5,"b","a",&quo ...
- Gradle Build速度加快方法汇总
Android Studio用起来越来越顺手,但是却发现Build的速度实在不敢恭维,在google和度娘了几把(....)之后,大体就是分配更高的内存,步骤:Setting-->搜索gradl ...
- openresty capture
local args = {} args["name"] = "张三" args["sex"] = "男" local ...
- C 语言的 GCC 扩展
GNU 编译器(GCC)提供了很多 C 语言扩展,编译器会使用该信息生成更高效的机器代码. 内联函数 static inline __attribute__ ((always_inline)) int ...
- 封装curl的get和post请求
/** * GET 请求 * @param string $url */ function http_get($url){ $oCurl = curl_init(); if(stripos($url, ...
- [POI2007]堆积木Klo
题解: dp定义方程的时候 好像也不能都用前一个来递推..这样就不能优化了 这题看了题解才想出来... 还是很简单的啊.... 我们定义f[i]表示前i个最大收益 那么j要能从i转移就得满足a[i]- ...