您可能应该改用 Pysql -纯 Python MySQL 客户端。
它适用于 Python 3.x,并且没有任何依赖关系。
这个纯 Python MySQL 客户机通过二进制客户机/服务器协议直接与服务器对话,从而为 MySQL 数据库提供了一个 DB-API。
例如:
import pymysql
conn = pymysql.connect(host='127.0.0.1', unix_socket='/tmp/mysql.sock', user='root', passwd=None, db='mysql')
cur = conn.cursor()
cur.execute("SELECT Host,User FROM user")
for r in cur:
print(r)
cur.close()
conn.close()
#!/Python36/python
#Please change above path to suit your platform. Am running it on Windows
import MySQLdb
db = MySQLdb.connect(user="my-username",passwd="my-password",host="localhost",db="my-databasename")
cursor = db.cursor()
cursor.execute("SELECT * from my-table-name")
data=cursor.fetchall()
for row in data :
print (row)
db.close()
--------------
//方法
--------------
在完成安装后首先安装 xampp-Install Python 3.7。
一旦完成安装两个-重新启动您的 Windows 系统。
现在启动 xampp 并从控制面板启动 mysql 服务器。
打开 CMD 并在终端类型 < br/>
中确认版本
c:\>cd c:\xampp\mysql\bin
c:\xampp\mysql\bin>mysql -h localhost -v
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 10.1.21-MariaDB mariadb.org binary distribution
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
这是为了检查 MYSQL 版本
c:\xampp\mysql\bin>python
Python 3.7.0b3 (v3.7.0b3:4e7efa9c6f, Mar 29 2018, 18:42:04) [MSC v.1913 64 bit (AMD64)] on win32
#!/usr/bin/python
import pymysql
pymysql.install_as_MySQLdb()
import MySQLdb
db = MySQLdb.connect(user="yourusernamehere",passwd="yourpasswordhere",host="yourhosthere",db="yourdatabasehere")
cursor = db.cursor()
cursor.execute("SELECT * from yourmysqltablehere")
data=cursor.fetchall()
for row in data :
print (row)
db.close()
db = pymysql.connect("localhost","root","","ornament")
使用 cursor ()方法准备游标对象
cursor = db.cursor()
sql = "SELECT * FROM item"
cursor.execute(sql)
获取列表列表中的所有行。
results = cursor.fetchall()
for row in results:
item_title = row[1]
comment = row[2]
print ("Title of items are the following = %s,Comments are the following = %s" % \
(item_title, comment))