Python Operation MySQL

Source: Internet
Author: User

1. Preparatory work
    • Installation pymysql :pip3 install pymysql
    • pymysqlis a python module dedicated to the operation of MySQL;
2. Working with MySQL
# 示例:import pymysql# 创建连接conn = pymysql.connect(host='local', port=3306, user='root', passwd='root', db='test', charset='utf8')# 创建游标cursor = conn.cursor()# 执行SQL, 并返回影响行数effect_row = cursor.execute("update hosts set host = '1.1.1.3'")# 执行SQL, 并返回影响行数# effect_row = cursor.execute("update hosts set host = '1.1.1.2' where nid > %s", (1,))# 执行SQL, 并返回受影响行数# effect_row = cursor.executemany(#    "insert into hosts(host, color_id)values(%s,%s)", [("1.1.1.11",1),("1.1.1.11",2)])# 提交,不然无法保存新建或者修改的数据conn.commit()# 查询r = cursor.execute('select * from student')print(r)# 从查询结果中获取所有数据print(cursor.fetchall())    # 结果为元组print(cursor.fetchone())    # 获取结果中的第一条数据# 关闭游标cursor.close()# 关闭连接conn.close()


Resources:

    • Python Full Stack

Python Operation MySQL

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.