python + sqlite, insert data from variables into table

前端 未结 3 404
暖寄归人
暖寄归人 2020-12-15 07:36

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         


        
相关标签:
3条回答
  • 2020-12-15 08:06
    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

    0 讨论(0)
  • 2020-12-15 08:13

    cur.executemany("insert into contacts (name, phone, email) values (?, ?, ?)", (name, phone, email))

    0 讨论(0)
  • 2020-12-15 08:17

    You can use ? to represent a parameter in an SQL query:

    cur.execute("insert into contacts (name, phone, email) values (?, ?, ?)",
                (name, phone, email))
    
    0 讨论(0)
提交回复
热议问题