python
  1. python-creating-new-database

Python Creating New Database

Syntax

import sqlite3

conn = sqlite3.connect('database_name.db')

Example

import sqlite3

# Creating new database named mydb.db
conn = sqlite3.connect('mydb.db')

Output

This code will create a new SQLite database named mydb.db in the current directory.

Explanation

The sqlite3 module is used to create a connection to the SQLite database. connect() method is used to create a new database and it accepts a database name as an argument. Once the connection is established, you can perform various operations on the database using the connection object.

Use

Creating a new database using Python is useful when you want to store data in an organized way that can be easily searched and retrieved. This can be used in various applications like web and app development, data analysis, scientific computing, and more.

Important Points

  • connect() method creates a new database if it doesn't exist.
  • If the file already exists then it will open the existing database.
  • You can also specify the path of the database file in the argument.

Summary

In this tutorial, we learned how to create a new database using Python. We used the sqlite3 module to establish a connection to the database and then used the connect() method to create a new database.

Published on: