· 8 years ago · Feb 02, 2018, 04:46 AM
1import MySQLdb
2
3# Open database connection ( If database is not created don't give dbname)
4db = MySQLdb.connect("localhost","yourusername","yourpassword","yourdbname" )
5
6# prepare a cursor object using cursor() method
7cursor = db.cursor()
8
9# For creating create db
10# Below line is hide your warning
11cursor.execute("SET sql_notes = 0; ")
12# create db here....
13cursor.execute("create database IF NOT EXISTS yourdbname")
14
15
16
17# create table
18cursor.execute("SET sql_notes = 0; ")
19cursor.execute("create table IF NOT EXISTS test (email varchar(70),pwd varchar(20));")
20cursor.execute("SET sql_notes = 1; ")
21
22#insert data
23cursor.execute("insert into test (email,pwd) values('test@gmail.com','test')")
24
25# Commit your changes in the database
26db.commit()
27
28# disconnect from server
29db.close()
30
31#OUTPUT
32
33# mysql> select * from test;
34# +-----------------+--------+
35# | email | pwd |
36# +-----------------+--------+
37# | test@gmail.com | test |
38# +-----------------+--------+