首页 > 数据库 >sqlite命令操作数据库

sqlite命令操作数据库

时间:2023-07-31 22:36:42浏览次数:61  
标签:sqlite database 数据库 命令 command mode sqlite3 table


Command Line Shell For SQLite

一、sqlite命令操作数据库

保证模拟器打开状态

1.cmd命令行 进入android安装目录我的是D:\android-sdk-windows-1.5_r1\tools

2.adb shell 命令打开模拟器上的命令行界面

3.# cd /data/data 进入数据库目录

4.# cd org.yihu

5.# ls ;列出目录下所有文件

6.# sqlite3 8ef43281-246d-4c98-a12d-6e27739d4cf4.db 

sqlite3 8ef43281-246d-4c98-a12d-6e27739d4cf4.db
SQLite version 3.5.9
Enter ".help" for instructions

7.sqlite> .tables    ;//列出数据库数据表
.tables
android_metadata  folders           pending_commands
attachments      

8.sqlite> select id from folders;  ;//查询数据表项
select id from folders;
1
3
4
5

9.具体命令参考下面。


The SQLite library includes a simple command-line utility named sqlite3 (or sqlite3.exe on windows) that allows the user to manually enter and execute SQL commands against an SQLite database. This document provides a brief introduction on how to use the sqlite3

Getting Started

To start the sqlite3 program, just type "sqlite3" followed by the name the file that holds the SQLite database. If the file does not exist, a new one is created automatically. The sqlite3

For example, to create a new SQLite database named "ex1" with a single table named "tbl1", you might do this:

$ sqlite3 ex1 SQLite version 3.6.11 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> create table tbl1(one varchar(10), two smallint); sqlite> insert into tbl1 values('hello!',10); sqlite> insert into tbl1 values('goodbye', 20); sqlite> select * from tbl1;

You can terminate the sqlite3 program by typing your systems End-Of-File character (usually a Control-D). Use the interrupt character (usually a Control-C) to stop a long-running SQL statement.

Make sure you type a semicolon at the end of each SQL command! The sqlite3 program looks for a semicolon to know when your SQL command is complete. If you omit the semicolon, sqlite3 will give you a continuation prompt and wait for you to enter more text to be added to the current SQL command. This feature allows you to enter SQL commands that span multiple lines. For example:

sqlite> CREATE TABLE tbl2 ( ...> f1 varchar(30) primary key, ...> f2 text, ...> f3 real ...> );

Aside: Querying the SQLITE_MASTER table

The database schema in an SQLite database is stored in a special table named "sqlite_master". You can execute "SELECT" statements against the special sqlite_master table just like any other table in an SQLite database. For example:

$ sqlite3 ex1 SQLite version 3.6.11 Enter ".help" for instructions sqlite> select * from sqlite_master;

But you cannot execute DROP TABLE, UPDATE, INSERT or DELETE against the sqlite_master table. The sqlite_master table is updated automatically as you create or drop tables and indices from the database. You can not make manual changes to the sqlite_master table.

The schema for TEMPORARY tables is not stored in the "sqlite_master" table since TEMPORARY tables are not visible to applications other than the application that created the table. The schema for TEMPORARY tables is stored in another special table named "sqlite_temp_master". The "sqlite_temp_master" table is temporary itself.

Special commands to sqlite3

Most of the time, sqlite3 just reads lines of input and passes them on to the SQLite library for execution. But if an input line begins with a dot ("."), then that line is intercepted and interpreted by the sqlite3 program itself. These "dot commands" are typically used to change the output format of queries, or to execute certain prepackaged query statements.

For a listing of the available dot commands, you can enter ".help" at any time. For example:

sqlite> .help

Changing Output Formats

The sqlite3 program is able to show the results of a query in eight different formats: "csv", "column", "html", "insert", "line", "list", "tabs", and "tcl". You can use the ".mode" dot command to switch between these output formats.

The default output mode is "list". In list mode, each record of a query result is written on one line of output and each column within that record is separated by a specific separator string. The default separator is a pipe symbol ("|"). List mode is especially useful when you are going to send the output of a query to another program (such as AWK) for additional processing.

sqlite> .mode list sqlite> select * from tbl1;

You can use the ".separator" dot command to change the separator for list mode. For example, to change the separator to a comma and a space, you could do this:

sqlite> .separator ", " sqlite> select * from tbl1;

In "line" mode, each column in a row of the database is shown on a line by itself. Each line consists of the column name, an equal sign and the column data. Successive records are separated by a blank line. Here is an example of line mode output:

sqlite> .mode line sqlite> select * from tbl1;

In column mode, each record is shown on a separate line with the data aligned in columns. For example:

sqlite> .mode column sqlite> select * from tbl1;

By default, each column is at least 10 characters wide. Data that is too wide to fit in a column is truncated. You can adjust the column widths using the ".width" command. Like this:

sqlite> .width 12 6 sqlite> select * from tbl1;

The ".width" command in the example above sets the width of the first column to 12 and the width of the second column to 6. All other column widths were unaltered. You can gives as many arguments to ".width" as necessary to specify the widths of as many columns as are in your query results.

