数据库学习_sqlite3概念和基本操作

Posted Leslie X徐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库学习_sqlite3概念和基本操作相关的知识,希望对你有一定的参考价值。

sqlite3

安装

sqlite3安装:

sudo apt-get install sqlite3

若出错则:
sudo dpkg --purge --force-depends libsqlite3-0
sudo apt-get install -f
sudo apt-get install libsqlite3-0
sudo apt-get install libsqlite3-dev
sudo apt-get install sqlite3

数据库的基本元素

元素意义
数据库一个文件
文件里的一个表格
字段列名
记录行内容

创建数据库

  1. 帮助
$ sqlite3 mydata.db
>.help  #查看帮助
.auth ON|OFF           Show authorizer callbacks
.backup ?DB? FILE      Backup DB (default "main") to FILE
.bail on|off           Stop after hitting an error.  Default OFF
.binary on|off         Turn binary output on or off.  Default OFF
.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
.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
.exit                  Exit this program
.explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic
.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.
.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 .separator strings
                         quote    Escape answers as for SQL
                         tabs     Tab-separated values
                         tcl      TCL list elements
.nullvalue STRING      Use STRING in place of NULL values
.once FILENAME         Output for the next SQL command only to FILENAME
.open ?--new? ?FILE?   Close existing database and reopen FILE
                         The --new starts with an empty file
.output ?FILENAME?     Send output to FILENAME 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
.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
.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

主要有:
".help"帮助,
".quit"退出,
".table"查看当前表
“.schma”

  1. 创建表——create table …
sqlite>create table student(ID integer,Name text,Age integer);
  1. 保存数据库
sqlite>; #通过一个";"保存
  1. 查看表项——schema …
sqlite>schema student
CREATE TABLE student(ID integer,Name text,Age integer);

  1. 插入表记录——insert into …(…) values(…)
sqlite> insert into student(ID,Name,Age) values(1,"徐文涵",25);

  1. 查看插入的记录——select … from …
sqlite> select * from student;
1|徐文涵|25

  1. 插入字段(增加列)——alter table … add column …
sqlite> alter table student add column sex char(2);

  1. 更新记录——update … … where …
sqlite> update student set sex='M' where ID=1;
sqlite> select * from student;
1|徐文涵|25|M
2|王永先|24|
sqlite> update student set sex='F' where ID=2;
sqlite> select * from student;
1|徐文涵|25|M
2|王永先|24|F

  1. 删除记录——delete from … where …
sqlite> delete from student where ID=2;

  1. 删除一个表——drop table …;
sqlite> drop table student;
  1. 排序——order by … asc(升序)/desc(降序);
sqlite> select * from student order by Age asc;
4|王|12|
2|王永先|24|
1|徐文涵|25|M
3|王永|29|

以上是关于数据库学习_sqlite3概念和基本操作的主要内容,如果未能解决你的问题,请参考以下文章

Linux网络学习_网络的基本概念

Android 逆向使用 DB Browser 查看并修改 SQLite 数据库 ( 从 Android 应用数据目录中拷贝数据库文件 | 使用 DB Browser 工具查看数据块文件 )(代码片段

Android 逆向使用 DB Browser 查看并修改 SQLite 数据库 ( 从 Android 应用数据目录中拷贝数据库文件 | 使用 DB Browser 工具查看数据块文件 )(代码片段

简易SQLite3数据库学习

python学习7_1面向对象基本概念和用法

Python学习笔记_01:基本概念介绍