pyqt5-没有查询无法获取行
我使用QSQLQUERY加入两个SQLITE表的问题。运行程序时,我会收到错误“无查询无法获取行”。我找不到哪个问题是 在sqlitestudio中执行相同的查询,它可以正常工作。 在下面,我将代码,表结构和.UI文件放置。 感谢您的任何帮助
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtCore import QSize, Qt, QModelIndex
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel, QSqlRelation, QSqlQuery, QSqlQueryModel
from PyQt5.QtWidgets import QApplication, QTableView,QWidget
from quadro_costiUi import Ui_f_riepilogoCosti
class QuadroCosti(QWidget, Ui_f_riepilogoCosti):
def __init__(self):
super().__init__()
con=QSqlDatabase.addDatabase('QSQLITE')
con.setConnectOptions("detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES")
con.setDatabaseName("/Users/User/Documents/Micro/db/Micro.db")
con.open()
query=QSqlQuery()
query.prepare("SELECT * FROM tb_PreventivoDatiGenerali \
INNER JOIN tb_PreventivoDettaglioFasi \
ON tb_PreventivoDettaglioFasi = tb_PreventivoDatiGenerali.codicePreventivo \
AND tb_PreventivoDettaglioFasi.revisionePreventivo = tb_PreventivoDatiGenerali.revisione")
query.exec()
print(query.lastError().text())
app = QApplication(sys.argv)
window = QuadroCosti()
window.show()
sys.exit(app.exec_())
- sqlite表格 -
CREATE TABLE tb_PreventivoDatiGenerali (
codicePreventivo INTEGER NOT NULL,
revisione VARCHAR NOT NULL,
nomePreventivo CHAR NOT NULL,
dataPreventivo DATETIME NOT NULL,
flagFasi BOOLEAN NOT NULL,
flagMateriale BOOLEAN NOT NULL,
dataInserimento VARCHAR,
dataModifica VARCHAR,
PRIMARY KEY (
codicePreventivo,
revisione
)
);
CREATE TABLE tb_PreventivoDettaglioFasi (
codicePreventivo INTEGER NOT NULL,
revisionePreventivo VARCHAR,
nomeFase CHAR (255),
internaEsterna CHAR (1),
quantità INTEGER,
PzPerCarico INTEGER,
Campioni INTEGER,
Risorsa STRING (5),
nrCarichi INTEGER,
barraUtile INTEGER,
setupOre REAL,
setupMinuti REAL,
cicloSecondi REAL,
cicloMinuti REAL,
[%scarto] REAL,
[%efficienza] REAL,
[€/h] REAL,
costoPerCarico REAL,
costoCadauno REAL,
costoAggiuntivo REAL,
[%ricarico] REAL,
[€/cadauno] REAL,
[T (h)] REAL,
[LT (gg)] REAL,
desMat CHAR,
quantitàMat INTEGER,
diametroMat REAL,
psMat REAL,
lungPezzoGrezzoMat INTEGER,
[€/KgMat] REAL,
[€/cadaunoMat] REAL,
[LTMat(gg)] REAL,
dataIns VARCHAR,
dataMod VARCHAR,
PRIMARY KEY (
codicePreventivo ASC,
revisionePreventivo,
nomeFase
)
);
I have a problem using QSQLQuery to join two SQLite tables. When I run the program I get the error "No query Unable to fetch row". I can't spot which could be the problem as
executing the same query in SQLiteStudio it works fine.
Below I put the code, the tables structure and the .ui file.
Thanks for any help
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtCore import QSize, Qt, QModelIndex
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel, QSqlRelation, QSqlQuery, QSqlQueryModel
from PyQt5.QtWidgets import QApplication, QTableView,QWidget
from quadro_costiUi import Ui_f_riepilogoCosti
class QuadroCosti(QWidget, Ui_f_riepilogoCosti):
def __init__(self):
super().__init__()
con=QSqlDatabase.addDatabase('QSQLITE')
con.setConnectOptions("detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES")
con.setDatabaseName("/Users/User/Documents/Micro/db/Micro.db")
con.open()
query=QSqlQuery()
query.prepare("SELECT * FROM tb_PreventivoDatiGenerali \
INNER JOIN tb_PreventivoDettaglioFasi \
ON tb_PreventivoDettaglioFasi = tb_PreventivoDatiGenerali.codicePreventivo \
AND tb_PreventivoDettaglioFasi.revisionePreventivo = tb_PreventivoDatiGenerali.revisione")
query.exec()
print(query.lastError().text())
app = QApplication(sys.argv)
window = QuadroCosti()
window.show()
sys.exit(app.exec_())
-- SQLite tables --
CREATE TABLE tb_PreventivoDatiGenerali (
codicePreventivo INTEGER NOT NULL,
revisione VARCHAR NOT NULL,
nomePreventivo CHAR NOT NULL,
dataPreventivo DATETIME NOT NULL,
flagFasi BOOLEAN NOT NULL,
flagMateriale BOOLEAN NOT NULL,
dataInserimento VARCHAR,
dataModifica VARCHAR,
PRIMARY KEY (
codicePreventivo,
revisione
)
);
CREATE TABLE tb_PreventivoDettaglioFasi (
codicePreventivo INTEGER NOT NULL,
revisionePreventivo VARCHAR,
nomeFase CHAR (255),
internaEsterna CHAR (1),
quantità INTEGER,
PzPerCarico INTEGER,
Campioni INTEGER,
Risorsa STRING (5),
nrCarichi INTEGER,
barraUtile INTEGER,
setupOre REAL,
setupMinuti REAL,
cicloSecondi REAL,
cicloMinuti REAL,
[%scarto] REAL,
[%efficienza] REAL,
[€/h] REAL,
costoPerCarico REAL,
costoCadauno REAL,
costoAggiuntivo REAL,
[%ricarico] REAL,
[€/cadauno] REAL,
[T (h)] REAL,
[LT (gg)] REAL,
desMat CHAR,
quantitàMat INTEGER,
diametroMat REAL,
psMat REAL,
lungPezzoGrezzoMat INTEGER,
[€/KgMat] REAL,
[€/cadaunoMat] REAL,
[LTMat(gg)] REAL,
dataIns VARCHAR,
dataMod VARCHAR,
PRIMARY KEY (
codicePreventivo ASC,
revisionePreventivo,
nomeFase
)
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论