If you specify a column a width of 0, then the column width is automatically adjusted to be the maximum of three numbers: 10, the width of the header, and the width of the first row of data. This makes the column width self-adjusting. The default width setting for every column is this auto-adjusting 0 value.

The column labels that appear on the first two lines of output can be turned on and off using the ".header" dot command. In the examples above, the column labels are on. To turn them off you could do this:

sqlite> .header off sqlite> select * from tbl1;

Another useful output mode is "insert". In insert mode, the output is formatted to look like SQL INSERT statements. You can use insert mode to generate text that can later be used to input data into a different database.

When specifying insert mode, you have to give an extra argument which is the name of the table to be inserted into. For example:

sqlite> .mode insert new_table sqlite> select * from tbl1;

The last output mode is "html". In this mode, sqlite3 writes the results of the query as an XHTML table. The beginning <TABLE> and the ending </TABLE> are not written, but all of the intervening <TR>s, <TH>s, and <TD>s are. The html output mode is envisioned as being useful for CGI.

Writing results to a file

By default, sqlite3 sends query results to standard output. You can change this using the ".output" command. Just put the name of an output file as an argument to the .output command and all subsequent query results will be written to that file. Use ".output stdout" to begin writing to standard output again. For example:

sqlite> .mode list sqlite> .separator | sqlite> .output test_file_1.txt sqlite> select * from tbl1; sqlite> .exit $ cat test_file_1.txt

Querying the database schema

The sqlite3 program provides several convenience commands that are useful for looking at the schema of the database. There is nothing that these commands do that cannot be done by some other means. These commands are provided purely as a shortcut.

For example, to see a list of the tables in the database, you can enter ".tables".

sqlite> .tables

The ".tables" command is similar to setting list mode then executing the following query:

SELECT name FROM sqlite_master WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' UNION ALL SELECT name FROM sqlite_temp_master WHERE type IN ('table','view') ORDER BY 1

In fact, if you look at the source code to the sqlite3 program (found in the source tree in the file src/shell.c) you'll find exactly the above query.

The ".indices" command works in a similar way to list all of the indices for a particular table. The ".indices" command takes a single argument which is the name of the table for which the indices are desired. Last, but not least, is the ".schema" command. With no arguments, the ".schema" command shows the original CREATE TABLE and CREATE INDEX statements that were used to build the current database. If you give the name of a table to ".schema", it shows the original CREATE statement used to make that table and all if its indices. We have:

sqlite> .schema create table tbl1(one varchar(10), two smallint) CREATE TABLE tbl2 ( f1 varchar(30) primary key, f2 text, f3 real ) sqlite> .schema tbl2

The ".schema" command accomplishes the same thing as setting list mode, then entering the following query:

SELECT sql FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type!='meta' ORDER BY tbl_name, type DESC, name

Or, if you give an argument to ".schema" because you only want the schema for a single table, the query looks like this:

SELECT sql FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%' ORDER BY substr(type,2,1), name

You can supply an argument to the .schema command. If you do, the query looks like this:

SELECT sql FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE tbl_name LIKE '%s' AND type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%' ORDER BY substr(type,2,1), name

The "%s" in the query is replace by your argument. This allows you to view the schema for some subset of the database.

sqlite> .schema �c%

Along these same lines, the ".table" command also accepts a pattern as its first argument. If you give an argument to the .table command, a "%" is both appended and prepended and a LIKE clause is added to the query. This allows you to list only those tables that match a particular pattern.

The ".databases" command shows a list of all databases open in the current connection. There will always be at least 2. The first one is "main", the original database opened. The second is "temp", the database used for temporary tables. There may be additional databases listed for databases attached using the ATTACH statement. The first output column is the name the database is attached with, and the second column is the filename of the external file.

sqlite> .databases

Converting An Entire Database To An ASCII Text File

Use the ".dump" command to convert the entire contents of a database into a single ASCII text file. This file can be converted back into a database by piping it back into sqlite3.

A good way to make an archival copy of a database is this:

$ echo '.dump' | sqlite3 ex1 | gzip -c >ex1.dump.gz

This generates a file named ex1.dump.gz

$ zcat ex1.dump.gz | sqlite3 ex2

The text format is pure SQL so you can also use the .dump command to export an SQLite database into other popular SQL database engines. Like this:

$ createdb ex2 $ sqlite3 ex1 .dump | psql ex2

Other Dot Commands

The ".explain" dot command can be used to set the output mode to "column" and to set the column widths to values that are reasonable for looking at the output of an EXPLAIN command. The EXPLAIN command is an SQLite-specific SQL extension that is useful for debugging. If any regular SQL is prefaced by EXPLAIN, then the SQL command is parsed and analyzed but is not executed. Instead, the sequence of virtual machine instructions that would have been used to execute the SQL command are returned like a query result. For example:

sqlite> .explain sqlite> explain delete from tbl1 where two<20;

The ".timeout" command sets the amount of time that the sqlite3

And finally, we mention the ".exit" command which causes the sqlite3 program to exit.

Using sqlite3 in a shell script

