#Coding=utf-8ImportCSVImportpymysql.cursors#Establish database connection, note Chinese encoding method UTF8conn=Pymysql.connect (Host='localhost', Port=13306, the user='Root', Password='Root', DB='Test', CharSet='UTF8')#Creating CursorsCur=conn.cursor ()#executes the SQL statement, returning the number of rows affectedSql="select * from student;"cur.execute (SQL)#get the first row of results#Row_1=cur.fetchone ()#get all the results of the query, the results of the query here are deducted from the results obtained above the remaining resultsRow_all=Cur.fetchall ()#commit, or the newly created or modified data cannot be savedConn.commit ()#Close Cursorscur.close ()#Close Connectionconn.close () L=list (row_all) F=open ('D:/123.csv','A +', newline="') Writer=csv.writer (f) Num=Len (l) forIinchrange (num): Writer.writerow (L[i]) f.close ()
Python Connection Database