First, Python2 in general use MYSQLDB to call MySQL, but in Python3 does not support the package, using Pymysql to replace, the use of the exact same.
Second, installation:
Pip Install Pymysql
Third, examples:
#Coding Utf-8ImportPymysqlTry: Conn= Pymysql.connect (host='localhost', user="Root", password='123456', database='Datarepair', port=3306,charset='UTF8') Cursor=conn.cursor ()#cursor.execute ("Select Rowid,name from Ruletab where ruletype=%s", (Ten))Cursor.executemany ("Select Rowid,name from Ruletab where ruletype=%s", [10,20])#generally used in bulk additions and deletions. Print(Cursor.rowcount,cursor.rownumber) forXinchCursor.fetchall ():Print("Rowid:{0[0]} name:{0[1]} rownumber:{1}". Format (x,cursor.rownumber))Print('-'* 50) forXinchCursor.fetchmany (2): Print("Rowid:{0[0]} name:{0[1]} rownumber:{1}". Format (x,cursor.rowcount))Print('-'* 50) cursor.scroll (0,'Absolute')#the cursor is recalled to the 0 position. forXinchCursor.fetchmany (2):#take 2 records. Print("Rowid:{0[0]} name:{0[1]} rownumber:{1}". Format (x,cursor.rownumber))Print(Cursor.rowcount,cursor.rownumber) cursor.close ()finally: if 'Conn' inchDir () andCallable (conn) andconn.open:conn.close ()Print("closed successfully.")
Usage of MySQL in Python3.