#python3 connecting MySQL instancesImportPymysql"""Import connection MySQL required package, do not install pymysql need to install using command line switch to python installation path under Scripts subdirectory installation (pip install pymysql)"""#connect to MySQL databasedb = Pymysql.connect ("localhost","Root","123456","Python_conn", CharSet ='UTF8')"""five parameters for the host name, user name, password, the database name to connect to, in order to solve the problem of Chinese characters character set"""#get an operation cursor using the cursor () methodcursor =db.cursor ()#executing SQL statements using the Execute () methodsql ='SELECT * from Test_conn'cursor.execute (SQL)#get all data using the Fetchone () methodResults =Cursor.fetchall ()#Print forRowinchResults:sid=Row[0] Name= Row[1] Age= Row[2] Sclass= Row[3] Print(Sid,name,age,sclass)#Close the databaseDb.close ()
Python3 connecting MySQL DB instance