Create database with python

SQLite Python: Creating a New Database

Summary: in this tutorial, you will learn how to create a new SQLite database from a Python program.

When you connect to an SQLite database file that does not exist, SQLite automatically creates the new database for you.

To create a database, first, you have to create a Connection object that represents the database using the connect() function of the sqlite3 module.

For example, the following Python program creates a new database file pythonsqlite.db in the c:\sqlite\db folder.

Note that you must create the c:\sqlite\db folder first before you execute the program. Or you can place the database file a folder of your choice.

import sqlite3 from sqlite3 import Error def create_connection(db_file): """ create a database connection to a SQLite database """ conn = None try: conn = sqlite3.connect(db_file) print(sqlite3.version) except Error as e: print(e) finally: if conn: conn.close() if __name__ == '__main__': create_connection(r"C:\sqlite\db\pythonsqlite.db") Code language: Python (python)

First, we define a function called create_connection() that connects to an SQLite database specified by the database file db_file . Inside the function, we call the connect() function of the sqlite3 module.

The connect() function opens a connection to an SQLite database. It returns a Connection object that represents the database. By using the Connection object, you can perform various database operations.

In case an error occurs, we catch it within the try except block and display the error message. If everything is fine, we display the SQLite database version.

Читайте также:  Java check if date is today

It is a good programming practice that you should always close the database connection when you complete with it.

Second, we pass the path of the database file to the create_connection() function to create the database. Note that the prefix r in the r»C:\sqlite\db\pythonsqlite.db» instructs Python that we are passing a raw string.

Let’s run the program and check the c:\sqlite\db folder.

python sqlite create database

If you skip the folder path c:\sqlite\db , the program will create the database file in the current working directory (CWD).

If you pass the file name as :memory: to the connect() function of the sqlite3 module, it will create a new database that resides in the memory (RAM) instead of a database file on disk.

The following program creates an SQLite database in the memory.

import sqlite3 from sqlite3 import Error def create_connection(): """ create a database connection to a database that resides in the memory """ conn = None; try: conn = sqlite3.connect(':memory:') print(sqlite3.version) except Error as e: print(e) finally: if conn: conn.close() if __name__ == '__main__': create_connection()Code language: Python (python)

In this tutorial, you have learned how to create an SQLite database on disk and in memory from a Python program using sqlite3 module.

Источник

Python MySQL Create Database

To create a database in MySQL, use the «CREATE DATABASE» statement:

Example

create a database named «mydatabase»:

mydb = mysql.connector.connect(
host=»localhost»,
user=»yourusername«,
password=»yourpassword«
)

mycursor.execute(«CREATE DATABASE mydatabase»)

If the above code was executed with no errors, you have successfully created a database.

Check if Database Exists

You can check if a database exist by listing all databases in your system by using the «SHOW DATABASES» statement:

Читайте также:  METANIT.COM

Example

Return a list of your system’s databases:

mydb = mysql.connector.connect(
host=»localhost»,
user=»yourusername«,
password=»yourpassword«
)

Or you can try to access the database when making the connection:

Example

Try connecting to the database «mydatabase»:

mydb = mysql.connector.connect(
host=»localhost»,
user=»yourusername«,
password=»yourpassword«,
database=»mydatabase»
)

If the database does not exist, you will get an error.

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Оцените статью