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__':