Hello,
I'm trying to display my database on my TableWidgetItem in PyQt.
This is a snippet of my SQLite part of the code:
But when I try to run my full script (shown below) I get an error saying there's no such table called 'items' (which is wrong because I already created the table and have some info loaded on it):
Any help would be greatly appreciated.
Thanks in advance.
Here's my full script right now:
I'm trying to display my database on my TableWidgetItem in PyQt.
This is a snippet of my SQLite part of the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#--------------------------------------------------------------------- self .loaddata() def loaddata( self ): connection = sqlite3.connect( "inventory.db" ) cursor = connection.cursor() cursor.execute( 'select * from items' ) tablerow = 0 results = cursor.fetchall() self .tableWidget.setRowCount( 40 ) for row in results: self .tableWidget.setItem(tablerow, 0 , QtWidgets.QTableWidgetItem(row[ 0 ])) self .tableWidget.setItem(tablerow, 1 , QtWidgets.QTableWidgetItem(row[ 1 ])) self .tableWidget.setItem(tablerow, 2 , QtWidgets.QTableWidgetItem(row[ 2 ])) tablerow + = 1 connection.close() #--------------------------------------------------------------------- |
Error:line 227, in loaddata
cursor.execute('select * from items')
sqlite3.OperationalError: no such table: items
Am I displaying my SQLite database to my GUI properly? How Do I fix this error?Any help would be greatly appreciated.
Thanks in advance.
Here's my full script right now:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
import sys from PyQt5 import QtWidgets from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QMainWindow, QLabel, QLineEdit, QTableWidget, QTableWidgetItem, QGridLayout, QVBoxLayout, QSizePolicy, QSpacerItem) from PyQt5.QtCore import Qt, QMetaObject, QCoreApplication from PyQt5.QtGui import QFont import sqlite3 class Ui_MainDisplay(QMainWindow): def __init__( self , parent = None ): super (Ui_MainDisplay, self ).__init__(parent) self .setObjectName( "MainDisplay" ) self .setGeometry( 0 , 0 , 1123 , 903 ) self .setStyleSheet( "background-color: rgb(0, 170, 255);" ) self .centralwidget = QWidget( self ) self .centralwidget.setObjectName( "centralwidget" ) self .gridLayout = QGridLayout( self .centralwidget) self .gridLayout.setObjectName( "gridLayout" ) self .verticalLayout = QVBoxLayout() self .verticalLayout.setContentsMargins( - 1 , 0 , - 1 , 0 ) self .verticalLayout.setSpacing( 6 ) self .verticalLayout.setObjectName( "verticalLayout" ) self .AddItemButton = QPushButton( self .centralwidget) font = QFont() font.setPointSize( 10 ) font.setBold( True ) font.setWeight( 75 ) self .AddItemButton.setFont(font) self .AddItemButton.setStyleSheet( "background-color: rgb(85, 255, 0);\n" "border-style: outset;\n" "border-width: 2px;\n" "border-radius: 15px;\n" "border-color: black;\n" "padding: 4px;\n" "\n" "") self .AddItemButton.setObjectName( "AddItemButton" ) self .verticalLayout.addWidget( self .AddItemButton) self .DeleteItemButton = QPushButton( self .centralwidget) font = QFont() font.setPointSize( 10 ) font.setBold( True ) font.setWeight( 75 ) self .DeleteItemButton.setFont(font) self .DeleteItemButton.setStyleSheet( "background-color: rgb(255, 65, 68);\n" "border-style: outset;\n" "border-width: 2px;\n" "border-radius: 15px;\n" "border-color: black;\n" "padding: 4px;\n" "") self .DeleteItemButton.setObjectName( "DeleteItemButton" ) self .verticalLayout.addWidget( self .DeleteItemButton) self .CheckoutButton = QPushButton( self .centralwidget) font = QFont() font.setPointSize( 10 ) font.setBold( True ) font.setWeight( 75 ) self .CheckoutButton.setFont(font) self .CheckoutButton.setStyleSheet( "background-color: rgb(255, 255, 0);\n" "border-style: outset;\n" "border-width: 2px;\n" "border-radius: 15px;\n" "border-color: black;\n" "padding: 4px;\n" "") self .CheckoutButton.setObjectName( "CheckoutButton" ) self .verticalLayout.addWidget( self .CheckoutButton) self .ReturnButton = QPushButton( self .centralwidget) font = QFont() font.setPointSize( 10 ) font.setBold( True ) font.setWeight( 75 ) self .ReturnButton.setFont(font) self .ReturnButton.setStyleSheet( "background-color: rgb(255, 170, 32);\n" "border-style: outset;\n" "border-width: 2px;\n" "border-radius: 15px;\n" "border-color: black;\n" "padding: 4px;\n" "") self .ReturnButton.setObjectName( "ReturnButton" ) self .verticalLayout.addWidget( self .ReturnButton) self .ScanBarcodeButton = QPushButton( self .centralwidget) font = QFont() font.setPointSize( 10 ) font.setBold( True ) font.setWeight( 75 ) self .ScanBarcodeButton.setFont(font) self .ScanBarcodeButton.setStyleSheet( "background-color: rgb(211, 211, 211);\n" "border-style: outset;\n" "border-width: 2px;\n" "border-radius: 15px;\n" "border-color: black;\n" "padding: 4px;\n" "") self .ScanBarcodeButton.setObjectName( "ScanBarcodeButton" ) self .verticalLayout.addWidget( self .ScanBarcodeButton) self .MoreInfoButton = QPushButton( self .centralwidget) font = QFont() font.setPointSize( 10 ) font.setBold( True ) font.setWeight( 75 ) self .MoreInfoButton.setFont(font) self .MoreInfoButton.setStyleSheet( "background-color: rgb(196, 17, 255);\n" "border-style: outset;\n" "border-width: 2px;\n" "border-radius: 15px;\n" "border-color: black;\n" "padding: 4px;" ) self .MoreInfoButton.setObjectName( "MoreInfoButton" ) self .verticalLayout.addWidget( self .MoreInfoButton) self .RefreshButton = QPushButton( self .centralwidget) font = QFont() font.setPointSize( 10 ) font.setBold( True ) font.setWeight( 75 ) self .RefreshButton.setFont(font) self .RefreshButton.setStyleSheet( "background-color: rgb(0, 255, 255);\n" "border-style: outset;\n" "border-width: 2px;\n" "border-radius: 15px;\n" "border-color: black;\n" "padding: 4px;\n" "\n" "") self .RefreshButton.setObjectName( "RefreshButton" ) self .verticalLayout.addWidget( self .RefreshButton) spacerItem = QSpacerItem( 20 , 40 , QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Expanding) self .verticalLayout.addItem(spacerItem) self .LogoutButton = QPushButton( self .centralwidget) font = QFont() font.setPointSize( 10 ) font.setBold( True ) font.setWeight( 75 ) self .LogoutButton.setFont(font) self .LogoutButton.setStyleSheet( "background-color: rgb(255, 255, 255);\n" "border-style: outset;\n" "border-width: 2px;\n" "border-radius: 15px;\n" "border-color: black;\n" "padding: 4px;\n" "") self .LogoutButton.setObjectName( "LogoutButton" ) self .verticalLayout.addWidget( self .LogoutButton) self .gridLayout.addLayout( self .verticalLayout, 4 , 3 , 1 , 1 ) self .Header = QLabel( self .centralwidget) font = QFont() font.setPointSize( 15 ) font.setBold( True ) font.setWeight( 75 ) self .Header.setFont(font) self .Header.setStyleSheet( "background-color: rgb(0, 0, 0);\n" "color: rgb(255, 255, 255);\n" "border-style: outset;\n" "border-width: 2px;\n" "border-radius: 15px;\n" "border-color: black;\n" "padding: 4px;" ) self .Header.setAlignment(Qt.AlignmentFlag.AlignCenter) self .Header.setObjectName( "Header" ) self .gridLayout.addWidget( self .Header, 0 , 1 , 1 , 3 ) self .SearchButton = QPushButton( self .centralwidget) font = QFont() font.setPointSize( 10 ) font.setBold( True ) font.setWeight( 75 ) self .SearchButton.setFont(font) self .SearchButton.setStyleSheet( "background-color: rgb(211, 211, 211);\n" "border-style: outset;\n" "border-width: 2px;\n" "border-radius: 15px;\n" "border-color: black;\n" "padding: 4px;\n" "") self .SearchButton.setObjectName( "SearchButton" ) self .gridLayout.addWidget( self .SearchButton, 2 , 1 , 1 , 1 ) self .SearchBar = QLineEdit( self .centralwidget) self .SearchBar.setStyleSheet( "background-color: rgb(255, 255, 255);" ) self .SearchBar.setObjectName( "SearchBar" ) self .gridLayout.addWidget( self .SearchBar, 2 , 2 , 1 , 1 ) self .InventoryDisplay = QTableWidget( self .centralwidget) font = QFont() font.setBold( False ) font.setWeight( 50 ) self .InventoryDisplay.setFont(font) self .InventoryDisplay.setStyleSheet( "background-color: rgb(255, 255, 255);\n" "") self .InventoryDisplay.setObjectName( "InventoryDisplay" ) self .InventoryDisplay.setColumnCount( 11 ) self .InventoryDisplay.setRowCount( 0 ) item = QTableWidgetItem() self .InventoryDisplay.setHorizontalHeaderItem( 0 , item) item = QTableWidgetItem() self .InventoryDisplay.setHorizontalHeaderItem( 1 , item) item = QTableWidgetItem() self .InventoryDisplay.setHorizontalHeaderItem( 2 , item) item = QTableWidgetItem() self .InventoryDisplay.setHorizontalHeaderItem( 3 , item) item = QTableWidgetItem() self .InventoryDisplay.setHorizontalHeaderItem( 4 , item) item = QTableWidgetItem() self .InventoryDisplay.setHorizontalHeaderItem( 5 , item) item = QTableWidgetItem() self .InventoryDisplay.setHorizontalHeaderItem( 6 , item) item = QTableWidgetItem() self .InventoryDisplay.setHorizontalHeaderItem( 7 , item) item = QTableWidgetItem() self .InventoryDisplay.setHorizontalHeaderItem( 8 , item) item = QTableWidgetItem() self .InventoryDisplay.setHorizontalHeaderItem( 9 , item) item = QTableWidgetItem() self .InventoryDisplay.setHorizontalHeaderItem( 10 , item) self .gridLayout.addWidget( self .InventoryDisplay, 4 , 1 , 1 , 2 ) self .setCentralWidget( self .centralwidget) self .statusbar = self .statusBar() self .statusbar.setObjectName( "statusbar" ) #self.setStatusBar(self.statusbar) #--------------------------------------------------------------------- self .loaddata() def loaddata( self ): connection = sqlite3.connect( "inventory.db" ) cursor = connection.cursor() cursor.execute( 'select * from items' ) tablerow = 0 results = cursor.fetchall() self .tableWidget.setRowCount( 40 ) for row in results: self .tableWidget.setItem(tablerow, 0 , QtWidgets.QTableWidgetItem(row[ 0 ])) self .tableWidget.setItem(tablerow, 1 , QtWidgets.QTableWidgetItem(row[ 1 ])) self .tableWidget.setItem(tablerow, 2 , QtWidgets.QTableWidgetItem(row[ 2 ])) tablerow + = 1 connection.close() #--------------------------------------------------------------------- self .retranslateUi( self ) QMetaObject.connectSlotsByName( self ) def retranslateUi( self , MainDisplay): _translate = QCoreApplication.translate self .setWindowTitle(_translate( "MainDisplay" , "AdminMenu" )) self .AddItemButton.setText(_translate( "MainDisplay" , "Add Item" )) self .DeleteItemButton.setText(_translate( "MainDisplay" , "Delete Item" )) self .CheckoutButton.setText(_translate( "MainDisplay" , "Check Out" )) self .ReturnButton.setText(_translate( "MainDisplay" , "Return" )) self .ScanBarcodeButton.setText(_translate( "MainDisplay" , "Scan Barcode" )) self .MoreInfoButton.setText(_translate( "MainDisplay" , "More Info" )) self .RefreshButton.setText(_translate( "MainDisplay" , "Refresh" )) self .LogoutButton.setText(_translate( "MainDisplay" , "Log Out" )) self .Header.setText(_translate( "MainDisplay" , "Admin Menu" )) self .SearchButton.setText(_translate( "MainDisplay" , "Search" )) item = self .InventoryDisplay.horizontalHeaderItem( 0 ) item.setText(_translate( "MainDisplay" , "ID" )) item = self .InventoryDisplay.horizontalHeaderItem( 1 ) item.setText(_translate( "MainDisplay" , "Name" )) item = self .InventoryDisplay.horizontalHeaderItem( 2 ) item.setText(_translate( "MainDisplay" , "Quantity" )) item = self .InventoryDisplay.horizontalHeaderItem( 3 ) item.setText(_translate( "MainDisplay" , "Price ($)" )) item = self .InventoryDisplay.horizontalHeaderItem( 4 ) item.setText(_translate( "MainDisplay" , "Sell Price ($)" )) item = self .InventoryDisplay.horizontalHeaderItem( 5 ) item.setText(_translate( "MainDisplay" , "Description" )) item = self .InventoryDisplay.horizontalHeaderItem( 6 ) item.setText(_translate( "MainDisplay" , "Category" )) item = self .InventoryDisplay.horizontalHeaderItem( 7 ) item.setText(_translate( "MainDisplay" , "Location" )) item = self .InventoryDisplay.horizontalHeaderItem( 8 ) item.setText(_translate( "MainDisplay" , "Length (Ft)" )) item = self .InventoryDisplay.horizontalHeaderItem( 9 ) item.setText(_translate( "MainDisplay" , "Barcode #" )) item = self .InventoryDisplay.horizontalHeaderItem( 10 ) item.setText(_translate( "MainDisplay" , "Date Updated" )) #---------------------------------------------------------------------------------------------------- # Button Actions #---------------------------------------------------------------------------------------------------- #------------------------------------------ #Logout Button #------------------------------------------ #When the Logout button is clicked -> LogoutClicked Function LogoutButton = self .LogoutButton LogoutButton.clicked.connect( self .LogoutClicked) #------------------------------------------ #------------------------------------------ #Add Item Button #------------------------------------------ #When the AddItem button is clicked -> AddItem Function AddItemButton = self .AddItemButton AddItemButton.clicked.connect( self .AddItemClicked) #------------------------------------------ #------------------------------------------ #Remove Item Button #------------------------------------------ #When the RemoveItem button is clicked -> RemoveItem Function RemoveItemButton = self .DeleteItemButton RemoveItemButton.clicked.connect( self .RemoveItemClicked) #------------------------------------------ #------------------------------------------ #Checkout Item Button #------------------------------------------ #When the Checkout button is clicked -> Checkout Function CheckoutButton = self .CheckoutButton CheckoutButton.clicked.connect( self .CheckoutClicked) #------------------------------------------ #------------------------------------------ #Return Item Button #------------------------------------------ #When the Return button is clicked -> Return Function ReturnButton = self .ReturnButton ReturnButton.clicked.connect( self .ReturnClicked) #------------------------------------------ #------------------------------------------ #Scan Barcode Button #------------------------------------------ #When the Scan Barcode button is clicked -> ScanBarcode Function ScanBarcodeButton = self .ScanBarcodeButton ScanBarcodeButton.clicked.connect( self .ScanBarcodeClicked) #------------------------------------------ #------------------------------------------ #More Info Button #------------------------------------------ #When the More Info button is clicked -> MoreInfo Function MoreInfoButton = self .MoreInfoButton MoreInfoButton.clicked.connect( self .MoreInfoClicked) #------------------------------------------ #------------------------------------------ #Refresh Button #------------------------------------------ #When the More Info button is clicked -> MoreInfo Function RefreshButton = self .RefreshButton RefreshButton.clicked.connect( self .RefreshClicked) #------------------------------------------ #------------------------------------------ #Search Button #------------------------------------------ #When the More Info button is clicked -> MoreInfo Function SearchButton = self .SearchButton SearchButton.clicked.connect( self .SearchClicked) #------------------------------------------ #---------------------------------- #Logout Function def LogoutClicked( self ): #Print in terminal for testing: print ( "The Logout Button was clicked" ) #Switch from this screen to the LoginScreen #(Import LoginScreen here to prevent circular import error) from LoginScreen import Ui_Loginscreen self .win = Ui_Loginscreen() #Define LoginScreen self .win.show() #Show Login Screen self .close() #Close this screen (AdminMenu) #---------------------------------- #---------------------------------- #AddItem Function def AddItemClicked( self ): #Print in terminal for testing: print ( "The Add Item Button was clicked" ) #---------------------------------- #---------------------------------- #RemoveItem Function def RemoveItemClicked( self ): #Print in terminal for testing: print ( "The Delete Item Button was clicked" ) #---------------------------------- #---------------------------------- #Checkout Function def CheckoutClicked( self ): #Print in terminal for testing: print ( "The Checkout Button was clicked" ) #---------------------------------- #---------------------------------- #Return Function def ReturnClicked( self ): #Print in terminal for testing: print ( "The Return Button was clicked" ) #---------------------------------- #---------------------------------- #ScanBarcode Function def ScanBarcodeClicked( self ): #Print in terminal for testing: print ( "The Scan Barcode Button was clicked" ) #---------------------------------- #---------------------------------- #MoreInfo Function def MoreInfoClicked( self ): #Print in terminal for testing: print ( "The More Info Button was clicked" ) #---------------------------------- #---------------------------------- #Refresh Function def RefreshClicked( self ): #Print in terminal for testing: print ( "The Refresh Button was clicked" ) #---------------------------------- #---------------------------------- #Search Function def SearchClicked( self ): #Print in terminal for testing: print ( "The Search Button was clicked" ) #---------------------------------- #---------------------------------------------------------------------------------------------------- if __name__ = = '__main__' : app = QApplication(sys.argv) win = Ui_MainDisplay() win.show() sys.exit(app.exec_()) |