python + sqlite,将变量中的数据插入表中 [英] python + sqlite, insert data from variables into table
本文介绍了python + sqlite,将变量中的数据插入表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以毫无问题地将硬编码值插入到 SQLite 表中,但我正在尝试执行以下操作:
I can insert hardcoded values into an SQLite table with no problem, but I'm trying to do something like this:
name = input("Name: ")
phone = input("Phone number: ")
email = input("Email: ")
cur.execute("create table contacts (name, phone, email)")
cur.execute("insert into contacts (name, phone, email) values"), (name, phone, email)
我知道这是错误的,但我找不到如何使它起作用.也许有人可以指出我正确的方向.
I know this is wrong, and I can't find how to make it work. Maybe someone could point me in the right direction.
推荐答案
您可以使用 ?
来表示 SQL 查询中的参数:
You can use ?
to represent a parameter in an SQL query:
cur.execute("insert into contacts (name, phone, email) values (?, ?, ?)",
(name, phone, email))
这篇关于python + sqlite,将变量中的数据插入表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文