Python connection operation MySQL Database

Source: Internet
Author: User
Tags sql using

  

Development Database Program Flow:

1. Create a Connection object to get the cursor

2. Execute SQL using the cursor

3. Use the cursor to get the data, judge the execution status

4. Committing a transaction or rolling back a transaction

Import

Database Connection object: Connection

Database Interaction object: Cursor

  1. #sql查询语句
  2. Sql=' select * from user '
  3. #sql语句执行
  4. Cursor.execute (SQL)
  5. #获取返回数据总条数
  6. Rs=cursor.rowcount
  7. Print RS
  8. #获取返回的第一条数据, when the cursor rownumber=0, gets =1
  9. Rs=cursor.fetchone ()
  10. Print RS
  11. #获取返回数据的接下来3条数据, when the cursor rownumber=1, gets =4
  12. Rs=cursor.fetchmany (3)
  13. Print RS
  14. #获取剩下的所有返回数据, at which time the cursor rownumber=4, gets after = Last bar
  15. Rs=cursor.fetchall ()
  16. Print RS
  17. #循环获取, process each piece of data
  18. For row in RS:
  19. print ' userid=%s username=%s '% row
  20. Cursor.close ()
  21. Conn.close ()

Python connection operation MySQL 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.