How to list the Tables in a SQLite Database File ?
Last Updated :
17 Jun, 2024
SQLite is a database engine which is written in C programming language. SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. SQLite is the most widely deployed SQL database engine in the world.
The source code for SQLite is in the public domain. SQLite engine is not a standalone process like other databases, you can link it statically or dynamically as per your requirement with your application. SQLite accesses its storage files directly. In this article, we'll learn how to list all the tables in a SQLite database file that was opened with an ATTACH statement. Before moving into the topic some prerequisites required are
Prerequisites
Before moving into the topic, some prerequisites are required:
- Installation of SQLite Database
- Creating databases and tables in SQLite
- Creating database files in SQLite
Installation of SQLite Database
After installation of the SQLite database engine, create a folder in your desired directory. Now using the command prompt navigate to the directory where you have created the folder and use the below command to create a SQLite database file.
sqlite> sqlite3 database_name.db;
After the creation of the database, we can access it using the below command.
sqlite> .databases
Example for creation and access of database file named 'geeks.db'
creation of database 'geeks.db'Creating Tables in SQLite
Similar to other databases, we use same syntax in SQLite. The command used to create a table is,
sqlite> create table table_name( column_name datatype(size) constraint);
After creating a table we can access it or retrieve it by using the below command,
sqlite> .tables
Example for creation of table named geek1 with geek_id of integer as primary key.
creation of table 'geek1'As we have seen above how to create databases in SQLite and creation of tables in SQLite databases, now we will look how to connect a SQLite database file using other method, i.e., using ATTACH statement.
ATTACH -SQLite
SQLite ATTACH or ATTACH DATABASE statement is used to select or connect to a particular database, and after this command, all SQLite statements will be executed under the attached database. Also ATTACH statement allows us to connect to another database file to our current database connection and execute commands under attached database using its alias name. Hence, this is helpful in working with multiple databases simultaneously.
Connecting SQLite database file using ATTACH statement
In SQLite database files can be opened or connected in two ways
- using 'sqlite3 database_name.db'
- using 'ATTACH DATABASE' statement
Here are the steps to list all the tables in a SQLite database file opened with ATTACH DATABASE statement.
- Install SQLite into the system if it is not installed.
- Create a database and create multiple tables in it which are to be listed.
- Now close the connection of SQLite database engine and then open or connect the database file using 'ATTACH DATABASE' statement.
- Now list the tables present in the database file using the syntax followed.
Now let us look into how to connect a database in SQLite using ATTACH statement.
Opening a Database File with ATTACH or ATTACH DATABASE Statement
There are two ways through which we can open or connect to a database file as follows:
1. Using ATTACH statement to open or connect to a database file:
The command used is,
sqlite> ATTACH 'database_name.db' AS 'alias_name';
2. We can use ATTACH DATABASE statement to open or connect to a database file:
The command used is,
sqlite> ATTACH DATABASE 'database_name.db' AS 'alias_name';
Listing Tables of Database Opened with ATTACH Statement
1. Command used to show all the tables in the attached database file:
sqlite> SELECT name FROM alias_name.sqlite_master WHERE type='table';
2. To show temporary tables in the database file, we need to run this:
sqlite> SELECT name FROM sqlite_temp_master WHERE type='table';
Example of Listing All the Tables in attached Database File Named 'geeks.db',
Before listing tables in a database file, We will create a database named geeks.db and create multiple tables in it. Command used to create database named geeks.db is,
sqlite3 geeks.db
Now let us create multiple tables geek1, geek2, geek3, geek4 in the above database which is created,
creation of tables in SQLiteNow we will commit and close the connection and connect again using ATTACH statement. Command used to attach database file which is created above i.e., geeks.db is,
sqlite> ATTACH 'geeks.db' as 'geek_db';
After attaching database file geeks.db we use below command to list all the tables present in it.
sqlite> SELECT name FROM geek_db.sqlite_master WHERE type='table';
we can see the output as below,
Listing tables of attached database connected with ATTACHThus after connecting a database file using ATTACH, we can perform all the operations on the attached database. For example, update, insertion, deletion, creation of tables can be performed.
Conclusion
Hence SQLite supports ATTACH or ATTACH DATABASE statement to connect a particular database file and perform operations. In addition, it also supports working with multiple databases or database files simultaneously. Also previously attached databases can be removed using DETACH DATABASE command.
Similar Reads
How to Select the Nth Row in a SQLite Database Table?
In SQLite, selecting a specific row from a table can be a common requirement, especially when dealing with large datasets. In this article, we will explore different methods to select the nth row from a SQLite database table. Whether we're a beginner or an experienced developer understanding these m
4 min read
How to Show/List Tables in MySQL Database
In MySQL, the SHOW TABLES command is a powerful tool used to list the tables within a specific database. This command provides a convenient way to view the tables that exist in a database without needing to query the database schema directly. In this article, we are going to explore various ways whe
5 min read
How to Export Database and Table Schemas in SQLite?
Exporting database schemas in SQLite is an important task for database management, enabling functions like data backup, recovery, migration, and auditing. In this article, We will go through the process of exporting database and table schemas in SQLite by understanding various examples to manage SQL
4 min read
How to Import a CSV file into a SQLite database Table using Python?
In this article, we are going to discuss how to import a CSV file content into an SQLite database table using Python. Approach:At first, we import csv module (to work with csv file) and sqlite3 module (to populate the database table).Then we connect to our geeks database using the sqlite3.connect()
3 min read
How to Show a List of All Databases in MySQL
MySQL is a popular open-source relational database management system (RDBMS) that is uniquely used to construct expandable and high-productivity databases. MySQL, which was created by MySQL AB and later acquired by its current owner Oracle Corporation, was originally introduced in 1995. MySQL is rep
7 min read
How to List All Tables in a Schema in Oracle Database?
In Oracle Database, listing all tables within a schema can be crucial for database management and analysis. we can use specific queries to retrieve information about tables in our schema. Below, we explore various queries to list tables, focusing on the SYSOBJECTS view that provides essential metada
3 min read
How to List All Tables in Oracle?
In this article, we will discuss all the methods to list all tables in the oracle SQL Database. We have three types of a subset of tables available to use as identifiers which in turn help us to sort the required table names. Here, are the following types of table identifiers in the Oracle SQL Datab
2 min read
How to Fix - NoSuchTable: Table doesn't exist in the database.
This article helps you to understand the "NoSuchTable: Table doesn't exist in the database" error, why it occurs and possible solutions you can try out to fix the errors. What is No Such Table Exist Error? Error message "NoSuchTable: Table doesn't exist in the database" appears when you have created
4 min read
How to Display the Database Name in the Result of a Query?
When working with multiple databases in SQL Server by identifying the source of data in query results is crucial. This is especially important for auditing, reporting, and debugging. Including the database name in your query output adds clarity and ensures an accurate interpretation of results. In t
3 min read
How to Display all Tables in PL/SQL?
In Oracle PL/SQL, we need to work with database objects like tables. It provides various approaches to display all the tables. Such as using the USER_TABLES, ALL_TABLES, and DBA_TABLES views. Each approach is explained with syntax and examples. In this article, We will learn about How to Display all
5 min read