这里使用了一个包
mysqlclient
这个包底层是c实现的,速度比较快
mysqlclient文档地址
>> pip install mysqlclient
Collecting mysqlclient
Downloading https://files.pythonhosted.org/packages/f4/f1/3bb6f64ca7a429729413e6556b7ba5976df06019a5245a43d36032f1061e/mysqlclient-1.4.2.post1.tar.gz (85kB)
100% |████████████████████████████████| 92kB 489kB/s
Building wheels for collected packages: mysqlclient
Building wheel for mysqlclient (setup.py) ... done
Stored in directory: /home/████████████████████████████████
Successfully built mysqlclient
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.4.2.post1
我们来试一下 连接mysql,代码如下:
import MySQLdb
db = MySQLdb.connect(
host="10.10.10.10", # 主机名
port=3310,
user="username", # 用户名
passwd="xxxxxxxxxxxxxsxx", # 密码
db="content_sample") # 数据库名称
# 查询前,必须先获取游标
cur = db.cursor()
# 执行的都是原生SQL语句
cur.execute("desc t_review")
for row in cur.fetchall():
print(row[0])
db.close()
如果正常连接,返回表结构
id
url
type
del_tags
add_tags
last_tags
other
mtime
成功了。