python3 mysql database access

Solutions on MaxInterview for python3 mysql database access by the best coders in the world

showing results for - "python3 mysql database access"
Camden
27 Jul 2017
1#!/usr/bin/python3
2
3import pymysql
4
5# Open database connection
6db = pymysql.connect("localhost","testuser","test123","TESTDB" )
7
8# prepare a cursor object using cursor() method
9cursor = db.cursor()
10
11# Drop table if it already exist using execute() method.
12cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
13
14# Create table as per requirement
15sql = """CREATE TABLE EMPLOYEE (
16   FIRST_NAME  CHAR(20) NOT NULL,
17   LAST_NAME  CHAR(20),
18   AGE INT,  
19   SEX CHAR(1),
20   INCOME FLOAT )"""
21
22cursor.execute(sql)
23
24# disconnect from server
25db.close()