Невозможно вставить значения в QSqlDatabase

from PyQt4 import QtGui,QtSql

def start_prestige():
    m_name = "Prestige"
    m_desc = "                The Prestige is a 2006 Britis-American mystery thriller film directed by Christopher Nolan, from a screenplay adapted by Nolan and his brother Jonathan from Christopher Priest's 1995\n\n novel of the same name." \
              " Its story follows Robert Angier and Alfred Borden, rival stage magicians in London at the end of the 19th century. Obsessed with creating the best stage illusion, they\n\n engage in competitive one-upmanship with tragic results." \
              " The film stars Hugh Jackman as Robert Angier, Christian Bale as Alfred Borden, and David Bowie as Nikola Tesla. It also stars Scarlett\n\nJohansson, Michael Caine, Piper Perabo, Andy Serkis, and Rebecca Hall." \
              " The film reunites Nolan with actors Bale and Caine from Batman Begins and returning cinematographer Wally Pfister,\n\n production designer Nathan Crowley, film score composer David Julyan, and editor Lee Smith."
    m_director = "Christopher Nolan"
    cast = "Hugh Jackman , Christian Bale , Scarlett Johansson , Michael Caine"
    duration = 130
    something(789, m_name, m_director, cast, m_desc, duration)

def something(id, title, director, cast, description, duration):
    db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
    db.setDatabaseName('db/test.db')
    if not db.open():
        QtGui.QMessageBox.critical(None, QtGui.qApp.tr("Cannot open database"),
                               QtGui.qApp.tr("Unable to establish a database connection.\n"
                                             "This example needs SQLite support. Please read "
                                             "the Qt SQL driver documentation for information "
                                             "how to build it.\n\n" "Click Cancel to exit."),
                               QtGui.QMessageBox.Cancel)

        return False

    query = QtSql.QSqlQuery()
    query.exec_(
        "create table movie(id INT PRIMARY KEY , title VARCHAR(20), description VARCHAR(1000), director VARCHAR(100), cast VARCHAR(100), duration_min INT")
    query.prepare("INSERT INTO movie VALUES(?,?,?,?,?,?)")
    query.addBindValue(id, title, description, director, cast, duration)
    if query.exec_():
        db.commit()

start_prestige()

Здесь проблема в том, что я не могу вставить значения в базу данных, так как она показывает TypeError:

TypeError: QSqlQuery.addBindValue (QVariant, тип QSql.ParamType = QSql.In): аргумент 2 имеет неожиданный тип «str»


person Karthik Suresh    schedule 27.10.2017    source источник


Ответы (1)


Вы можете добавить только одно связанное значение за раз. Итак, вы должны либо сделать это:

query.prepare("INSERT INTO movie VALUES(?,?,?,?,?,?)")
query.addBindValue(id)
query.addBindValue(title)
query.addBindValue(description)
query.addBindValue(director)
query.addBindValue(cast)
query.addBindValue(duration)

или что-то вроде этого:

def something(*args):
    ...
    query.prepare("INSERT INTO movie VALUES(?,?,?,?,?,?)")
    for arg in args:
        query.addBindValue(arg)

Порядок вызовов addBindValue должен соответствовать количеству и порядку заполнителей в операторе SQL.

person ekhumoro    schedule 27.10.2017