我有点像菜鸟,我一直在按照教程创建一个使用PyQt / Python的基本应用来查询MySql数据库。这是代码:
import sys
from PyQt4 import QtCore, QtGui, QtSql
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSql import *
app = QApplication(sys.argv)
w = QTextBrowser()
# DB type, host, user, password...
db = QSqlDatabase.addDatabase("QMYSQL");
db.setHostName("localhost")
db.setDatabaseName("microphones")
db.setUserName("root")
db.setPassword("")
ok = db.open()
# True if connected
if ok:
w.insertHtml('Connected to MySQL<br />')
else:
w.insertHtml('ERROR connecting to MySQL<br />')
# do a query "on" a DB connection
query = QSqlQuery(db)
if query.exec_("SHOW TABLES"):
w.insertHtml('<br />')
while query.next():
table = query.value(0).toString()
w.insertHtml('%s<br />' % table)
w.insertHtml('<br />')
w.insertHtml('TOTAL %s TABLES' % query.size())
w.show()
sys.exit(app.exec_())
这是表&#39; ebay_microphones&#39;在数据库&#39;麦克风&#39;:
ID,product_name,item_number,price
1,"AKG C14",564564564,32
2,"Samsung ",454564545,99
3,"Shure ",564687784,80
据我所知,代码应该显示数据库中的表格,但我收到错误builtins.AttributeError: 'str' object has no attribute 'toString'
有人能指出我正确的方向,为什么我会收到这个错误? 感谢
答案 0 :(得分:1)
只需更改此行 - table = query.value(0).toString()
即可 - table = query.value(0)