如何在不包含语法错误的情况下将整个 table 插入?
How to insert whole table into without syntax error included?
我遇到语法错误问题:
self.db_conn.execute(queryStr,rowData)
sqlite3.OperationalError: near "%": syntax error
我想做的是将我在 QTableWidget 中编辑的内容发送回 SqlLite。还有一个我无法理解的语法问题。 xlsx 和数据库以及所有地方的列名都是 same.I 对 python 来说是新的,我无法解决这个问题 :( 如果你知道如何解决这个问题,请帮帮我 :(谢谢你的每一个回答!
我遇到问题的代码如下:
import sys
from pandas.core.frame import DataFrame
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QDialog, QApplication
import sqlite3
import pandas as pd
import os.path
class MainWindow(QDialog):
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
db_path = os.path.join(BASE_DIR, "zestawienie.db")
db_conn = sqlite3.connect('zestawienie.db')
Cursor = db_conn.cursor()
def __init__(self):
super(MainWindow, self).__init__()
loadUi("tabletutorial.ui",self)
self.tableWidget.setColumnWidth(0, 50)
self.tableWidget.setColumnWidth(1, 350)
self.tableWidget.setColumnWidth(2, 100)
# self.tableWidget.setHorizontalHeaderLabels(["Harmonogram", "Uwagi", "DXF", "Nazwa", "Kolumna1"])
self.testowy.clicked.connect(self.loaddata)
self.aktualizacja.clicked.connect(self.Aktualizuj)
self.baza.clicked.connect(self.CreateDB)
def CreateDB(self):
Zestawienie_test = pd.read_excel(
'Zestawienie.xlsx',
sheet_name='BOM',
header=0)
db_conn = sqlite3.connect("zestawienie.db")
c = db_conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS Zestawienie (
"Harmonogram" TEXT,
"Uwagi" TEXT,
"DXF" TEXT,
"Nazwa" TEXT,
"Kolumna1" TEXT)
''')
Zestawienie_test.to_sql('Zestawienie', db_conn, if_exists='append', index=False)
def Aktualizuj(self):
db_conn = sqlite3.connect("zestawienie.db")
c = db_conn.cursor()
rowCount =self.tableWidget.rowCount()
columnCount = self.tableWidget.columnCount()
for row in range(rowCount):
rowData = []
for column in range(columnCount):
widgetItem = self.tableWidget.item(row,column)
if(widgetItem and widgetItem.text):
rowData.append(widgetItem.text())
else:
rowData.append('NULL')
print(rowData)
self.insertRowInDB(rowData)
def insertRowInDB(self,rowData):
queryStr = '''INSERT INTO Zestawienie (Harmonogram, Uwagi, DXF, Nazwa, Kolumna1) VALUES (%s, %s, %s, %s, %s)'''
self.db_conn.execute(queryStr,rowData)
self.db_conn.commit()
def loaddata(self):
db_conn = sqlite3.connect('zestawienie.db')
c = db_conn.cursor()
sqlstr = 'SELECT * FROM Zestawienie LIMIT 10'
tablerow=0
results = c.execute(sqlstr)
self.tableWidget.setRowCount(10)
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]))
self.tableWidget.setItem(tablerow, 3, QtWidgets.QTableWidgetItem(row[3]))
self.tableWidget.setItem(tablerow, 4, QtWidgets.QTableWidgetItem(row[4]))
tablerow+=1
app = QApplication(sys.argv)
mainwindow = MainWindow()
widget = QtWidgets.QStackedWidget()
widget.addWidget(mainwindow)
widget.setFixedHeight(850)
widget.setFixedWidth(1120)
widget.show()
try:
sys.exit(app.exec_())
except:
print("Exiting")
sqlite Python 驱动程序使用 ?
作为占位符,而不是 %s
:
queryStr = '''INSERT INTO Zestawienie (Harmonogram, Uwagi, DXF, Nazwa, Kolumna1) VALUES (?, ?, ?, ?, ?)'''
我遇到语法错误问题:
self.db_conn.execute(queryStr,rowData)
sqlite3.OperationalError: near "%": syntax error
我想做的是将我在 QTableWidget 中编辑的内容发送回 SqlLite。还有一个我无法理解的语法问题。 xlsx 和数据库以及所有地方的列名都是 same.I 对 python 来说是新的,我无法解决这个问题 :( 如果你知道如何解决这个问题,请帮帮我 :(谢谢你的每一个回答!
我遇到问题的代码如下:
import sys
from pandas.core.frame import DataFrame
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QDialog, QApplication
import sqlite3
import pandas as pd
import os.path
class MainWindow(QDialog):
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
db_path = os.path.join(BASE_DIR, "zestawienie.db")
db_conn = sqlite3.connect('zestawienie.db')
Cursor = db_conn.cursor()
def __init__(self):
super(MainWindow, self).__init__()
loadUi("tabletutorial.ui",self)
self.tableWidget.setColumnWidth(0, 50)
self.tableWidget.setColumnWidth(1, 350)
self.tableWidget.setColumnWidth(2, 100)
# self.tableWidget.setHorizontalHeaderLabels(["Harmonogram", "Uwagi", "DXF", "Nazwa", "Kolumna1"])
self.testowy.clicked.connect(self.loaddata)
self.aktualizacja.clicked.connect(self.Aktualizuj)
self.baza.clicked.connect(self.CreateDB)
def CreateDB(self):
Zestawienie_test = pd.read_excel(
'Zestawienie.xlsx',
sheet_name='BOM',
header=0)
db_conn = sqlite3.connect("zestawienie.db")
c = db_conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS Zestawienie (
"Harmonogram" TEXT,
"Uwagi" TEXT,
"DXF" TEXT,
"Nazwa" TEXT,
"Kolumna1" TEXT)
''')
Zestawienie_test.to_sql('Zestawienie', db_conn, if_exists='append', index=False)
def Aktualizuj(self):
db_conn = sqlite3.connect("zestawienie.db")
c = db_conn.cursor()
rowCount =self.tableWidget.rowCount()
columnCount = self.tableWidget.columnCount()
for row in range(rowCount):
rowData = []
for column in range(columnCount):
widgetItem = self.tableWidget.item(row,column)
if(widgetItem and widgetItem.text):
rowData.append(widgetItem.text())
else:
rowData.append('NULL')
print(rowData)
self.insertRowInDB(rowData)
def insertRowInDB(self,rowData):
queryStr = '''INSERT INTO Zestawienie (Harmonogram, Uwagi, DXF, Nazwa, Kolumna1) VALUES (%s, %s, %s, %s, %s)'''
self.db_conn.execute(queryStr,rowData)
self.db_conn.commit()
def loaddata(self):
db_conn = sqlite3.connect('zestawienie.db')
c = db_conn.cursor()
sqlstr = 'SELECT * FROM Zestawienie LIMIT 10'
tablerow=0
results = c.execute(sqlstr)
self.tableWidget.setRowCount(10)
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]))
self.tableWidget.setItem(tablerow, 3, QtWidgets.QTableWidgetItem(row[3]))
self.tableWidget.setItem(tablerow, 4, QtWidgets.QTableWidgetItem(row[4]))
tablerow+=1
app = QApplication(sys.argv)
mainwindow = MainWindow()
widget = QtWidgets.QStackedWidget()
widget.addWidget(mainwindow)
widget.setFixedHeight(850)
widget.setFixedWidth(1120)
widget.show()
try:
sys.exit(app.exec_())
except:
print("Exiting")
sqlite Python 驱动程序使用 ?
作为占位符,而不是 %s
:
queryStr = '''INSERT INTO Zestawienie (Harmonogram, Uwagi, DXF, Nazwa, Kolumna1) VALUES (?, ?, ?, ?, ?)'''