[英]In Python: Insert all Entries in a QtableWidget into Database Table at once
wonder if someone knows how to insert with python all entries of a qtablewidget into a database.想知道是否有人知道如何使用 python 将 qtablewidget 的所有条目插入到数据库中。 So far i found this coding:
到目前为止,我发现了这个编码:
# Creating a query for later execution using .prepare()
insertDataQuery = QSqlQuery()
insertDataQuery.prepare(
"""
INSERT INTO contacts (
name,
job,
email
)
VALUES (?, ?, ?)
"""
)
# Sample data
data = [
("Joe", "Senior Web Developer", "joe@example.com"),
("Lara", "Project Manager", "lara@example.com"),
("David", "Data Analyst", "david@example.com"),
("Jane", "Senior Python Developer", "jane@example.com"),
]
# Use .addBindValue() to insert data
for name, job, email in data:
insertDataQuery.addBindValue(name)
insertDataQuery.addBindValue(job)
insertDataQuery.addBindValue(email)
insertDataQuery.exec()
I am stuck in assigning the entries of a qtablewidget to data我被困在将 qtablewidget 的条目分配给数据
You can use the following approach:您可以使用以下方法:
# Ignore lazy imports
import sys
from typing import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtSql import *
from PyQt5.QtWidgets import *
First, let's create a function that returns a QTableWidget
with the data you provided:首先,让我们创建一个函数,它使用您提供的数据返回一个
QTableWidget
:
def createTable() -> QTableWidget:
"""Let's create a QTableWidget with the data you provided."""
data = [
("Joe", "Senior Web Developer", "joe@example.com"),
("Lara", "Project Manager", "lara@example.com"),
("David", "Data Analyst", "david@example.com"),
("Jane", "Senior Python Developer", "jane@example.com"),
]
table = QTableWidget()
table.setRowCount(len(data))
table.setColumnCount(len(data[0]))
for i, row in enumerate(data):
for j, val in enumerate(row):
table.setItem(i, j, QTableWidgetItem(val))
return table
Now we create a function to fetch the data from the table and return it in the same exact format:现在我们创建一个函数来从表中获取数据并以完全相同的格式返回它:
def getData(table: QTableWidget) -> List[Tuple[str]]:
"""Fetch the data from the QTableWidget and return it as `data`."""
data = []
for row in range(table.rowCount()):
rowData = []
for col in range(table.columnCount()):
rowData.append(table.item(row, col).data(Qt.EditRole))
data.append(tuple(rowData))
return data
For the db insert part, we can reuse the code you provided:对于 db 插入部分,我们可以重用您提供的代码:
def insertData(data: List[Tuple[str]]) -> None:
"""Creating a query for later execution using .prepare()"""
insertDataQuery = QSqlQuery()
insertDataQuery.prepare(
"""
INSERT INTO contacts (
name,
job,
email
)
VALUES (?, ?, ?)
"""
)
# Use .addBindValue() to insert data
for name, job, email in data:
insertDataQuery.addBindValue(name)
insertDataQuery.addBindValue(job)
insertDataQuery.addBindValue(email)
insertDataQuery.exec_()
# Note that you need to run `QSqlDatabase().commit()`` if you want the data to be committed in the database.
Now putting it all together:现在把它们放在一起:
app = QApplication([sys.argv])
# Connect to sample database
sampleDb = QSqlDatabase.addDatabase("QSQLITE")
sampleDb.setDatabaseName("sample.sqlite")
sampleDb.open()
# Create table with sample data
table = createTable()
# Get data from table
data = getData(table)
# Insert data into the database
insertData(data)
sys.exit(app.exec_())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.