Reputation: 9492
this is the code from Tutorialspoint.com that i am looking at.
#!/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()
how do i add an integer variable to one of the values, let say AGE.For example, i tried this but i got a value error:
p=32
conn.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES(1,'Paul',?,'California',20000.00)",p)
Do i have to store all the variable in the list then add to the database like this?
list = [(1,'Paul',32,'California',20000.00)]
for item in list:
conn.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES(?,?,?,?,?)",item)
Upvotes: 0
Views: 151
Reputation: 368904
Pass parameter value as list or tuple:
p = 32
conn.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES(1,'Paul',?,'California',20000.00)", [p])
According to the DB API specification:
.execute(operation [, parameters])
... Parameters may be provided as sequence or mapping and will be bound to variables in the operation. ...
Upvotes: 2