简单的Python-MySQL桥?

zac*_*618 2 python mysql sql database

Python和MySQL之间有一个漂亮而简单的接口吗?我查看了MySQLdb模块,SQLAlchemy和MySQL提供的模块.他们工作,但只是笨重,很难快速合作.我是Python的新手,但我在MATLAB中做到了这一点,他们有一个非常简单的界面.IE

每次要在Python中执行查询时,您似乎必须执行以下操作:

import datetime
import mysql.connector
cnx = mysql.connector.connect(user='scott', database='employees')
cursor = cnx.cursor()
query = ("SELECT first_name, last_name, hire_date FROM employees "
     "WHERE hire_date BETWEEN %s AND %s")
hire_start = datetime.date(1999, 1, 1)
hire_end = datetime.date(1999, 12, 31)
cursor.execute(query, (hire_start, hire_end))
for (first_name, last_name, hire_date) in cursor:
print("{}, {} was hired on {:%d %b %Y}".format(
last_name, first_name, hire_date))
cursor.close()
cnx.close()
Run Code Online (Sandbox Code Playgroud)

在MATLAB中,我启动连接一次(比如启动程序时,然后检索某些东西就像(从这里)一样简单:

[Fn,Ln,Hd] = mysql(['SELECT first_name, last_name, hire_date FROM employees WHERE hire_date = ',num2str(some_date)])
Run Code Online (Sandbox Code Playgroud)

每次查询时都没有游标和连接,只需一个简单的I/O查询执行器和数据返回器.我喜欢玩数据库,有很多跨平台的项目.能够在MATLAB中即时连接和查看数据是一个很棒的功能.有没有Python桥来做到这一点?

din*_*elk 5

使用熊猫.它有一个很棒的界面.看这里:

python-pandas和mysql这样的数据库

我用它来从python访问我的所有数据库.