[数据库]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 ...
随机推荐
- 性能测试四十七:jmeter性能监控工具ServerAgent
在liunx压力机进行压测的时候,可以在widows下开一个jmeter,只进行监控用,不产生压力,监控效果和dstat差不多 jmeter安装客户端插件 把工具放到服务端任意目录并解压,我这里放到了 ...
- Android学习基础部分
今天是放寒假的第四天,这几天加上放假前总共弄了一些Android编程的基础部分,比如下载sdk.配置环境.下载Eclipse的插件(ADT).下载Android Studio.VirtualBox.G ...
- python进行进制转换
# 10进制转为2进制 print(bin(10)) # 结果:0b1010 # 2进制转为10进制 print(int("1001", 2)) # 结果:9 # 10进制转为16 ...
- 十图详解tensorflow数据读取机制(附代码)转知乎
十图详解tensorflow数据读取机制(附代码) - 何之源的文章 - 知乎 https://zhuanlan.zhihu.com/p/27238630
- WBXML 1.3协议摘要
协议地址:WAP195 网络字节顺序:big-endian. 为什么要加0x40? 参考:Compressing XML When an element contains content (t ...
- 网络安全:robots.txt防止向黑客泄露后台地址和隐私目录的写法
做优化的朋友都知道网站的robots的目的是让搜索引擎知道我们网站哪些目录可以收录,哪些目录禁止收录.通常情况蜘蛛访问网站时,会首先检查你的网站根目录是否有robots文件,如果有,则会根据此文件来进 ...
- 435. Non-overlapping Intervals
Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...
- python--自己实现的单链表常用功能
最近一个月,就耗在这上面吧. 很有收获的. # coding = utf-8 # 单向链表 class Node: def __init__(self, new_data): self.data = ...
- ThreadLocal详解,ThreadLocal源码分析,ThreadLocal图解
本文脉路: 概念阐释 ----> 原理图解 ------> 源码分析 ------> 思路整理 ----> 其他补充. 一.概念阐述. ThreadLocal 是一个为 ...
- HDU 1074 Doing Homework(DP状态压缩)
题意:有n门功课需要完成,每一门功课都有时间期限以及你完成所需要的时间,如果完成的时间超出时间期限多少单位,就会被减多少学分,问以怎样的功课完成顺序,会使减掉的学分最少,有多个解时,输出功课名字典序最 ...