SQLite x Python插入表返回;假“;,不知道为什么

SQLite x Python插入表返回;假“;,不知道为什么,python,sqlite,sql-insert,Python,Sqlite,Sql Insert,我尝试用Python与SQL数据库协作制作一个简单的联系人簿,当我尝试进行查询时,我的INSERT函数返回false 这是我的表格声明 from PyQt5.QtWidgets import QMessageBox from PyQt5.QtSql import QSqlDatabase, QSqlQuery def _createContactsTable(): """Create the contacts table in the database

我尝试用Python与SQL数据库协作制作一个简单的联系人簿,当我尝试进行查询时,我的INSERT函数返回false

这是我的表格声明


from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtSql import QSqlDatabase, QSqlQuery


def _createContactsTable():
    """Create the contacts table in the database."""
    createTableQuery = QSqlQuery()
    return createTableQuery.exec(
        """
        CREATE TABLE IF NOT EXISTS contacts (
            id INTEGER PRIMARY KEY AUTOINCRE MENT UNIQUE NOT NULL,
            short VARCHAR(40) NOT NULL,
            fullname VARCHAR(50) NOT NULL,
            postcode VARCHAR(50) NOT NULL,
            city VARCHAR(50) NOT NULL,
            street VARCHAR(50) NOT NULL,
            phone VARCHAR(50),
            email VARCHAR(40) NOT NULL
        )
        """
    )


def createConnection(databaseName):
    """Create and open a database connection."""
    connection = QSqlDatabase.addDatabase("QSQLITE")
    connection.setDatabaseName(databaseName)

    if not connection.open():
        QMessageBox.warning(
            None,
            "CustomersManager",
            f"Błąd bazy danych: {connection.lastError().text()}",
        )
        return False

    _createContactsTable()
    return True

还有一个问题

from struktura.database import createConnection

# Create a connection
createConnection("contacts.sqlite")


# Confirm that contacts table exists
from PyQt5.QtSql import QSqlDatabase
db = QSqlDatabase.database()
db.tables()

from PyQt5.QtSql import QSqlQuery

insertDataQuery = QSqlQuery()
insertDataQuery.prepare(
    """
    INSERT INTO contacts (
        short,
        fullname,
        postcode,
        city,
        street,
        phone,
        email
    )
    VALUES (?, ?, ?, ?, ?, ?, ?)
    """
)

此过程正在创建一个新的sqlite文件,但它是空的。无法用表属性填充它。当我制作一个表中有三个值的简单模型时,它起了作用。问题出在哪里?

您是否尝试过先定义模式,然后写入模式?看看示例,您的代码没有正确地处理查询。如何绑定这些值?代码何时执行
insertDataQuery
?此处是否真的有空格:
自动递增