安装
pip install xx
orpython -m pip install xx -i https://pypi.douban.com/simple
- v2.x:
MySQLdb
- v3.x:
pymysql
、mysql-connector
使用
conn
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| config = { 'host': 'localhost', 'port': 3306, 'user': 'root', 'passwd': '123456', 'db': 'test_db', 'charset': 'utf8', "local_infile": 1, }
import MySQLdb
conn = MySQLdb.connect(**config) cursor = conn.cursor()
import pymysql
conn = pymysql.connect(**config) cursor = conn.cursor(pymysql.cursors.DictCursor)
from mysql.connector.cursor import MySQLCursorDict import mysql.connector
conn = mysql.connector.connect(**config) cursor = MySQLCursorDict(conn)
|
cursor
执行 sql 语句
execute(sql, arg)
1 2 3
| sql = """SELECT * FROM %s WHERE username=%s AND password=%s"""
cursor.execute(sql, (arg1, arg2, arg3)))
|
executemany(sql, args)
1 2 3 4 5 6 7 8
| sql = """SELECT * FROM %s WHERE username=%s AND password=%s"""
args = [ (arg11, arg12, arg13), (arg21, arg22, arg23), ]
cursor.executemany(sql, args))
|
MySQLdb.cursors.Cursor
fetchone() -> tuple
1 2 3 4 5 6 7 8
| cursor.execute(sql, (arg1, arg2, arg3)) cursor.fetchone()
res = cursor.execute('select count(1) from %s where %s=%s', (arg1, arg2, arg3))
data = cursor.fetchone()
|
fetchall() -> tuple
of tuple
MySQLdb.cursors.DictCursor
1
| cursor = conn.cursor(MySQLdb.cursors.DictCursor)
|
fetchone() -> dict
fetchall() -> tuple
of dict
问题
使用pymysql报错:AttributeError: module ‘pymysql._auth‘ has no attribute ‘scramble_old_password‘
解决:使用0.6.7
版本,pip install pymysql==0.6.7
https://blog.csdn.net/qq_38866586/article/details/121545480