0
這是我正在查看的Tutorialspoint.com的代碼。Python SQLite插入操作
#!/usr/bin/python
import sqlite3
conn = sqlite3.connect('test.db')
conn.execute('''CREATE TABLE COMPANY
(ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL);''')
conn.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES (1, 'Paul', 32, 'California', 20000.00)");
conn.commit()
conn.close()
我如何整數變量添加到值中的一個,讓說AGE.For例子,我試過,但我得到的值錯誤:
p=32
conn.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES(1,'Paul',?,'California',20000.00)",p)
我一定來存儲所有列表中的變量然後像這樣添加到數據庫中?
list = [(1,'Paul',32,'California',20000.00)]
for item in list:
conn.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES(?,?,?,?,?)",item)