Python3 MySQL 数据库连接 - PyMySQL 驱动
发布日期:2021-05-10 07:21:15 浏览次数:24 分类:精选文章

本文共 2734 字,大约阅读时间需要 9 分钟。

���������������������������������


��������� PyMySQL���

PyMySQL ��� Python 3.x ��������������������� MySQL ��������������������������� Python 2 ������������ mysqldb ���������PyMySQL ������ Python ��������� API v2.0 ��������������� pure-Python MySQL ������������������������������������


PyMySQL ������

������������������������ PyMySQL ���������������������������GitHub ���������������������������������������������������

pip3 install PyMySQL

������������������ pip���������������������������

  • ������������������������������
  • git clone https://github.com/PyMySQL/PyMySQL
    cd PyMySQL
    1. ������ setup.py ���������
    2. python3 setup.py install

      ���stitial: PyMySQL ������������������ "ImportError: No module named setuptools"������������������olest������ setuptools ������������

      wget https://bootstrap.pypa.io/ez_setup.py
      python3 ez_setup.py

      1. ���������������������

      ���������������������������

      • ��������������� TESTDB
      • EMPLOYEE ��������������������������� FIRST_NAME���LAST_NAME���AGE���SEX���INCOME
      • ������������������������������������������ testuser ��� test123������������ root ������

      ������������������������������������


      2. ���������������������

      PyMySQL ������ fetchone()��������������������������� fetchall()���������������������������rowcount ������������������ SQL ������������������������

      ��������������������������� 1000 ���������

      import pymysql
      db = pymysql.connect("localhost", "testuser", "test123", "TESTDB")
      cursor = db.cursor()
      sql = "SELECT * FROM EMPLOYEE WHERE INCOME > %s" % (1000)
      try:
      cursor.execute(sql)
      results = cursor.fetchall()
      for row in results:
      print("fname=%s, lname=%s, age=%s, sex=%s, income=%s" % (row[0], row[1], row[2], row[3], row[4]))
      except:
      print("Error: unable to fetch data")
      db.close()

      3. ���������������������

      ������������������������������������ SEX ��� 'M' ��������������� 1���

      import pymysql
      db = pymysql.connect("localhost", "testuser", "test123", "TESTDB")
      cursor = db.cursor()
      try:
      sq1 = "UPDATE EMPLOYEE SET AGE = AGE + 1 WHERE SEX = '%c'" % ('M')
      cursor.execute(sq1)
      db.commit()
      except:
      db.rollback()
      db.close()

      ������������

      PyMySQL ������������������������������������������������������ ACID ���������

      • ���������������������������������������������������������������
      • ���������������������������������������
      • ���������������������������������
      • ������������������������������������

      ��� Python DB API ������commit() ��� rollback() ���������������������


      ������������

      PyMySQL ������������������������������������������������������������������ Warning���Error���ProgrammingError���DataError ���������������������������������������������������������������������������


      ������������ PyMySQL ���������������������������������������������������������������������������������

    上一篇:Python MySQL - mysql-connector 驱动
    下一篇:Python3 网络编程

    发表评论

    最新留言

    路过按个爪印,很不错,赞一个!
    [***.219.124.196]2025年04月12日 22时31分55秒