【问题标题】:Simple Python-MySQL Bridge?简单的 Python-MySQL 桥?
【发布时间】:2012-10-08 03:32:10
【问题描述】:

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()

而在 MATLAB 中,我会启动一次连接(例如在启动程序时,然后检索某些内容就像 (from here) 一样简单:

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

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

【问题讨论】:

  • 您仍然需要为 MySQL 数据库 somewhere 提供连接信息。

标签: python mysql sql database


【解决方案1】:

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

python-pandas and databases like mysql

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

【讨论】:

    【解决方案2】:

    有一个名为 SqlSoup 的 SQLAlchemy 扩展,它消除了大部分设置的需要:

    from sqlalchemy.ext.sqlsoup import SqlSoup
    db = SqlSoup('mysql://scott:mypassword@localhost/employees')
    

    然后要运行 SQL 查询,请参阅 SqlSoup 文档的 raw SQL section

    rp = db.execute('select name, email from users where name like :name order by name', name='%Bhargan%')
    for name, email in rp.fetchall():
        print name, email
    

    或者如果您只想要一个结果,请使用相同的db.execute 调用,然后:

    name, email = rp.fetchone()
    

    您还可以使用 SQLAlchemy 的功能,例如 query syntax,而不是编写 SQL。

    【讨论】:

    • 不知道 sqlsoup。 +1
    • 也没听说过,我去看看。非常感谢!
    【解决方案3】:

    当然,你可以写一个这样的生成器

    import datetime
    import mysql.connector
    
    def do_mysql(query, *args):
        cnx = mysql.connector.connect(user='scott', database='employees')
        cursor = cnx.cursor()
        cursor.execute(query, args)
        for result in cursor:
            yield result
        cursor.close()
        cnx.close()
    

    但现在usernamedatabase 被硬编码到函数中。不过,MATLAB 也必须将这些参数存储在某个地方。

    您可以将usernamedatabase 作为额外参数提取出来,但随后您又回到了相同级别的复杂性 - 没有能够控制连接池等的优势。

    def do_mysql(user, database, query, *args):
        cnx = mysql.connector.connect(user=user, database=database)
        cursor = cnx.cursor()
        cursor.execute(query, args)
        for result in cursor:
            yield result
        cursor.close()
        cnx.close()
    

    因此,要从处理大量数据库查询的程序中获得所需的性能,我们至少需要传入连接

    def do_mysql(cnx, query, *args):
        cursor = cnx.cursor()
        cursor.execute(query, args)
        for result in cursor:
            yield result
        cursor.close()
    

    啊,现在这个函数没有任何胆量,代码的所有参数部分都被推回给调用者

    【讨论】:

    • +1 用于说明如何自己提取复杂性。谢谢!如果没有其他答案,我会在一两天内检查一下。
    猜你喜欢
    • 2018-04-08
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 2010-09-29
    相关资源
    最近更新 更多