SQlite3, Trying to insert values from user input into DB browser db using placeholders with a field that is autoincrement

huangapple go评论71阅读模式
英文:

SQlite3, Trying to insert values from user input into DB browser db using placeholders with a field that is autoincrement

问题

我正在尝试使用占位符将值插入到我的数据库中的MyAccount表中。我认为问题在于我有一个自增字段,所以我没有引用它。
我一直在遇到不同的错误,比如数据库数据类型不匹配,我认为这是因为自增字段没有输入(accountID),而且它根本不起作用,我已经尝试了很多不同的变化。有什么帮助吗?

SQlite3, Trying to insert values from user input into DB browser db using placeholders with a field that is autoincrement
SQlite3, Trying to insert values from user input into DB browser db using placeholders with a field that is autoincrement
我尝试了这个,我期望值插入到数据库中。

英文:

I'm trying to insert values into my db for the MyAccount table using placeholders. I think the issue lies in the fact that I have a field that is autoincrement and so I am not referencing it.
I keep getting different errors, such as database data type mismatch which i think is for the autoincrement field that has no input for it (accountID) and it's just not working and I have tried to look up so many different variations. Any help?

SQlite3, Trying to insert values from user input into DB browser db using placeholders with a field that is autoincrement
SQlite3, Trying to insert values from user input into DB browser db using placeholders with a field that is autoincrement
I tried this, I was expecting the values to insert into the db

答案1

得分: 1

你需要像这样调用 execute(例如)

cur = con.cursor()
cur.execute(
    'INSERT INTO MyAccount (fullName, username, password, school, phoneNumber, yearGroup) VALUES (?, ?, ?, ?, ?, ?)',
    (input_fullName, input_username, input_password, input_school, input_phoneNumber, input_yearGroup),
)
con.commit()

  • 将查询作为字符串并包含 ? 占位符(我已经拼写出列名,因此占位符的顺序不重要)
  • 将占位符的值作为单个元组

这个方法(以及替代的字典方法)在 sqlite3 模块的文档 中有文档记录。

英文:

You'll need to call execute like (e.g.)

cur = con.cursor()
cur.execute(
    'INSERT INTO MyAccount (fullName, username, password, school, phoneNumber, yearGroup) VALUES (?, ?, ?, ?, ?, ?)',
    (input_fullName, input_username, input_password, input_school, input_phoneNumber, input_yearGroup),
)
con.commit()

, that is

  • the query as a string with ? placeholders (and I've here spelled out the column names so the placeholders' order doesn't matter)
  • the placeholders' values as a single tuple

This (and the alternative dictionary method) is documented in the sqlite3 module's documentation.

答案2

得分: 0

As AKX mentioned you have to pass the parameters by tuple or list. Here's a short example for a function:

def qry_exec(db, sql, parameters):
    qry = QSqlQuery (db)
    qry.prepare (sql)
    for value in parameters:
       qry.addBindValue (value)
    if not qry.exec_ ():
        print (qry.lastError ().text ())
        return False
    else:
        return True

You can call it by:

sql = "INSERT INTO dbtable (field1, field2, field3) VALUES ( ?, ?, ?)"
ret = QtSqlTools.qry_exec (db, sql, [ data1, data2, data3])

where db is an open database

英文:

As AKX mentioned you have to pass the parameters by tuple or list. Here's a short example for a function:

def qry_exec(db, sql, parameters):
    qry = QSqlQuery (db)
    qry.prepare (sql)
    for value in parameters:
       qry.addBindValue (value)
    if not qry.exec_ ():
        print (qry.lastError ().text ())
        return False
    else:
        return True

You can call it by:

    sql = "INSERT INTO dbtable (field1, field2, field3) VALUES ( ?, ?, ?)"
    ret = QtSqlTools.qry_exec (db, sql, [ data1, data2, data3])

where db is an open database

huangapple
  • 本文由 发表于 2023年4月17日 18:34:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034216.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定