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.
Asclepius
64.7k20 gold badges188 silver badges165 bronze badges
3 Answers 3
You can use ? to represent a parameter in an SQL query:
cur.execute("insert into contacts (name, phone, email) values (?, ?, ?)",
(name, phone, email))
answered Dec 5, 2010 at 19:11
Mark Byers
844k202 gold badges1.6k silver badges1.5k bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
steini
Thank you sir,I had been trying some variations of that but this works :D
Amey P Naik
can i use a list [name, phone, email] instead of a set (name, phone, email) ?
ArduinoBen
@AmeyPNaik I think it needs a tuple
cur.executemany("insert into contacts (name, phone, email) values (?, ?, ?)", (name, phone, email))
1 Comment
Anh Pham
Usually it's better to explain a solution instead of just posting some rows of anonymous code. You can read How do I write a good answer, and also Explaining entirely code-based answers
cur.execute("create table contacts (name, phone, email)")
cur.execute("insert into contacts (name, phone, email) values(?,?,?)",(name, phone, email))
OR
cur.execute("insert into contacts values(?,?,?)",(name, phone, email))
As you are inserting values to all available fields, its okay not to mention columns name in insert query
Suraj Rao
29.7k11 gold badges96 silver badges104 bronze badges
answered Aug 12, 2020 at 10:09
Sameer Ranjan Sahoo
111 bronze badge
Comments
default
[solved]in the title is not usual here. Thanks!