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 23, 2019 in Python by Kim
1,071 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 23, 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, 2020 in Python by MD
• 95,440 points
1,347 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 22, 2019 in Python by Neha
1,352 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
• 58,090 points
19,623 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, 2020 in Python by Niroj
• 82,880 points
12,658 views
0 votes
1 answer

Error:Python3.4 can't install mysql-python

Hello @kartik, You can resolved this by the ...READ MORE

answered Jun 24, 2020 in Python by Niroj
• 82,880 points
1,808 views
0 votes
1 answer

ModuleNotFoundError: No module named 'mysql'

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

answered Jul 15, 2020 in Python by MD
• 95,440 points
49,249 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
• 58,090 points
1,088 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,720 points
1,642 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP