是否可以在.txt或.doc或任何可打印文件中查看此查询的结果(以及类似查询),然后用户可以在任何他喜欢的地方保存并打印(如果他愿意)?
query.setQuery(""" SELECT * FROM "%s" WHERE METAPHONE("%s", 3) = METAPHONE('%s', 3) OR LEVENSHTEIN("%s", '%s') < 4 """ % (str(self.search_from_table_lineEdit.text()), str(self.search_where_lineEdit.text()), str(self.search_has_value_lineEdit.text()), str(self.search_where_lineEdit.text()), str(self.search_has_value_lineEdit.text())))
global var
var = QtGui.QTableView()
var.setModel(query)
var.show()
修改 我试过这个:
class report_GUI(QtGui.QWidget, Ui_report_Widget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.setupUi(self)
self.report_Create_Report_button.clicked.connect(self.report_data)
def report_data(self):
FILE_NAME = 'export.csv'
exportQSqlQueryModel = QSqlQueryModel()
exportQSqlQueryModel.setQuery(""" SELECT * FROM "%s" WHERE "%s" = '%s' """ % (str(self.report_from_table_lineEdit.text()), str(self.report_where_lineEdit.text()), str(self.report_has_value_lineEdit.text())))
exportFile = open(FILE_NAME, 'wt')
writer = csv.writer(exportFile)
for row in exportQSqlQueryModel.rowCount():
listsTmpData = []
for column in exportQSqlQueryModel.columnCount():
listsTmpData.append(str(exportQSqlQueryModel.record(row).value(column).toString()))
writer.writerow(listsTmpData)
exportFile.close()
得到了这个错误:
line 563, in report_data
for row in exportQSqlQueryModel.rowCount():
TypeError: 'int' object is not iterable
EDIT2 :
def report_data(self):
FILE_NAME = 'export.csv'
exportQSqlQueryModel = QSqlQueryModel()
exportQSqlQueryModel.setQuery(""" SELECT * FROM "%s" WHERE "%s" = '%s' """ % (str(self.report_from_table_lineEdit.text()), str(self.report_where_lineEdit.text()), str(self.report_has_value_lineEdit.text())))
exportFile = open(FILE_NAME, 'wt')
writer = csv.writer(exportFile)
if (self.report_from_table_lineEdit.text() == "pacijent"):
exportQSqlQueryModel.setHeaderData(0, QtCore.Qt.Horizontal, 'OIB pacijenta')
exportQSqlQueryModel.setHeaderData(1, QtCore.Qt.Horizontal, 'Prezime')
exportQSqlQueryModel.setHeaderData(2, QtCore.Qt.Horizontal, 'Ime')
exportQSqlQueryModel.setHeaderData(3, QtCore.Qt.Horizontal, 'ID sobe')
exportQSqlQueryModel.setHeaderData(4, QtCore.Qt.Horizontal, 'Adresa')
exportQSqlQueryModel.setHeaderData(5, QtCore.Qt.Horizontal, 'Datum rodjenja')
exportQSqlQueryModel.setHeaderData(6, QtCore.Qt.Horizontal, 'Spol')
listsTmpData = []
for row in range(exportQSqlQueryModel.rowCount()):
listsTmpData.append(str(exportQSqlQueryModel.headerData(row, QtCore.Qt.Horizontal)))
writer.writerow(listsTmpData)
for row in range(exportQSqlQueryModel.rowCount()):
listsTmpData = []
for column in range(exportQSqlQueryModel.columnCount()):
listsTmpData.append(str(exportQSqlQueryModel.record(row).value(column)))
writer.writerow(listsTmpData)
exportFile.close()
它仅显示前4列标题,如下所示:http://oi62.tinypic.com/2guxf5t.jpg
答案 0 :(得分:2)
我建议使用csv轻松阅读。阅读示例this。
小例子;
import csv
import sys
from PyQt4 import QtCore, QtGui, QtSql
FILE_NAME = 'export.csv'
# .
# .
# .
exportQSqlQueryModel = QtSql.QSqlQueryModel()
exportQSqlQueryModel.setQuery('YOUR QUERY')
# .
# . # Do anything in query
# .
# Open file
exportFile = open(FILE_NAME, 'wt')
writer = csv.writer(exportFile)
# If your don't have header data, Your can delete this section
listsTmpData = []
for column in range(exportQSqlQueryModel.columnCount()):
listsTmpData.append(str(exportQSqlQueryModel.headerData(column, QtCore.Qt.Horizontal).toString()))
writer.writerow(listsTmpData)
# Write file
for row in range(exportQSqlQueryModel.rowCount()):
listsTmpData = []
for column in range(exportQSqlQueryModel.columnCount()):
listsTmpData.append(str(exportQSqlQueryModel.record(row).value(column).toString()))
writer.writerow(listsTmpData)
exportFile.close()
如果您想要导出文件中的标题,请在QSqlQueryMode.setHeaderData
中设置标题并在导出数据之前写入标题;
.
.
.
exportQSqlQueryModel.setHeaderData(0, QtCore.Qt.Horizontal, 'Name')
exportQSqlQueryModel.setHeaderData(1, QtCore.Qt.Horizontal, 'Age')
.
.
.
listsTmpData = []
for column in range(exportQSqlQueryModel.columnCount()):
listsTmpData.append(str(exportQSqlQueryModel.headerData(column, QtCore.Qt.Horizontal).toString()))
writer.writerow(listsTmpData)
.
.
.
要在open office calc
中查看您的数据,您的配置会使用Comma
来正确引用您的数据;
答案 1 :(得分:0)
如果您只在linux终端的.py
文件中使用此代码,只需执行以下命令:
python your_file_nam > ex_file.txt