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 in Python using the sqlite3 module, perform basic operations, and handle errors effectively.Connecting to the DatabaseTo interact with an SQLite database, we first need to establish a connection to it using the connect() method. If
2 min read
Python SQLite - Connecting to Database In this article, we'll discuss how to connect to an SQLite database in Python using the sqlite3 module, perform basic operations, and handle errors effectively.Connecting to the DatabaseTo interact with an SQLite database, we first need to establish a connection to it using the connect() method. If
2 min read
Python SQLite - Connecting to Database In this article, we'll discuss how to connect to an SQLite database in Python using the sqlite3 module, perform basic operations, and handle errors effectively.Connecting to the DatabaseTo interact with an SQLite database, we first need to establish a connection to it using the connect() method. If
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