Create a database table using python

0 votes
Can I create a database(Mysql) table using python? How? Can you give an example? Thank you
Jul 22, 2019 in Python by Kim
97 views

1 answer to this question.

0 votes

Hey @Kim, try something like this:

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()
answered Jul 22, 2019 by Barbara

Related Questions In Python

0 votes
1 answer

How to create a database engine using Sqlalchemy in Python?

Hi@akhtar, You can use the Sqlalchemy module in ...READ MORE

answered Jun 26 in Python by MD
• 40,740 points
58 views
0 votes
1 answer

How do I create a star using the turtle module of python?

Hey @Jinu, Try something like this: import turtle star = ...READ MORE

answered Jun 21, 2019 in Python by Neha
179 views
0 votes
0 answers
0 votes
1 answer
0 votes
1 answer

ProgrammingError: not all arguments converted during string formatting

Sorted!!!! just found the solution, 1 - apparently ...READ MORE

answered Sep 10, 2018 in Python by Priyaj
• 57,550 points
8,601 views
0 votes
1 answer

How to output text from database with line breaks in a django template?

Hello @kartik, Use linebreaks or linebreaksbr filter: {{ text|linebreaks }} Or surround the text ...READ MORE

answered Jun 24 in Python by Niroj
• 41,680 points
76 views
0 votes
1 answer
0 votes
1 answer

ModuleNotFoundError: No module named 'mysql'

Hi@akhtar, You need to install mysql-connector to connect ...READ MORE

answered Jul 15 in Python by MD
• 40,740 points
113 views
0 votes
1 answer

I'm using Python 2.7 to convert an XML response (from a REST call to Atlassian Fisheye) into an HTML table.

You don't have a template matching tabularQueryResult in your ...READ MORE

answered Oct 4, 2018 in Python by Priyaj
• 57,550 points
143 views
0 votes
1 answer

When I create and remove files rapidly on windows using python I get WindowsError (Error 5)

Here's the short answer: disable any antivirus or ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,780 points
561 views