Skip to content

sqlite3 MacOS command

SQLite is a command-line utility available on MacOS for interacting with SQLite databases. With sqlite3, users can create, query, and modify databases seamlessly. This powerful tool supports various SQL commands, making it versatile for database management tasks. SQLite is lightweight, fast, and easy to use, making it a popular choice for developers working on MacOS systems. The sqlite3 command line utility on MacOS provides a user-friendly interface for managing SQLite databases efficiently.

sqlite3 Syntax:

Terminal window
sqlite3 [database_file]

sqlite3 Options:

OptionDescription
-init filenameSpecify a file with SQL commands to initialize the database
-htmlShow query results in HTML format
-lineShow query results in column-aligned format
-listShow query results in list format
-separator stringChange the column separator for the list output format
-column-namesShow column names in the list output format
-echoDisplay SQL commands before execution
-headerDisplay the column names in query output
-nullvalue stringChange the string displayed for NULL values
-bailStop after hitting an error
-helpShow help for sqlite3 command-line options

Parameters:

ParameterDescription
database_fileThe SQLite database file to work with

sqlite3 Command Usage Examples:

Connect to a Database File

Terminal window
sqlite3 test.db

This command connects to the “test.db” database file for further operations.

Display the SQLite3 Version

Terminal window
sqlite3 --version

Displays the version of SQLite3 that is currently installed on the system.

Show Available Tables in the Database

Terminal window
sqlite3 test.db .tables

Lists all the tables available in the “test.db” database.

Execute SQL Query on a Database

Terminal window
sqlite3 test.db "SELECT * FROM employees;"

Executes the SQL query that selects all records from the “employees” table in the “test.db” database.

Exit SQLite3

Terminal window
sqlite3 test.db .exit

Exits the SQLite3 prompt and closes the connection to the “test.db” database.

How do I open a database with sqlite3 in MacOS?

To open a specific database file using sqlite3 in MacOS, use the following command:

Terminal window
sqlite3 database_file.db

How do I list all tables in a database using sqlite3 in MacOS?

To list all tables in a database using sqlite3 in MacOS, run the following command after opening the database:

Terminal window
.tables

How do I run SQL queries in sqlite3 in MacOS?

To execute SQL queries within sqlite3 in MacOS, you can use the following command syntax:

Terminal window
sqlite3 database_file.db "SELECT * FROM table_name;"

How do I exit sqlite3 in MacOS?

To exit from the sqlite3 command prompt in MacOS, type the following command:

Terminal window
.exit

How do I import data from a CSV file into a table in sqlite3 on MacOS?

You can import data from a CSV file into a table in sqlite3 on MacOS by using the following command:

Terminal window
sqlite3 -separator ',' -cmd ".import file.csv table_name" database_file.db

How do I create a new table in a database using sqlite3 in MacOS?

To create a new table in a database with sqlite3 on MacOS, use the following syntax:

Terminal window
sqlite3 database_file.db "CREATE TABLE table_name (column1 INTEGER, column2 TEXT);"

How do I display the schema of a table in sqlite3 on MacOS?

You can view the schema of a table in sqlite3 on MacOS by running the following command:

Terminal window
sqlite3 database_file.db "PRAGMA table_info(table_name);"

How do I perform a backup of a SQLite database in MacOS with sqlite3?

To create a backup of a SQLite database in MacOS using sqlite3, you can run the following command:

Terminal window
sqlite3 database_file.db ".backup backup_file.db"

Applications of the sqlite3 MacOS Command

  • Create or open a SQLite database
  • Perform various operations on a SQLite database such as querying, inserting, updating, and deleting data
  • Execute SQL commands and statements
  • Import and export data from/to a SQLite database
  • Perform transactions within a SQLite database
  • Manage database schema and structure