Python edits MySQL database via mysqldb module, python3 using Pymysql module
Add data
1 ImportPymysql2 3conn = Pymysql.connect (host='127.0.0.1', user='Root', passwd='xilehang99', db='Liyaoguo')4 5Cur =conn.cursor ()6 7recount = Cur.execute ('INSERT into students (name,sex,age,tel,nal) VALUES (%s,%s,%s,%s,%s)',('Jack','F', 23,123456,'chain'))8 9 Conn.commit ()Ten One cur.close () A conn.close () - - Print(recount)
View data
1 ImportPymysql2 3conn = Pymysql.connect (host='127.0.0.1', user='Root', passwd='xilehang99', db='Liyaoguo')4 5Cur =conn.cursor ()6 7recount = Cur.execute ('SELECT * FROM Students')#Querying table Contents8 9 Conn.commit ()Ten One cur.close () A conn.close () - - Print(recount) the Print(Cur.fetchone ())#Get a result - Print(Cur.fetchmany (2))#get top two results - Print(Cur.fetchall ())#Get all results
modifying data
1 ImportPymysql2 3conn = Pymysql.connect (host='127.0.0.1', user='Root', passwd='xilehang99', db='Liyaoguo')4 5Cur =conn.cursor ()6 7recount = Cur.execute ('Update students Set Name =%s',('Alin',))#Modifying Data8recount = Cur.execute ('Delete from students')#Delete Data9 Ten Conn.commit () One A cur.close () - conn.close () - the Print(recount)
Python operation MySQL