bms9nmh bms9nmh -4 years ago 387
Linux Question

builtins.AttributeError: 'str' object has no attribute 'toString'

I'm a bit of a noob and I've been following a tutorial to create a basic app with PyQt/Python to query a MySql database. Here is the code:

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");
ok =

# True if connected
if ok:
w.insertHtml('Connected to MySQL<br />')
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 />')
table = query.value(0).toString()
w.insertHtml('%s<br />' % table)

w.insertHtml('<br />')
w.insertHtml('TOTAL %s TABLES' % query.size())

Here is the table 'ebay_microphones' in the database 'microphones':

1,"AKG C14",564564564,32
2,"Samsung ",454564545,99
3,"Shure ",564687784,80

As far as I'm aware the code is supposed to display the tables in the database, but I'm getting the error
builtins.AttributeError: 'str' object has no attribute 'toString'

Could anyone point me in the right direction, why am I getting this error?

Answer Source

Just change this line - table = query.value(0).toString() to this - table = str(query.value(0))

There is no method named toString() in Python.
You can check here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download