One way to use sqlite3 in a shell script is to use "echo" or "cat" to generate a sequence of commands in a file, then invoke sqlite3 while redirecting input from the generated command file. This works fine and is appropriate in many circumstances. But as an added convenience, sqlite3 allows a single SQL command to be entered on the command line as a second argument after the database name. When the sqlite3 program is launched with two arguments, the second argument is passed to the SQLite library for processing, the query results are printed on standard output in list mode, and the program exits. This mechanism is designed to make sqlite3 easy to use in conjunction with programs like "awk". For example:

$ sqlite3 ex1 'select * from tbl1' | > awk '{printf "<tr><td>%s<td>%s\n",$1,$2 }'

Ending shell commands

SQLite commands are normally terminated by a semicolon. In the shell you can also use the word "GO" (case-insensitive) or a slash character "/" on a line by itself to end a command. These are used by SQL Server and Oracle, respectively. These won't work in sqlite3_exec(), because the shell translates these into a semicolon before passing them to that function.

Compiling the sqlite3 program from sources

The source code to the sqlite3 command line interface is in a single file named "shell.c" which you can download from the SQLite website. Compile this file (together with the sqlite3 library source code to generate the executable. For example:

gcc -o sqlite3 shell.c sqlite3.c -ldl -lpthread

http://www.sqlite.org/sqlite.html

标签:sqlite,database,数据库,命令,command,mode,sqlite3,table
From: https://blog.51cto.com/u_3124497/6914115

相关文章

  • 【已解决】如果将MySQL数据库中的表生成PDM
    数据库表PDM关系图                        | 原创作者/编辑:凯哥Java                                    | 分类:经验分享有时候,我们需要MySQL数据库中的表生成对应的PDM文件,这里凯哥就讲讲第一种将MySQL数据库......
  • 【已解决】如果将MySQL数据库中的表生成PDM
     数据库表PDM关系图                                                        | 原创作者/编辑:凯哥Java                                     | 分类:经验分享 有......
  • 6、Mysql操作数据库以及数据表
    学习sql规则,可以让mysql服务器帮咱们做其他操作1、操作数据库(文件夹)createdatabase数据库名defaultcharsetutf8;表示整个数据库是utf8的格式 use数据库名;使用这个数据库 查看数据库showdatabases; 删除数据库dropdatabase数据库名;数据库没有修改这一说......
  • openGauss数据库常用操作命令
    1.以操作系统用户omm登录数据库主节点su-omm1.1启动服务分布式openGauss:gs_om-tstart启动服务gs_om-trestart重启服务集中式openGauss:gs_om-tstop关闭服务gs_om-tstart启动服务1.2使用“gs_om-tstatus–detail”命令查询openGauss各实例状......
  • Docker常用命令
    title:"Docker常用命令"date:2023-07-31T12:05:25+08:00tags:["Linux运维","Docker"]categories:[]draft:falsedockerdockerinfo#docker配置信息dockerinspect$cid#查看容器的配置信息dockerimagesdockerps-adockerrun-it$image_id-......
  • MongoDB数据库的部署和应用
    推荐步骤:在Centos01上部署MongoDB服务器客户端登录验证在centos01的MongoDB配置文件通过配置文件控制MongoDB服务,配置MongoDB身份验证在centos01的MongoDB服务器配置身份验证管理和修改配置文件支持验证在centos01管理MongoDB管理数据,集合批量数据管理实验步骤创建管理MongoDB组和......
  • 数据库三大范式是什么、mysql有哪些索引类型,分别有什么作用、事务的特性和隔离级别
    目录1数据库三大范式是什么2mysql有哪些索引类型,分别有什么作用3事务的特性和隔离级别事务四大特性(ACID)隔离级别--->为了保证四个特性的隔离性而有的1数据库三大范式是什么-https://zhuanlan.zhihu.com/p/618012849-#第一范式:1NF是指数据库表的每一列都是不可分割 -每列......
  • 数据库三大范式&mysql的索引类型和作用&事务的特性和隔离级别
    数据库三大范式&mysql的索引类型和作用&事务的特性和隔离级别数据库三大范式第一范式#数据库表的每一列都是不可分割的基本数据-每列的值具有原子性,不可再分割-每个字段的值都只能是单一值举例:学籍信息不符合第一范式,可以继续分割第二范式#在第一范式的基础上-如果......
  • 008、adb命令 (service call iphonesubinfo 15),获取手机号码
    获取手机号码命令, adb shell service call iphonesubinfo 15 如下:不同android版本15的数字不一样,可是是13或17等C:\Users\SZ-M1-BD-0080>adbshellservicecalliphonesubinfo15Result:Parcel(0x00000000:000000000000000e0038002b00310036'..............
  • JDBC p5 数据库连接池
    数据库连接池传统获取Connection问题分析传统的JDBC数据库使用DriverManager来获取,每次向数据库建立连接的时候都要将Connection加载到内存中,再验证IP地址,用户名和密码(0.05~1s时间)。需要数据库连接的时候,就向数据库要求一个,频繁的进行数据库连接操作将占用很多的系统......