
本文共 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/PyMySQLcd PyMySQL
- ������ setup.py ���������
- ���������������
TESTDB
EMPLOYEE
���������������������������FIRST_NAME
���LAST_NAME
���AGE
���SEX
���INCOME
- ������������������������������������������
testuser
���test123
������������ root ������ - ���������������������������������������������������������������
- ���������������������������������������
- ���������������������������������
- ������������������������������������
python3 setup.py install
���stitial: PyMySQL ������������������ "ImportError: No module named setuptools"������������������olest������ setuptools ������������
wget https://bootstrap.pypa.io/ez_setup.pypython3 ez_setup.py
1. ���������������������
���������������������������
������������������������������������
2. ���������������������
PyMySQL ������ fetchone()
��������������������������� fetchall()
���������������������������rowcount
������������������ SQL ������������������������
��������������������������� 1000 ���������
import pymysqldb = 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 pymysqldb = 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 ���������������������������������������������������������������������������������
发表评论
最新留言
关于作者
