- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
The sqlite3 command line tool
In this part of the SQLite tutorial, we cover the sqlite3
command line tool.
The sqlite3 tool
The sqlite3
tool is a terminal based frontend to the SQLite library that can evaluate queries interactively and display the results in multiple formats. It can also be used within scripts.
On the terminal screen, we see the following prompt of the sqlite3
tool:
$ sqlite3 test.db SQLite version 3.7.13 2012-06-11 02:05:22 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite>
The .help
command is one of the meta commands of the sqlite3
tool. It lists all meta commands. The .exit
and the .quit
commands exit the sqlite3
session. We can also use the Ctrl+D key combination to quit sqlite3
. The .databases
command shows the attached databases. The .tables
command lists the available tables.
Creating a database
The complete SQLite database is stored in a single cross-platform disk file. We use the sqlite3
command line tool to create a new database file.
$ sqlite3 test.db
Here we create a new test.db
database. If the file exists, it is opened.
Basic sqlite3 meta commands
Next we describe some of the meta commands of the sqlite3
tool.
sqlite> .tables Books Customers Log Orders Testing Cars Friends Names Reservations
The .tables
command shows the available tables.
sqlite> SELECT * FROM Friends; 1|Jane|F 2|Thomas|M 3|Frank|M 4|Elisabeth|F 5|Mary|F 6|Lucy|F 7|Jack|M
Here we get the output of a SELECT
statement. By default, the output mode is line
and the separator is |
.
sqlite> .separator : sqlite> SELECT * FROM Friends; 1:Jane:F 2:Thomas:M 3:Frank:M 4:Elisabeth:F 5:Mary:F 6:Lucy:F 7:Jack:M
Here we have used a new colon separator.
There are several other output modes available. The following example shows the column
output mode.
sqlite> .mode column sqlite> .headers on sqlite> SELECT * FROM Friends; Id Name Sex ---------- ---------- ---------- 1 Jane F 2 Thomas M 3 Frank M 4 Elisabeth F 5 Mary F 6 Lucy F 7 Jack M
This example shows how data is formatted in sqlite's column mode. The .headers
command has also been used to show column headers. By default, the headers are hidden.
The .width
command adjusts the size of the columns. (This meta command is relevant only for tables in the column mode.)
sqlite> SELECT Name, Title FROM Authors NATURAL JOIN Books; Name Title ----------- ---------- Jane Austen Emma Leo Tolstoy War and Pe Joseph Hell Catch XII Charles Dic David Copp Joseph Hell Good as Go Leo Tolstoy Anna Karen
The column widths are not wide enough to display all data correctly.
sqlite> .width 15 18 sqlite> SELECT Name, Title FROM Authors NATURAL JOIN Books; Name Title --------------- ------------------ Jane Austen Emma Leo Tolstoy War and Peace Joseph Heller Catch XII Charles Dickens David Copperfield Joseph Heller Good as Gold Leo Tolstoy Anna Karenia
Here we change the column widths. The first column will be 15 characters wide, the second 18.
sqlite> .show echo: off explain: off headers: on mode: column nullvalue: "NULL" output: stdout separator: "|" stats: off width: 15 18
The .show
command lists various settings. These include the output mode, the separator used in the list mode, and whether the headers are on.
sqlite> .schema Cars CREATE TABLE Cars(Id INTEGER PRIMARY KEY, Name TEXT, Price INTEGER);
The .schema
command shows the structure of the table. It gives the DDL SQL to create the table.
It is possible to change the prompt of the sqlite3
with the .prompt
command.
sqlite> .prompt "> " ". " > SELECT * FROM Cars . LIMIT 2; Id Name Price ---------- ---------- ---------- 1 Audi 52642 2 Mercedes 57127 >
There are two prompts. One is the main prompt and the other is a continuation prompt. The default main prompt is sqlite>
and the default continuation prompt is ...>
.
Executing SQL from the shell
We can execute SQL commands from the shell.
$ sqlite3 test.db "SELECT * FROM Cars;" 1|Audi|52642 2|Mercedes|57127 3|Skoda|9000 4|Volvo|29000 5|Bentley|350000 6|Citroen|21000 7|Hummer|41400 8|Volkswagen|21600
Here we have non-interactively executed a SELECT
statement; all cars from the Cars
table were selected.
Dumping tables
It is possible to dump tables in SQL format to the disk. This way we can easily save the structure and the data of a database table.
We have the Cars table.
sqlite> SELECT * FROM Cars; Id Name Price ---------- ---------- ---------- 1 Audi 52642 2 Mercedes 57127 3 Skoda 9000 4 Volvo 29000 5 Bentley 350000 6 Citroen 21000 7 Hummer 41400 8 Volkswagen 21600
We are going to use the .dump
command to dump the table.
sqlite> .dump Cars PRAGMA foreign_keys=OFF; BEGIN TRANSACTION; CREATE TABLE Cars(Id INTEGER PRIMARY KEY, Name TEXT, Price 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;
The .dump
command shows us the SQL necessary to create the Cars
table.
sqlite> .output cars2.sql sqlite> .dump Cars
We can also redirect the output to a file. The .output
command will redirect the output to the cars2.sql
file.
$ cat cars2.sql PRAGMA foreign_keys=OFF; BEGIN TRANSACTION; CREATE TABLE Cars(Id INTEGER PRIMARY KEY, Name TEXT, Price INTEGER); INSERT INTO "Cars" VALUES(1,'Audi',52642); ...
We show the contents of the cars2.sql
file with the cat
command.
Reading SQL
We can read SQL statements from a file name with the .read
command.
sqlite> .tables Cars Cars sqlite> DROP TABLE Cars; sqlite> .tables Cars sqlite> .read cars.sql sqlite> .tables Cars Cars sqlite> SELECT * FROM Cars WHERE Id=1; Id Name Price ---------- ---------- ---------- 1 Audi 52642
Here we have executed a series of commands. We drop the table and read it from cars.sql
.
The .sqlite_history file
The commands and statements are archived in the .sqlite_history
file. The file is located in the home directory.
$ tail -5 ~/.sqlite_history SELECT * FROM Cars LIMIT 2; .help .tables .echo ON SELECT * FROM Cars;
Using the tail
command, we show the last five entries.
Resource file
The sqlite3
tool has a resource file called .sqliterc
. It is located in the home directory. If there is no such file, we can simply create it. The resouce file can contain meta commands, or regular SQL statements. However, we should avoid using SQL in the file.
$ cat .sqliterc .mode column .header on .nullvalue NULL
Here is a simple example of a resource file. It has three meta commands. With a resource file, we do not have to execute meta commands all over again when we start the sqlite3
tool. They will be executed automatically at the start of the tool.
$ sqlite3 test.db -- Loading resources from /home/janbodnar/.sqliterc SQLite version 3.7.7 2011-06-23 19:49:22 Enter ".help" for instructions Enter SQL statements terminated with a ";"
We have a message saying that the tool loaded resources upon the beginning.
Command line options
The tool has several command line options. They mostly duplicate the meta commands. Note that command line options overwrite the resource file meta commands.
$ sqlite3 --help Usage: sqlite3 [OPTIONS] FILENAME [SQL] FILENAME is the name of an SQLite database. A new database is created if the file does not previously exist. OPTIONS include: -bail stop after hitting an error -batch force batch I/O -column set output mode to 'column' -cmd command run "command" before reading stdin -csv set output mode to 'csv' -echo print commands before execution -init filename read/process named file -[no]header turn headers on or off -help show this message -html set output mode to HTML -interactive force interactive I/O -line set output mode to 'line' -list set output mode to 'list' -nullvalue 'text' set text string for NULL values -separator 'x' set output field separator (|) -stats print memory stats before each finalize -version show SQLite version -vfs NAME use NAME as the default VFS
The -help
options gives us the list of all available options with a brief description.
$ sqlite3 -echo -line -noheader test.db -- Loading resources from /home/janbodnar/.sqliterc SQLite version 3.7.13 2012-06-11 02:05:22 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> SELECT * FROM Cars LIMIT 2; SELECT * FROM Cars LIMIT 2; Id = 1 Name = Audi Price = 52642 Id = 2 Name = Mercedes Price = 57127
We start the sqlite3
tool with -echo
, -line
, and -noheader
options. The SELECT
statement is repeated/echoed after being launched. The ouput is in the line mode and we have no headers displayed.
$ sqlite3 -version -- Loading resources from /home/janbodnar/.sqliterc 3.7.13 2012-06-11 02:05:22 f5b5a13f7394dc143aa136f1d4faba6839eaa6dc
With the -version
option we get the sqlite3's version.
$ sqlite3 -html test.db -- Loading resources from /home/janbodnar/.sqliterc SQLite version 3.7.13 2012-06-11 02:05:22 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> SELECT * FROM Cars LIMIT 2; <TR><TH>Id</TH> <TH>Name</TH> <TH>Price</TH> </TR> <TR><TD>1</TD> <TD>Audi</TD> <TD>52642</TD> </TR> <TR><TD>2</TD> <TD>Mercedes</TD> <TD>57127</TD> </TR>
With the -html
option, we can output the results as simple HTML tables.
In this part of the SQLite tutorial, we worked with the sqlite3
command line tool. We have described various meta commands, shown how to dump tables, and read SQL from files. We described the sqlite's resource file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论