如何通过索引将数组元素插入SQLite表?

时间:2015-10-12 19:53:42

标签: python python-2.7 sqlite sql-insert

似乎数组元素索引不是SQLite INSERT允许的类型。

contact[0] = raw_input("Last Name: ")
contact[1] = raw_input("First Name: ")
contact[2] = raw_input("Street Address: ")
contact[3] = raw_input("City: ")
contact[4] = raw_input("State: ")
contact[5] = raw_input("Zip Code: ")
contact[6] = raw_input("Phone: ")
contact[7] = raw_input("Email: ")

cur.execute("INSERT INTO Contacts VALUES(contact[0], contact[1], contact[2], contact[3], contact[4], contact[5], contact[6], contact[7])")

有办法做到这一点吗? 谢谢!

1 个答案:

答案 0 :(得分:1)

如何让它更清洁:

labels = [
    "Last Name: ", 
    "First Name: ", 
    "Street Address: ", 
    "City: ", 
    "State: ",
    "Zip Code: ",
    "Phone: ",
    "Email: "
]
contact = [raw_input(label) for label in labels]
cur.execute("INSERT INTO Contacts VALUES(?, ?, ?, ?, ?, ?, ?, ?)", contact)

其中?placeholders for query parameters