SlideShare a Scribd company logo
The sqlite3 command line tool

In this part of the SQLite tutorial, we will cover the sqlite3 command line tool.


The manual describes the sqlite3 as follows: sqlite3 is a terminal-based front-end to the SQLite library

that can evaluate queries interactively and display the results in multiple for‐ mats. sqlite3 can also be

used within shell scripts and other applica‐ tions to provide batch processing features.




sqlite3 tool

sqlite3 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.5.9




  Enter ".help" for instructions




  sqlite>


The .help is one of the meta commnads of the sqlite3 tool. It shows a list of them. The .exit and the

The .quit commands exit the sqlite3 session. 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 movies.db


Here we create a new movies.db database. If the exists, it is opened.




Basic sqlite3 meta commnads

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 commnad now shows the available tables.




  sqlite> SELECT * FROM Names;




  1|Tom




  2|Lucy




  3|Frank




  4|Jane




  5|Robert


Here we get the output of a simple SELECT statement. By default, the output mode is line and the

separator is |.
sqlite> .separator :




  sqlite> SELECT * FROM Names;




  1:Tom




  2:Lucy




  3:Frank




  4:Jane




  5:Robert


Here we have used a new colon separator.


There are several other output modes available. The following example will show the column output

mode.




  sqlite> .mode column




  sqlite> .headers on




  sqlite> SELECT * FROM Names;




  Id            Name




  ----------    ----------




  1             Tom
2              Lucy




  3              Frank




  4              Jane




  5              Robert


In this example, we have our data in the column mode. Plus we show the column headers with the

.headers command. By default, the headers are hidden.


The .width command adjusts the size of the columns.




  sqlite> SELECT Title, Author FROM Books;




  Title             Author




  -------------     -----------




  War and Peace     Leo Tolstoy




  The Brothers      Fyodor Dost




  Crime and Pun     Fyodor Dost


Here, the column widths are not wide enough to display all data correctly.




  sqlite> .width 22, 18




  sqlite> SELECT Title, Author FROM Books;




  Title                        Author
----------------------      ------------------




  War and Peace               Leo Tolstoy




  The Brothers Karamazov      Fyodor Dostoyevsky




  Crime and Punishment        Fyodor Dostoyevsky


Here we change the column widths. The first column will be 22 characters wide, the second 18.




  sqlite> .show




        echo: off




    explain: off




    headers: off




        mode: list




  nullvalue: ""




     output: stdout




  separator: "|"




       width:


The .show command lists various settings. We can see the output mode, the separator used in the list

mode, the headers.
sqlite> .schema Cars




  CREATE TABLE Cars(Id integer primary key, Name text, Cost integer);


The .schema command shows the structure of the table. It gives the DDL SQL to create the table.




Executing SQL from the shell

We can execute SQL commands from the shell.




  $ sqlite3 test.db "SELECT * FROM Cars;"




  Id            Name          Cost




  ----------    ----------    ----------




  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 SQL command. We selected all cars from the Cars

table.




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           Cost




  ----------     ----------     ----------




  1              Audi           52642




  2              Mercedes       57127




  3              Skoda          9000




  4              Volvo          29000




  5              Bentley        350000




  6              Citroen        21000




  7              Hummer         41400
8            Volkswagen    21600


