E:\sqlite3_mgmt\
. After opening the window command prompt, we navigate to the location and given the command sqlite3.exe or sqlite3 to run the application. sqlite> .exit
Listing all the commands by using help
sqlite> .help
Note that all our commands starts with dot (.), however this is not required for SQL statements. sqlite> .open E:/my_db/my_student.db
sqlite> .tables
Let us create one Table, here is a multiline SQL terminated by using ; and not starting with dot (.)
sqlite> CREATE TABLE IF NOT EXISTS
student(id integer primary key,
name text,
class text,
mark integer,
gender text
);
List all tables ( dot (.) is used here)
sqlite> .tables
Insert one record to our student table ( Note : dot (.) is not used here) but terminated with ;
sqlite> INSERT INTO `student`
(`id`, `name`, `class`, `mark`, `gender`) VALUES
(1, 'John Deo', 'Four', 75, 'female');
Display all records of the student table
sqlite> SELECT * FROM student;
Output is here
1|John Deo|Four|75|female
.mode
command.
sqlite> .mode
We can display records in different mode, the values for mode are list, quote, line, column,box, table, markdown
sqlite> .mode column
Output is here
sqlite> SELECT * FROM student;
id name class mark gender
-- -------- ----- ---- ------
1 John Deo Four 75 female
sqlite> .mode insert new_table
Output is here
sqlite> .mode insert
sqlite> SELECT * FROM student;
INSERT INTO "table"(id,name,class,mark,gender) VALUES(1,'John Deo','Four',75,'female');
We can use other output modes : "csv", "json", and "tcl"
sqlite> DROP table student;
sqlite> .open E:/my_db/my_student.db
sqlite> .tables
Checking the connection to database. This will list all the connection by giving a number.
sqlite> .conn
ACTIVE 0: E:/sqlite3_mgmt/analytics3.db
sqlite> .save my_student.db
In a different location
sqlite> .save E:/my_db/my_student.db
sqlite> .open E:/my_db/my_student2.db
There are two parts of this command, one is to create the table and other one is to insert the records. CREATE TABLE IF NOT EXISTS
student(id integer primary key,
name text,
class text,
mark integer,
gender text
);
INSERT INTO `student`
(`id`, `name`, `class`, `mark`, `gender`) VALUES
(1, 'John Deo', 'Four', 75, 'female'),
(2, 'Max Ruin', 'Three', 85, 'male'),
(3, 'Arnold', 'Three', 55, 'male'),
(4, 'Krish Star', 'Four', 60, 'female'),
(5, 'John Mike', 'Four', 60, 'female'),
(6, 'Alex John', 'Four', 55, 'male'),
(7, 'My John Rob', 'Five', 78, 'male'),
(8, 'Asruid', 'Five', 85, 'male'),
(9, 'Tes Qry', 'Six', 78, 'male'),
(10, 'Big John', 'Four', 55, 'female'),
(11, 'Ronald', 'Six', 89, 'female'),
(12, 'Recky', 'Six', 94, 'female'),
(13, 'Kty', 'Seven', 88, 'female'),
(14, 'Bigy', 'Seven', 88, 'female'),
(15, 'Tade Row', 'Four', 88, 'male'),
(16, 'Gimmy', 'Four', 88, 'male'),
(17, 'Tumyu', 'Six', 54, 'male'),
(18, 'Honny', 'Five', 75, 'male'),
(19, 'Tinny', 'Nine', 18, 'male'),
(20, 'Jackly', 'Nine', 65, 'female'),
(21, 'Babby John', 'Four', 69, 'female'),
(22, 'Reggid', 'Seven', 55, 'female'),
(23, 'Herod', 'Eight', 79, 'male'),
(24, 'Tiddy Now', 'Seven', 78, 'male'),
(25, 'Giff Tow', 'Seven', 88, 'male'),
(26, 'Crelea', 'Seven', 79, 'male'),
(27, 'Big Nose', 'Three', 81, 'female'),
(28, 'Rojj Base', 'Seven', 86, 'female'),
(29, 'Tess Played', 'Seven', 55, 'male'),
(30, 'Reppy Red', 'Six', 79, 'female'),
(31, 'Marry Toeey', 'Four', 88, 'male'),
(32, 'Binn Rott', 'Seven', 90, 'female'),
(33, 'Kenn Rein', 'Six', 96, 'female'),
(34, 'Gain Toe', 'Seven', 69, 'male'),
(35, 'Rows Noump', 'Six', 88, 'female');
sqlite> .schema invoice_client
CREATE TABLE `invoice_client` (
`client_id` integer primary key,
`client_name` text NOT NULL,
`client_add` text ,
`client_state` text,
`client_country` text ,
`client_email` text ,
`client_phone` text
);
sqlite> PRAGMA table_info('invoice_client');
0|client_id|INTEGER|0||1
1|client_name|TEXT|1||0
2|client_add|TEXT|0||0
3|client_state|TEXT|0||0
4|client_country|TEXT|0||0
5|client_email|TEXT|0||0
6|client_phone|TEXT|0||0
sqlite> SELECT sql FROM sqlite_master WHERE tbl_name='invoice_client';
CREATE TABLE `invoice_client` (
`client_id` integer primary key,
`client_name` text NOT NULL,
`client_add` text ,
`client_state` text,
`client_country` text ,
`client_email` text ,
`client_phone` text
)
.headers on
This will keep the column headers , it can be set to off ( default ) .mode csv
Output as csv format .once E:/sqlite3_mgmt/student.csv
Output is stored in CSV file at the path given. .system E:/sqlite3_mgmt/student.csv
Same as double click on file or opening the file. .once
will create the student.csv file inside the given directory.sqlite> .headers on
sqlite> .mode csv
sqlite> .once E:/sqlite3_mgmt/student.csv
sqlite> SELECT * FROM student LIMIT 0,5;
sqlite> .system E:/sqlite3_mgmt/student.csv
sqlite>
This will save and open the student.csv file from the location. sqlite> .excel
sqlite> SELECT * FROM student LIMIT 0,5;
sqlite>
This will create one temporary file with the data and user can save the file in desired location. sqlite> .mode json
sqlite> .once E:/sqlite3_mgmt/student.json
sqlite> SELECT * FROM student LIMIT 10,5;
sqlite>
To other formats we can export data, list of formats supported is here.
ascii,box,csv,column,html,insert,json,line,
list,markdown,quote,table,tabs,tcl
sqlite>.open E:\sqlite3_mgmt\my_db\my_db.db
List of tables in our my_db.db database
sqlite>.tables
As we already have student table so we can delete the same.
sqlite> DROP TABLE student;
Since we are going to import one CSV file to create the table so change the mode. Now our SQLite is ready to use CSV file.
sqlite> .mode csv
Here we are creating a table name student1 by importing the csv file from the path D:\my_db\student.csv
sqlite> .import D:\my_db\student.csv student1
Once our student1 table is created we can check the list of tables by using .tables command.
sqlite> .tables
Use query to get all the records.
sqlite> SELECT * FROM student1;
Get the structure of our student1 table
sqlite> .schema student1
Output
CREATE TABLE IF NOT EXISTS "student1"(
"id" TEXT, "name" TEXT, "class" TEXT, "mark" TEXT,
"gender" TEXT);
Note that here id and mark columns are TEXT datatype, it should be changed to integer ( INT ) .
sqlite> CREATE TABLE student ( id int,name TEXT,class TEXT,mark INT, gender TEXT);
Now copy all data from student1 table to student table.
sqlite> INSERT INTO student(id,name,class,mark,gender)
SELECT id,name,class, mark,gender FROM student1;
Check the data in our student table.
sqlite> SELECT * FROM student;
Delete the student1 table as we have copied the data to our new table.
sqlite> DROP TABLE student1;
sqlite> .open D:/sqlite3_mgmt/my_student.db
sqlite> .output D:/sqlite3_mgmt/my_dump.sql
sqlite> .dump
sqlite> .exit
Create the database with all tables from the file ( my_dump.sql )
sqlite> .read D:/sqlite3_mgmt/my_dump.sql
sqlite> .save D:/sqlite3_mgmt/my_save.db
sqlite> .exit
Our new database name is my_save.db, from this database let us save one table ( table name is y22_12 ) . Open this database if it is not opened.
sqlite> .output D:/sqlite3_mgmt/y22_12.sql
sqlite> .dump y22_12
Create backup of the table structure only of the table y22_12
sqlite> .output D:/sqlite3_mgmt/y22_12_str.sql
sqlite> .schema y22_12
Create backup of all the tables of the database ( structure only )
sqlite> .open D:/sqlite3_mgmt/my_save.db
sqlite> .output D:/sqlite3_mgmt/my_all.sql
sqlite> .schema
.open C:/xampp/htdocs/plus2net/templates/temp1/analytics3.db
I copy this code and use but I keep the sqlite3.exe file in a removable disk so the drive name changes as I connect to different systems. If you are using one system then better to use environmental variable to call this sqlite3.exe file.
SELECT changes()
More on sqlite3 changes() function
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.