0

I am writing a code in python with PyQt to use Qtablewidget to display the data from table in MySQL. I successfully fetched the data from table that is varchar but I failed to fetch and display integers into Qwidget. Can you see my code and correct?

Can you also tell me how to fetch the timestamp information, and how to put it on display in qtable?

from PyQt4.QtGui import *
from PyQt4.QtSql import *

import sys


def main():
app = QApplication(sys.argv)
table = QTableWidget()
db = QSqlDatabase.addDatabase("QMYSQL")

table.setWindowTitle("Connect to Mysql Database Example")

db.setHostName("127.0.0.1")
db.setDatabaseName("attendancemanagementsystem")
db.setUserName("root")
db.setPassword("password")

if (db.open() == False):
    QMessageBox.critical(None, "Database Error",
                         db.lastError().text())

query = QSqlQuery("select * from users")

table.setColumnCount(query.record().count())
table.setRowCount(query.size())


index = 0
while (query.next()):
    table.setItem(index, 0, QTableWidgetItem(query.value(0)))
    table.setItem(index, 1, QTableWidgetItem(query.value(1)))
    table.setItem(index, 2, QTableWidgetItem(query.value(2)))
    index = index + 1

table.show()

return app.exec_()


if __name__ == '__main__':
James K
  • 3,692
  • 1
  • 28
  • 36
Zavi
  • 1
  • 4
  • http://stackoverflow.com/questions/13074035/qtablewidget-integer – PyNico Oct 05 '16 at 10:02
  • I've done some grammar improvements, and cut out some parts that were not relevant. You can be more specific. How does this code fail to display integers? Does is crash, display nothing? display the wrong values? – James K Oct 05 '16 at 18:47
  • it gives error when read integer in the table from MySQL, if we read varchar from the table than it displays without error – Zavi Oct 10 '16 at 17:22
  • hey i have checked that out, it don't gives any error, just dont show int data on the table widget, but successfully shows varchar data on table – Zavi Oct 11 '16 at 06:41

0 Answers0