Now, we are going to use the .dump command to dump the table.




  sqlite> .dump Cars




  BEGIN TRANSACTION;




  CREATE TABLE Cars(Id integer primary key, Name text, Cost 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 recreate the table.




  sqlite> .output cars.sql
sqlite> .dump Cars


We can also redirect the output to a file. The .output command will redirect the output to the cars.sql

file.




   $ cat cars.sql




   BEGIN TRANSACTION;




   CREATE TABLE Cars(Id integer primary key, Name text, Cost integer);




   INSERT INTO "Cars" VALUES(1,'Audi',52642);




   ...


We verify it.




Reading SQL

We can read SQL from a file name with the .read commnad.




   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           Cost




  ----------      ----------     ----------




  1               Audi           52642


Here we have executed a series of commands. We drop the table and read it from the cars.sql, that we

have created previously.




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. We can place the meta commnads there or the regular SQL

statements. However, we should avoid using SQL in the resource file.




  $ cat .sqliterc




  .mode column




  .header on




  .nullvalue NULL


Here is a simple example of a resource file. It has three meta commands. With resource file, we don't

have to execute meta commnads 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/vronskij/.sqliterc




  SQLite version 3.5.9




  Enter ".help" for instructions


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

commnad line options overwrite the resource file meta commands.




  $ sqlite3 -version




  -- Loading resources from /home/vronskij/.sqliterc




  3.5.9


We get the sqlite3 version.




  $ sqlite3 -html test.db




  -- Loading resources from /home/vronskij/.sqliterc




  SQLite version 3.5.9




  Enter ".help" for instructions
sqlite> SELECT * FROM Cars LIMIT 2;




  <TR><TH>Id</TH><TH>Name</TH><TH>Cost</TH></TR>




  <TR><TD>1</TD>




  <TD>Audi</TD>




  <TD>52642</TD>




  </TR>




  <TR><TD>2</TD>




  <TD>Mercedes</TD>




  <TD>57127</TD>




  </TR>




The -html option causes the results to be output as simple HTML tables


In this part of the SQLite tutorial, we worked with the sqlite3 command line tool. We have described

various meta commnads. We have shown, how to dump tables, read SQL from files; we described the

sqlite's resource file.

More Related Content

What's hot

235689260 oracle-forms-10g-tutorial
235689260 oracle-forms-10g-tutorial235689260 oracle-forms-10g-tutorial
235689260 oracle-forms-10g-tutorial
homeworkping3
 
Sql Injection
Sql Injection Sql Injection
Sql Injection
Sanjeev Kumar Jaiswal
 
Oracle: PLSQL Commands
Oracle: PLSQL CommandsOracle: PLSQL Commands
Oracle: PLSQL Commands
DataminingTools Inc
 
CIS 336 Education guide/Tutorialrank.com
CIS 336 Education guide/Tutorialrank.comCIS 336 Education guide/Tutorialrank.com
CIS 336 Education guide/Tutorialrank.com
tyjhgfsdfgh
 
My sql with querys
My sql with querysMy sql with querys
My sql with querys
NIRMAL FELIX
 
MYSQL
MYSQLMYSQL
Sql injections
Sql injectionsSql injections
Sql injections
Manish Kushwaha
 
Instalacion ambiente web am en linux red hat
Instalacion ambiente web am en linux red hatInstalacion ambiente web am en linux red hat
Instalacion ambiente web am en linux red hat
Janneth Parra
 
Les12[1]Creating Views
Les12[1]Creating ViewsLes12[1]Creating Views
Les12[1]Creating Views
siavosh kaviani
 
Les01[1]Writing Basic SQL Statements
Les01[1]Writing Basic SQL StatementsLes01[1]Writing Basic SQL Statements
Les01[1]Writing Basic SQL Statements
siavosh kaviani
 
Advanced tips of dbms statas
Advanced tips of dbms statasAdvanced tips of dbms statas
Advanced tips of dbms statas
Louis liu
 
Les06[1]Subqueries
Les06[1]SubqueriesLes06[1]Subqueries
Les06[1]Subqueries
siavosh kaviani
 
Oracle Web Adi For upload item master
Oracle Web Adi For upload item masterOracle Web Adi For upload item master
Oracle Web Adi For upload item master
Ahmed Elshayeb
 
Exportrows
ExportrowsExportrows
Exportrows
oracle documents
 
Explain
ExplainExplain
database-querry-student-note
database-querry-student-notedatabase-querry-student-note
database-querry-student-note
Leerpiny Makouach
 
Hbase interact with shell
Hbase interact with shellHbase interact with shell
Hbase interact with shell
Shashwat Shriparv
 
Connor McDonald 11g for developers
Connor McDonald 11g for developersConnor McDonald 11g for developers
Connor McDonald 11g for developers
InSync Conference
 

What's hot (18)

235689260 oracle-forms-10g-tutorial
235689260 oracle-forms-10g-tutorial235689260 oracle-forms-10g-tutorial
235689260 oracle-forms-10g-tutorial
 
Sql Injection
Sql Injection Sql Injection
Sql Injection
 
Oracle: PLSQL Commands
Oracle: PLSQL CommandsOracle: PLSQL Commands
Oracle: PLSQL Commands
 
CIS 336 Education guide/Tutorialrank.com
CIS 336 Education guide/Tutorialrank.comCIS 336 Education guide/Tutorialrank.com
CIS 336 Education guide/Tutorialrank.com
 
My sql with querys
My sql with querysMy sql with querys
My sql with querys
 
MYSQL
MYSQLMYSQL
MYSQL
 
Sql injections
Sql injectionsSql injections
Sql injections
 
Instalacion ambiente web am en linux red hat
Instalacion ambiente web am en linux red hatInstalacion ambiente web am en linux red hat
Instalacion ambiente web am en linux red hat
 
Les12[1]Creating Views
Les12[1]Creating ViewsLes12[1]Creating Views
Les12[1]Creating Views
 
Les01[1]Writing Basic SQL Statements
Les01[1]Writing Basic SQL StatementsLes01[1]Writing Basic SQL Statements
Les01[1]Writing Basic SQL Statements
 
Advanced tips of dbms statas
Advanced tips of dbms statasAdvanced tips of dbms statas
Advanced tips of dbms statas
 
Les06[1]Subqueries
Les06[1]SubqueriesLes06[1]Subqueries
Les06[1]Subqueries
 
Oracle Web Adi For upload item master
Oracle Web Adi For upload item masterOracle Web Adi For upload item master
Oracle Web Adi For upload item master
 
Exportrows
ExportrowsExportrows
Exportrows
 
Explain
ExplainExplain
Explain
 
database-querry-student-note
database-querry-student-notedatabase-querry-student-note
database-querry-student-note
 
Hbase interact with shell
Hbase interact with shellHbase interact with shell
Hbase interact with shell
 
Connor McDonald 11g for developers
Connor McDonald 11g for developersConnor McDonald 11g for developers
Connor McDonald 11g for developers
 

Viewers also liked

Java one 2010
Java one 2010Java one 2010
Java one 2010
scdn
 
SCDN 1
SCDN 1SCDN 1
SCDN 1
scdn
 
第5回SCDN - Things that become possible with HTML5
第5回SCDN - Things that become possible with HTML5第5回SCDN - Things that become possible with HTML5
第5回SCDN - Things that become possible with HTML5
scdn
 
Introduction to sq lite
Introduction to sq liteIntroduction to sq lite
Introduction to sq lite
punu_82
 
Creating, altering and dropping tables
Creating, altering and dropping tablesCreating, altering and dropping tables
Creating, altering and dropping tables
punu_82
 
мясной дом Бородина.
мясной дом Бородина.мясной дом Бородина.
мясной дом Бородина.
Санечка Бравова
 

Viewers also liked (6)

Java one 2010
Java one 2010Java one 2010
Java one 2010
 
SCDN 1
SCDN 1SCDN 1
SCDN 1
 
第5回SCDN - Things that become possible with HTML5
第5回SCDN - Things that become possible with HTML5第5回SCDN - Things that become possible with HTML5
第5回SCDN - Things that become possible with HTML5
 
Introduction to sq lite
Introduction to sq liteIntroduction to sq lite
Introduction to sq lite
 
Creating, altering and dropping tables
Creating, altering and dropping tablesCreating, altering and dropping tables
Creating, altering and dropping tables
 
мясной дом Бородина.
мясной дом Бородина.мясной дом Бородина.
мясной дом Бородина.
 

Similar to The sqlite3 commnad line tool

Sq lite python tutorial sqlite programming in python
Sq lite python tutorial   sqlite programming in pythonSq lite python tutorial   sqlite programming in python
Sq lite python tutorial sqlite programming in python
Martin Soria
 
Sqlite3 command reference
Sqlite3 command referenceSqlite3 command reference
Sqlite3 command reference
Raghu nath
 
DEF CON 27 -OMER GULL - select code execution from using sq lite
DEF CON 27 -OMER GULL - select code execution from using sq liteDEF CON 27 -OMER GULL - select code execution from using sq lite
DEF CON 27 -OMER GULL - select code execution from using sq lite
Felipe Prado
 
Introduction to Threading in .Net
Introduction to Threading in .NetIntroduction to Threading in .Net
Introduction to Threading in .Net
webhostingguy
 
JDBC
JDBCJDBC
JDBC
Sunil OS
 
MySQL and its basic commands
MySQL and its basic commandsMySQL and its basic commands
MySQL and its basic commands
Bwsrang Basumatary
 
mysqlanditsbasiccommands-150226033905-conversion-gate02.pdf
mysqlanditsbasiccommands-150226033905-conversion-gate02.pdfmysqlanditsbasiccommands-150226033905-conversion-gate02.pdf
mysqlanditsbasiccommands-150226033905-conversion-gate02.pdf
pradnyamulay
 
SQLQueries
SQLQueriesSQLQueries
SQLQueries
karunakar81987
 
Sq lite expressions
Sq lite expressionsSq lite expressions
Sq lite expressions
punu_82
 
MySql slides (ppt)
MySql slides (ppt)MySql slides (ppt)
MySql slides (ppt)
webhostingguy
 
[Www.pkbulk.blogspot.com]dbms07
[Www.pkbulk.blogspot.com]dbms07[Www.pkbulk.blogspot.com]dbms07
[Www.pkbulk.blogspot.com]dbms07
AnusAhmad
 
The select statement
The select statementThe select statement
The select statement
punu_82
 
Introduction4 SQLite
Introduction4 SQLiteIntroduction4 SQLite
Introduction4 SQLite
Stanley Huang
 
chapter 8 SQL.ppt
chapter 8 SQL.pptchapter 8 SQL.ppt
chapter 8 SQL.ppt
YitbarekMurche
 
The ultimate-guide-to-sql
The ultimate-guide-to-sqlThe ultimate-guide-to-sql
The ultimate-guide-to-sql
McNamaraChiwaye
 
SQL (1).pptx
SQL (1).pptxSQL (1).pptx
SQL (1).pptx
AdnanHaque6
 
Database Connectivity with JDBC
Database Connectivity with JDBCDatabase Connectivity with JDBC
Database Connectivity with JDBC
Dudy Ali
 
Dbms lab Manual
Dbms lab ManualDbms lab Manual
Dbms lab Manual
Vivek Kumar Sinha
 
Oracle training in hyderabad
Oracle training in hyderabadOracle training in hyderabad
Oracle training in hyderabad
Kelly Technologies
 
android sqlite
android sqliteandroid sqlite
android sqlite
Deepa Rani
 

Similar to The sqlite3 commnad line tool (20)

Sq lite python tutorial sqlite programming in python
Sq lite python tutorial   sqlite programming in pythonSq lite python tutorial   sqlite programming in python
Sq lite python tutorial sqlite programming in python
 
Sqlite3 command reference
Sqlite3 command referenceSqlite3 command reference
Sqlite3 command reference
 
DEF CON 27 -OMER GULL - select code execution from using sq lite
DEF CON 27 -OMER GULL - select code execution from using sq liteDEF CON 27 -OMER GULL - select code execution from using sq lite
DEF CON 27 -OMER GULL - select code execution from using sq lite
 
Introduction to Threading in .Net
Introduction to Threading in .NetIntroduction to Threading in .Net
Introduction to Threading in .Net
 
JDBC
JDBCJDBC
JDBC
 
MySQL and its basic commands
MySQL and its basic commandsMySQL and its basic commands
MySQL and its basic commands
 
mysqlanditsbasiccommands-150226033905-conversion-gate02.pdf
mysqlanditsbasiccommands-150226033905-conversion-gate02.pdfmysqlanditsbasiccommands-150226033905-conversion-gate02.pdf
mysqlanditsbasiccommands-150226033905-conversion-gate02.pdf
 
SQLQueries
SQLQueriesSQLQueries
SQLQueries
 
Sq lite expressions
Sq lite expressionsSq lite expressions
Sq lite expressions
 
MySql slides (ppt)
MySql slides (ppt)MySql slides (ppt)
MySql slides (ppt)
 
[Www.pkbulk.blogspot.com]dbms07
[Www.pkbulk.blogspot.com]dbms07[Www.pkbulk.blogspot.com]dbms07
[Www.pkbulk.blogspot.com]dbms07
 
The select statement
The select statementThe select statement
The select statement
 
Introduction4 SQLite
Introduction4 SQLiteIntroduction4 SQLite
Introduction4 SQLite
 
chapter 8 SQL.ppt
chapter 8 SQL.pptchapter 8 SQL.ppt
chapter 8 SQL.ppt
 
The ultimate-guide-to-sql
The ultimate-guide-to-sqlThe ultimate-guide-to-sql
The ultimate-guide-to-sql
 
SQL (1).pptx
SQL (1).pptxSQL (1).pptx
SQL (1).pptx
 
Database Connectivity with JDBC
Database Connectivity with JDBCDatabase Connectivity with JDBC
Database Connectivity with JDBC
 
Dbms lab Manual
Dbms lab ManualDbms lab Manual
Dbms lab Manual
 
Oracle training in hyderabad
Oracle training in hyderabadOracle training in hyderabad
Oracle training in hyderabad
 
android sqlite
android sqliteandroid sqlite
android sqlite
 

Recently uploaded

JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
Miro Wengner
 
Getting the Most Out of ScyllaDB Monitoring: ShareChat's Tips
Getting the Most Out of ScyllaDB Monitoring: ShareChat's TipsGetting the Most Out of ScyllaDB Monitoring: ShareChat's Tips
Getting the Most Out of ScyllaDB Monitoring: ShareChat's Tips
ScyllaDB
 
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeckPoznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
FilipTomaszewski5
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
DianaGray10
 
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin..."$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
Fwdays
 
Demystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through StorytellingDemystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through Storytelling
Enterprise Knowledge
 
From Natural Language to Structured Solr Queries using LLMs
From Natural Language to Structured Solr Queries using LLMsFrom Natural Language to Structured Solr Queries using LLMs
From Natural Language to Structured Solr Queries using LLMs
Sease
 
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
AlexanderRichford
 
Introducing BoxLang : A new JVM language for productivity and modularity!
Introducing BoxLang : A new JVM language for productivity and modularity!Introducing BoxLang : A new JVM language for productivity and modularity!
Introducing BoxLang : A new JVM language for productivity and modularity!
Ortus Solutions, Corp
 
Northern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving | Modern Metal Trim, Nameplates and Appliance PanelsNorthern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving
 
Lee Barnes - Path to Becoming an Effective Test Automation Engineer.pdf
Lee Barnes - Path to Becoming an Effective Test Automation Engineer.pdfLee Barnes - Path to Becoming an Effective Test Automation Engineer.pdf
Lee Barnes - Path to Becoming an Effective Test Automation Engineer.pdf
leebarnesutopia
 
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
zjhamm304
 
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
DanBrown980551
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
Ivo Velitchkov
 
Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
Safe Software
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
Pablo Gómez Abajo
 
AppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSFAppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSF
Ajin Abraham
 
Dandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity serverDandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity server
Antonios Katsarakis
 
Day 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio FundamentalsDay 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio Fundamentals
UiPathCommunity
 
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
manji sharman06
 

Recently uploaded (20)

JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
 
Getting the Most Out of ScyllaDB Monitoring: ShareChat's Tips
Getting the Most Out of ScyllaDB Monitoring: ShareChat's TipsGetting the Most Out of ScyllaDB Monitoring: ShareChat's Tips
Getting the Most Out of ScyllaDB Monitoring: ShareChat's Tips
 
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeckPoznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
 
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin..."$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
 
Demystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through StorytellingDemystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through Storytelling
 
From Natural Language to Structured Solr Queries using LLMs
From Natural Language to Structured Solr Queries using LLMsFrom Natural Language to Structured Solr Queries using LLMs
From Natural Language to Structured Solr Queries using LLMs
 
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
 
Introducing BoxLang : A new JVM language for productivity and modularity!
Introducing BoxLang : A new JVM language for productivity and modularity!Introducing BoxLang : A new JVM language for productivity and modularity!
Introducing BoxLang : A new JVM language for productivity and modularity!
 
Northern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving | Modern Metal Trim, Nameplates and Appliance PanelsNorthern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
 
Lee Barnes - Path to Becoming an Effective Test Automation Engineer.pdf
Lee Barnes - Path to Becoming an Effective Test Automation Engineer.pdfLee Barnes - Path to Becoming an Effective Test Automation Engineer.pdf
Lee Barnes - Path to Becoming an Effective Test Automation Engineer.pdf
 
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
 
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
 
Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
 
AppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSFAppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSF
 
Dandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity serverDandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity server
 
Day 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio FundamentalsDay 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio Fundamentals
 
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
 

The sqlite3 commnad line tool

  • 1. The sqlite3 command line tool In this part of the SQLite tutorial, we will cover the sqlite3 command line tool. The manual describes the sqlite3 as follows: sqlite3 is a terminal-based front-end to the SQLite library that can evaluate queries interactively and display the results in multiple for‐ mats. sqlite3 can also be used within shell scripts and other applica‐ tions to provide batch processing features. sqlite3 tool sqlite3 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.5.9 Enter ".help" for instructions sqlite> The .help is one of the meta commnads of the sqlite3 tool. It shows a list of them. The .exit and the The .quit commands exit the sqlite3 session. 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
  • 2. command line tool to create a new database file. $ sqlite3 movies.db Here we create a new movies.db database. If the exists, it is opened. Basic sqlite3 meta commnads 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 commnad now shows the available tables. sqlite> SELECT * FROM Names; 1|Tom 2|Lucy 3|Frank 4|Jane 5|Robert Here we get the output of a simple SELECT statement. By default, the output mode is line and the separator is |.
  • 3. sqlite> .separator : sqlite> SELECT * FROM Names; 1:Tom 2:Lucy 3:Frank 4:Jane 5:Robert Here we have used a new colon separator. There are several other output modes available. The following example will show the column output mode. sqlite> .mode column sqlite> .headers on sqlite> SELECT * FROM Names; Id Name ---------- ---------- 1 Tom
  • 4. 2 Lucy 3 Frank 4 Jane 5 Robert In this example, we have our data in the column mode. Plus we show the column headers with the .headers command. By default, the headers are hidden. The .width command adjusts the size of the columns. sqlite> SELECT Title, Author FROM Books; Title Author ------------- ----------- War and Peace Leo Tolstoy The Brothers Fyodor Dost Crime and Pun Fyodor Dost Here, the column widths are not wide enough to display all data correctly. sqlite> .width 22, 18 sqlite> SELECT Title, Author FROM Books; Title Author
  • 5. ---------------------- ------------------ War and Peace Leo Tolstoy The Brothers Karamazov Fyodor Dostoyevsky Crime and Punishment Fyodor Dostoyevsky Here we change the column widths. The first column will be 22 characters wide, the second 18. sqlite> .show echo: off explain: off headers: off mode: list nullvalue: "" output: stdout separator: "|" width: The .show command lists various settings. We can see the output mode, the separator used in the list mode, the headers.
  • 6. sqlite> .schema Cars CREATE TABLE Cars(Id integer primary key, Name text, Cost integer); The .schema command shows the structure of the table. It gives the DDL SQL to create the table. Executing SQL from the shell We can execute SQL commands from the shell. $ sqlite3 test.db "SELECT * FROM Cars;" Id Name Cost ---------- ---------- ---------- 1 Audi 52642 2 Mercedes 57127 3 Skoda 9000 4 Volvo 29000 5 Bentley 350000 6 Citroen 21000 7 Hummer 41400 8 Volkswagen 21600
  • 7. Here we have non interactively executed a SELECT SQL command. We selected all cars from the Cars table. 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 Cost ---------- ---------- ---------- 1 Audi 52642 2 Mercedes 57127 3 Skoda 9000 4 Volvo 29000 5 Bentley 350000 6 Citroen 21000 7 Hummer 41400
  • 8. 8 Volkswagen 21600 Now, we are going to use the .dump command to dump the table. sqlite> .dump Cars BEGIN TRANSACTION; CREATE TABLE Cars(Id integer primary key, Name text, Cost 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 recreate the table. sqlite> .output cars.sql
  • 9. sqlite> .dump Cars We can also redirect the output to a file. The .output command will redirect the output to the cars.sql file. $ cat cars.sql BEGIN TRANSACTION; CREATE TABLE Cars(Id integer primary key, Name text, Cost integer); INSERT INTO "Cars" VALUES(1,'Audi',52642); ... We verify it. Reading SQL We can read SQL from a file name with the .read commnad. sqlite> .tables Cars Cars sqlite> DROP TABLE CARS; sqlite> .tables Cars sqlite> .read cars.sql
  • 10. sqlite> .tables Cars Cars sqlite> SELECT * FROM Cars WHERE id=1; Id Name Cost ---------- ---------- ---------- 1 Audi 52642 Here we have executed a series of commands. We drop the table and read it from the cars.sql, that we have created previously. 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. We can place the meta commnads there or the regular SQL statements. However, we should avoid using SQL in the resource file. $ cat .sqliterc .mode column .header on .nullvalue NULL Here is a simple example of a resource file. It has three meta commands. With resource file, we don't have to execute meta commnads all over again, when we start the sqlite3 tool. They will be executed automatically at the start of the tool.
  • 11. $ sqlite3 test.db -- Loading resources from /home/vronskij/.sqliterc SQLite version 3.5.9 Enter ".help" for instructions 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 commnad line options overwrite the resource file meta commands. $ sqlite3 -version -- Loading resources from /home/vronskij/.sqliterc 3.5.9 We get the sqlite3 version. $ sqlite3 -html test.db -- Loading resources from /home/vronskij/.sqliterc SQLite version 3.5.9 Enter ".help" for instructions
  • 12. sqlite> SELECT * FROM Cars LIMIT 2; <TR><TH>Id</TH><TH>Name</TH><TH>Cost</TH></TR> <TR><TD>1</TD> <TD>Audi</TD> <TD>52642</TD> </TR> <TR><TD>2</TD> <TD>Mercedes</TD> <TD>57127</TD> </TR> The -html option causes the results to be output as simple HTML tables In this part of the SQLite tutorial, we worked with the sqlite3 command line tool. We have described various meta commnads. We have shown, how to dump tables, read SQL from files; we described the sqlite's resource file.