- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Creating Database Table in Python
Once a database connection is established, we are ready to create tables or records into the database tables using execute method of the created cursor.
Example
Let us create Database table EMPLOYEE −
#!/usr/bin/python import MySQLdb # Open database connection db = MySQLdb.connect("localhost","testuser","test123","TESTDB" ) # prepare a cursor object using cursor() method cursor = db.cursor() # Drop table if it already exist using execute() method. cursor.execute("DROP TABLE IF EXISTS EMPLOYEE") # Create table as per requirement sql = """CREATE TABLE EMPLOYEE ( FIRST_NAME CHAR(20) NOT NULL, LAST_NAME CHAR(20), AGE INT, SEX CHAR(1), INCOME FLOAT )""" cursor.execute(sql) # disconnect from server db.close()
- Related Articles
- Creating column table in SAP HANA database
- Refreshing a schema after creating a table in SAP HANA database
- Creating and Using a MySQL Database
- Creating and Selecting a MySQL Database
- Example of creating a database view in SAP HANA
- Creating a new table in SAP HANA
- Fix ERROR 1064 (42000) while creating a database in MySQL?
- Creating a hash table using Javascript
- Creating a table with MySQL - Hibernate
- Creating a MySQL table using Node.js
- How can you delete a table from a database in MySQL Python?
- Creating Classes in Python
- Creating a MySQL Table in NodeJS using Sequelize
- How to check statement of creating a particular MySQL database?
- Creating Unique Key in MySQL table referring to date?

Advertisements