For create table through python program
#!/usr/bin/python
import MySQLdb
db=MySQLdb.Connect("localhost","root","root","Education")
cursor=db.cursor()
sql="CREATE TABLE Student(First_Name varchar(30),Last_Name varchar(30),Age int(20),Gender varchar(30),Email varchar(50),Languages Varchar(100),Contact_Number int(20),Department varchar(30),Year varchar(20))"
cursor.execute(sql)
db.close()
For Insert data into table though python
#!/usr/bin/python
import MySQLdb
db=MySQLdb.Connect("localhost","root","root","Education")
cursor=db.cursor()
sql="""INSERT INTO Student(First_Name,Last_Name,Age,Gender,Email,Languages,Contact_Number,Department,Year) VALUES("Mohan","patil",28,"Male","mohan@gmail.com","Hindi",7875,"Mech","2nd");"""
try:
cursor.execute(sql)
db.commit()
except :
db.rollback()
db.close()
For Show database
#!/usr/bin/python
import MySQLdb
db=MySQLdb.Connect("localhost","root","root","Education")
cursor=db.cursor()
cursor.execute("SHOW TABLES")
data=cursor.fetchall()
for row in data:
dbname = row[0]
print "db name is =%s"%dbname
db.close()
For retrieve data from Mysql or maraidb
#!/usr/bin/python
import MySQLdb
db = MySQLdb.connect("localhost","root","root","Education" )
print "db connect success"
cursor = db.cursor()
print "cursor success"
sql = "SELECT * FROM Address"
print "sql success"
try:
cursor.execute(sql)
# Fetch all the rows in a list of lists.
results = cursor.fetchall()
for row in results:
fname = row[0]
lname = row[1]
email = row[2]
mobileno = row[3]
uname = row[4]
password = row[5]
# Now print fetched result
print "fname=%s,lname=%s,email=%s,mobileno=%s,uname=%s,password=%s" % \
(fname, lname, email, mobileno, uname,password )
except:
print "Error: unable to fecth data"
# disconnect from server
db.close()