Python SQLite - Creating a New Database
Last Updated :
20 May, 2021
In this article, we will discuss how to create a Database in SQLite using Python.
Creating a Database
You do not need any special permissions to create a database. The sqlite3 command used to create the database has the following basic syntax
Syntax: $ sqlite3 <database_name_with_db_extension>
The database name must always be unique in the RDBMS.
Example:
When we create a sqlite database.
Similarly, we can create this database in python using the SQlite3 module.
Python3
import sqlite3
# filename to form database
file = "Sqlite3.db"
try:
conn = sqlite3.connect(file)
print("Database Sqlite3.db formed.")
except:
print("Database Sqlite3.db not formed.")
Output:
Database Sqlite3.db formed.
Connect Database:
Creating a brand-new SQLite database is as easy as growing a connection to the usage of the sqlite3 module inside the Python preferred library. To establish a connection, all you have to do is to pass the file path to the connect(…) method in the sqlite3 module. If the database represented by the file does not exist, it will be created under this path.
import sqlite3
connection = sqlite3.connect(<path_to_file_db.sqlite3>)
Let's see some description about connect() method,
Syntax: sqlite3.connect(database [, timeout , other optional arguments])
The use of this API opens a connection to the SQLite database file. Use ":memory:" to set up a connection to a database placed in RAM in place of the hard disk. A Connection object will be returned, when a database opened correctly.
The database can be accessed through multiple connections, and one of the processes is modifying the database. The SQLite database will hang until the transaction is committed. The timeout parameter specifies how long the connection should wait to unlock before throwing an exception. Timeout parameter 5.0 (five seconds). If the specified database name does not exist, this call will create the database.
If we want to create database data in a location other than the current directory, we can also use the desired path to specify the file name.
Implementation:
1. Creating a Connection between sqlite3 database and Python Program
sqliteConnection = sqlite3.connect('SQLite_Retrieving_data.db')
2. If sqlite3 makes a connection with the python program then it will print "Connected to SQLite", Otherwise it will show errors
print("Connected to SQLite")
3. If the connection is open, we need to close it. Closing code is present inside the final Block. We will use close() method for closing the connection object. After closing the connection object, we will print "the SQLite connection is closed"
if sqliteConnection:
sqliteConnection.close()
print("the sqlite connection is closed")
Python3
# Importing Sqlite3 Module
import sqlite3
try:
# Making a connection between sqlite3 database and Python Program
sqliteConnection = sqlite3.connect('SQLite_Retrieving_data.db')
# If sqlite3 makes a connection with python program then it will print "Connected to SQLite"
# Otherwise it will show errors
print("Connected to SQLite")
except sqlite3.Error as error:
print("Failed to connect with sqlite3 database", error)
finally:
# Inside Finally Block, If connection is open, we need to close it
if sqliteConnection:
# using close() method, we will close the connection
sqliteConnection.close()
# After closing connection object, we will print "the sqlite connection is closed"
print("the sqlite connection is closed")
Output:
Output for above python program
Similar Reads
Python SQLite - Connecting to Database
In this article, we'll discuss how to connect to an SQLite Database using the sqlite3 module in Python. Connecting to the Database Connecting to the SQLite Database can be established using the connect() method, passing the name of the database to be accessed as a parameter. If that database does no
2 min read
Interface Python with an SQL Database
Python is an easy-to-learn language and connectivity of python with any SQL database is a much-desired option to have the persistence feature. Python is an object-oriented programming language and it is open source. Newcomers to the software industry including school children too can learn Python ea
8 min read
Create a database in MongoDB using Python
MongoDB is a general-purpose, document-based, distributed database built for modern application developers and the cloud. It is a document database, which means it stores data in JSON-like documents. This is an efficient way to think about data and is more expressive and powerful than the traditiona
2 min read
How to Connect Python with SQL Database?
In this article, we will learn how to connect SQL with Python using the MySQL Connector Python module. Below diagram illustrates how a connection request is sent to MySQL connector Python, how it gets accepted from the database and how the cursor is executed with result data. To create a connection
2 min read
Android SQLite Database in Kotlin
Android comes with an inbuilt implementation of a database package, which is SQLite, an open-source SQL database that stores data in form of text in devices. In this article, we will look at the implementation of Android SQLite in Kotlin. SQLite is a self-contained, high-reliability, embedded, full-
5 min read
Python MySQL - Create Database
Python Database API ( Application Program Interface ) is the Database interface for the standard Python. This standard is adhered to by most Python Database interfaces. There are various Database servers supported by Python Database such as MySQL, GadFly, mSQL, PostgreSQL, Microsoft SQL Server 2000,
2 min read
Python SQLite - Insert Data
In this article, we will discuss how can we insert data in a table in the SQLite database from Python using the sqlite3 module. The SQL INSERT INTO statement of SQL is used to insert a new row in a table. There are two ways of using the INSERT INTO statement for inserting rows: Only values: The firs
3 min read
SQL CREATE DATABASE
Creating a database is one of the fundamental tasks in SQL and is the first step in structuring your data for efficient management. Whether you're a developer or a database administrator, understanding the CREATE DATABASE statement is essential. Understanding how to use this command effectively is c
6 min read
Connecting to SQL Database using SQLAlchemy in Python
In this article, we will see how to connect to an SQL database using SQLAlchemy in Python. To connect to a SQL database using SQLAlchemy we will require the sqlalchemy library installed in our python environment. It can be installed using pip - !pip install sqlalchemyThe create_engine() method of sq
3 min read
Python SQLite - ORDER BY Clause
In this article, we will discuss ORDER BY clause in SQLite using Python. The ORDER BY statement is a SQL statement that is used to sort the data in either ascending or descending according to one or more columns. By default, ORDER BY sorts the data in ascending order. DESC is used to sort the data i
3 min read