Python Connection Database

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.