Python Data Access Tutorial
Selected Reading
- Python Data Access - Discussion
- Python Data Access - Useful Resources
- Python Data Access - Quick Guide
- Python MongoDB - Limit
- Python MongoDB - Update
- Python MongoDB - Drop Collection
- Python MongoDB - Delete Document
- Python MongoDB - Sort
- Python MongoDB - Query
- Python MongoDB - Find
- Python MongoDB - Insert Document
- Python MongoDB - Create Collection
- Python MongoDB - Create Database
- Python MongoDB - Introduction
- Python SQLite - Cursor Object
- Python SQLite - Join
- Python SQLite - Limit
- Python SQLite - Drop Table
- Python SQLite - Delete Data
- Python SQLite - Update Table
- Python SQLite - Order By
- Python SQLite - Where Clause
- Python SQLite - Select Data
- Python SQLite - Insert Data
- Python SQLite - Create Table
- Python SQLite - Establishing Connection
- Python SQLite - Introduction
- Python PostgreSQL - Cursor Object
- Python PostgreSQL - Join
- Python PostgreSQL - Limit
- Python PostgreSQL - Drop Table
- Python PostgreSQL - Delete Data
- Python PostgreSQL - Update Table
- Python PostgreSQL - Order By
- Python PostgreSQL - Where Clause
- Python PostgreSQL - Select Data
- Python PostgreSQL - Insert Data
- Python PostgreSQL - Create Table
- Python PostgreSQL - Create Database
- Python PostgreSQL - Database Connection
- Python PostgreSQL - Introduction
- Python MySQL - Cursor Object
- Python MySQL - Join
- Python MySQL - Limit
- Python MySQL - Drop Table
- Python MySQL - Delete Data
- Python MySQL - Update Table
- Python MySQL - Order By
- Python MySQL - Where Clause
- Python MySQL - Select Data
- Python MySQL - Insert Data
- Python MySQL - Create Table
- Python MySQL - Create Database
- Python MySQL - Database Connection
- Python MySQL - Introduction
- Python Data Access - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python MySQL - Create Database
Python MySQL - Create Database
You can create a database in MYSQL using the CREATE DATABASE query.
Syntax
Following is the syntax of the CREATE DATABASE query −
CREATE DATABASE name_of_the_database
Example
Following statement creates a database with name mydb in MySQL −
mysql> CREATE DATABASE mydb; Query OK, 1 row affected (0.04 sec)
If you observe the pst of databases using the SHOW DATABASES statement, you can observe the newly created database in it as shown below −
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | logging | | mydatabase | | mydb | | performance_schema | | students | | sys | +--------------------+ 26 rows in set (0.15 sec)
Creating a database in MySQL using python
After estabpshing connection with MySQL, to manipulate data in it you need to connect to a database. You can connect to an existing database or, create your own.
You would need special privileges to create or to delete a MySQL database. So if you have access to the root user, you can create any database.
Example
Following example estabpshes connection with MYSQL and creates a database in it.
import mysql.connector #estabpshing the connection conn = mysql.connector.connect(user= root , password= password , host= 127.0.0.1 ) #Creating a cursor object using the cursor() method cursor = conn.cursor() #Doping database MYDATABASE if already exists. cursor.execute("DROP database IF EXISTS MyDatabase") #Preparing query to create a database sql = "CREATE database MYDATABASE"; #Creating a database cursor.execute(sql) #Retrieving the pst of databases print("List of databases: ") cursor.execute("SHOW DATABASES") print(cursor.fetchall()) #Closing the connection conn.close()
Output
List of databases: [( information_schema ,), ( dbbug61332 ,), ( details ,), ( exampledatabase ,), ( mydatabase ,), ( mydb ,), ( mysql ,), ( performance_schema ,)]Advertisements