Python & MySQL Tutorial
Selected Reading
- Python & MySQL - Discussion
- Python & MySQL - Useful Resources
- Python & MySQL - Quick Guide
- Python & MySQL - Handling Errors
- Python & MySQL - Performing Transactions
- Python & MySQL - Using Joins
- Python & MySQL - Sorting Data
- Python & MySQL - Like Clause
- Python & MySQL - Where Clause
- Python & MySQL - Delete Records
- Python & MySQL - Update Records
- Python & MySQL - Select Records
- Python & MySQL - Insert Records
- Python & MySQL - Drop Tables
- Python & MySQL - Create Tables
- Python & MySQL - Select Database
- Python & MySQL - Drop Database
- Python & MySQL - Create Database
- Python & MySQL - Connect Database
- Python & MySQL - Environment Setup
- Python & MySQL - Overview
- Python & MySQL - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python & MySQL - Create Tables
Python & MySQL - Create Table
Python uses c.execute(q) function to create a table 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 table. |
Example
Try the following example to create a table −
Copy and paste the following example as mysql_example.ty −
#!/usr/bin/python import MySQLdb # Open database connection db = MySQLdb.connect("localhost","root","root@123", "TUTORIALS") # prepare a cursor object using cursor() method cursor = db.cursor() sql = """CREATE TABLE tutorials_tbl( tutorial_id INT NOT NULL AUTO_INCREMENT, tutorial_title VARCHAR(100) NOT NULL, tutorial_author VARCHAR(40) NOT NULL, submission_date DATE, PRIMARY KEY ( tutorial_id )); """; # execute SQL query using execute() method. cursor.execute(sql) print( tutorials_tbl created ) # disconnect from server db.close()
Output
Execute the mysql_example.py script using python and verify the output.
tutorials_tbl createdAdvertisements