pyQT 和 MySQL 或 MSSQL 连接
经过大量的研究和调查,我决定使用 pyQT4 使用 Eric5 作为编辑器进行 Python 开发。然而,我在尝试让 MySQL 工作时遇到了障碍。 QMySQL 驱动程序似乎存在问题。从到目前为止我所看到的讨论来看,唯一的解决方法是安装 pyQT SDK,然后重新编译 MySQL 驱动程序。一个痛苦的过程,我真的不想经历。我实际上更喜欢使用 MS SQL,但我没有找到任何支持 MSSQL 的 pyQT 驱动程序。
所以,我的问题是:将 pyQT 与 mySQL 或 MSSQL 结合使用的最佳方法是什么?
在等待答案的同时,我可能会修改 SQLAlchemy 和 mySQL.Connector 来看看它是否会与 pyQT 共存。
After much study and investigation, I've decided to do my Python development with pyQT4 using Eric5 as the editor. However, I've run into a brick wall with trying to get MySQL to work. It appears that there's an issue with the QMySQL driver. From the discussions that I've seen so far, the only fix is to install the pyQT SDK and then recompile the MySQL driver. A painful process that I really don't want to have to go through. I would actually prefer to use MS SQL but I'm not finding any drivers for pyQT with MSSQL support.
So, my question is: What is the best approach for using pyQT with either mySQL or MSSQL, that actually works?
While waiting for an answer, I might just tinker with SQLAlchemy and mySQL.Connector to see if it will co-exist with pyQT.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您更愿意使用 MS SQL,那么在 PyQt 中使用 QSqlDatabase 和 ODBC 驱动程序非常容易。
以下是我正在开发的一个使用 MS SQL 的项目的摘录。
显然,您需要已经设置主机名、数据库名、用户名和密码变量。注释掉的行显示可信连接的连接字符串;这不是我实际上需要尝试的东西,但如果这是你需要的,它应该可以工作。
打开数据库后,您可以使用 QSqlQuery,就像使用带有 PyQt 驱动程序的 MySql 一样。
If you'd rather use MS SQL it's quite easy in PyQt using QSqlDatabase and the ODBC driver.
Here's an excerpt from a project I'm working on that uses MS SQL.
Obviously you'll need to have already set the hostname, databasename, username and password variables. The commented out line shows the connection string for Trusted Connections; not something I've actually needed to try yet, but it should work if that's what you need.
Once you have the database opened you can use QSqlQuery as you would if using MySql with the PyQt driver.
是的,这会起作用,我也会做同样的事情。我喜欢编程 API,比如 SQLAlchemy 通过 Qt 的 QtSql 模块的原始 SQL 版本提供的 API。它工作得很好,只需使用 sqlalchemy 查询中的数据填充子类 QAbstractTableModel 即可,就像使用任何其他 python 对象的数据一样。但这意味着您正在处理缓存和数据库查询,从而失去了 QtSqlTableModel 的优点。但应该不会太糟糕。
yes that will work, I do the same thing. I like a programming API, like what SQLAlchemy provides over the Raw SQL version of Qt's QtSql module. It works fine and nice, just populate a subclassed QAbstractTableModel with data from your sqlalchemy queries, like you would with data from any other python object. This though means you're handling caching and database queries, losing the niceness of QtSqlTableModel. But shouldn't be too bad.
即使 QT 提供 GUI,您也可以考虑使用 pyodbc 进行数据库连接。 Pyodbc 与 MySQL 和 MS SQL Server 兼容。
You might consider pyodbc for your database connectivity, even if the GUI is being provided by QT. Pyodbc is compatible with both MySQL and MS SQL Server.