The use of MySQLdb.
1.create table:
#-*-coding:utf-8-*-
'
@data: 2015-01-05
@filename: createtable.py
'
Import mysqldb as MDB
Import sys
con = None
Try:
con = mdb.connect (' LocalHost ', ' root ', ' 123456 ', ' py ')
cur = con.cursor ()
Cur.execute ("CREATE TABLE IF not EXISTS \
writers (Id INT PRIMARY KEY auto_increment,name VARCHAR) ")
cur . Execute ("INSERT into writers (name) VALUES (' Jack London ')")
Cur.execute ("INSERT into writers (name) Values (' Honore de Balzac ')
Cur.execute ("INSERT into writers (Name) VALUES (' Lion ')")
Cur.execute ("INSERT into writers (name) VALUES (' Emile ')")
Cur.execute ("INSERT into writers (name) VALUES (' Truman Capote ')
print "Database Table writes Create success."
finally:
If con:
con.close ()
2.select table
#-*-Coding:utf-8-*-
‘‘‘
@data: 2015-01-05
@filename: selecttable.py
‘‘‘
Import MySQLdb as MDB
Import Sys
con = None
Try
con = mdb.connect (' localhost ', ' root ', ' 123456 ', ' py ')
cur = con.cursor ()
Cur.execute ("SELECT * From writers")
numrows = Int (cur.rowcount)
For I in Range (NumRows):
row = Cur.fetchone ()
Print row[0], ' | ', row[1]
Finally
If con:
Con.close ()
Use of MySQLdb