Python operation MySQL

Source: Internet
Author: User

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

Related Article

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.