简体   繁体   中英

sqlite3.OperationalError: near "show": syntax error

I am building a table in pyqt5 which will show the output of SQL in the table. First you have to type the command in command line and then press the button to update the table.

Although, the commands like insert into table are working but when i type show databases; in the command line it is showing me this error:

Traceback (most recent call last):
  File "c:/Users/intel/Desktop/table.py", line 24, in load_data
    result = connection.execute(inp)
sqlite3.OperationalError: near "show": syntax error

Here is my code:

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QAction, QTableWidget,QTableWidgetItem,QVBoxLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
from PyQt5 import QtWidgets
import sqlite3

inp = input('>> ')

class App(QWidget):
    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 table - pythonspot.com'
        self.left = 0
        self.top = 0
        self.width = 300
        self.height = 200
        self.initUI()
#CREATE TABLE employees(email varchar(30), password(30));
    def load_data(self):
        connection = sqlite3.connect('my_db.db')
        #connection.execute(inp)
        #
        result = connection.execute(inp)
        self.tableWidget.setRowCount(0)

        for row_number, row_data in enumerate(result):
            self.tableWidget.insertRow(row_number)
            for column_number, data in enumerate(row_data):
                self.tableWidget.setItem(row_number, column_number, QTableWidgetItem(str(data)))

        connection.close()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        self.createTable()

        # Add box layout, add table to box layout and add box layout to widget
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.tableWidget) 
        self.setLayout(self.layout) 

        self.btn = QtWidgets.QPushButton(self)
        self.btn.move(100,600)
        self.btn.setText("Hello")
        self.btn.clicked.connect(self.load_data)
        # Show widget
        self.show()

    def createTable(self):

        self.tableWidget = QTableWidget()
        self.tableWidget.setRowCount(1) ##set number of rows 
        self.tableWidget.setColumnCount(8) ##this is fixed for myTableWidget, ensure that both of your tables, sql and qtablewidged have the same number of columns 


        self.tableWidget.move(1,1)

        # table selection change
        self.tableWidget.doubleClicked.connect(self.on_click)

    @pyqtSlot()
    def on_click(self):
        print("\n")
        for currentQTableWidgetItem in self.tableWidget.selectedItems():
            print(currentQTableWidgetItem.row(), currentQTableWidgetItem.column(), currentQTableWidgetItem.text())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_()) 

Any help would be appreciated!!

There is no show databases command in SQLite because there is typically only the one. However, there is PRAGMA database_list database_list . This will list the main database and any attached databases (as attached using ATTACH DATABASE 'path_to_the_database' the_schema ) ATTACH .

eg PRAGMA database_list; :-

seq name    file
0   main    E:\Navicat\SQlite Databases\soanswers.db
2   sw      E:\Navicat\SQlite Databases\xxx.db  
  • name is the schema name used when attaching the database.

Otherwise, it would be a case of searching through the file system or part of it, perhaps checking the first 16 bytes of the file for "SQLite format 3\\000" database_header if you wanted to find SQLite database files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM