Python & MySQL Examples
- Python & MySQL - Connect Database
- Python & MySQL - Create Database
- Python & MySQL - Drop Database
- Python & MySQL - Select Database
- Python & MySQL - Create Tables
- Python & MySQL - Drop Tables
- Python & MySQL - Insert Records
- Python & MySQL - Select Records
- Python & MySQL - Update Records
- Python & MySQL - Delete Records
- Python & MySQL - Where Clause
- Python & MySQL - Like Clause
- Python & MySQL - Sorting Data
- Python & MySQL - Using Joins
- Python & MySQL - Performing Transactions
- Python & MySQL - Handling Errors
Python & MySQL Useful Resources
Python MySql - Creating Database
Python uses c.execute(q) function to create or delete a MySQL database where c is cursor and q is the query to be executed.
Syntax
# execute SQL query using execute() method. cursor.execute(sql)
| Sr.No. | Parameter & Description |
|---|---|
| 1 | $sql Required - SQL query to create a MySQL database. |
Example - Creating a Database
Try the following example to create a database −
Copy and paste the following example as main.py −
main.py
import mysql.connector
# Open database connection
db = mysql.connector.connect(host="localhost",user="root",password="Micro@Soft12")
# prepare a cursor object using cursor() method
cursor = db.cursor()
# execute SQL query using execute() method.
cursor.execute("CREATE DATABASE TUTORIALS")
print('Database created');
# disconnect from server
db.close()
Output
Execute the main.py script using python and verify the output.
Database created
Advertisements