Passing SQLite variables in Python

I am writing an application in python and am using sqlite. I have a list of rows that I would like to add also to the database, where each element represents some data that matches the column to be placed.

I currently have something like this

cursor.execute("""insert into credit values ('Citi','5567','visa',6000,9.99,'23',9000)""") 

I can easily add lines, but I don’t know how to add the variables of my list.

+4
source share
1 answer

Use options for .execute() :

 query = """ INSERT INTO credit (bank, number, card, int1, value, type, int2) VALUES (?, ?, ?, ?, ?, ?, ?) """ data = ['Citi', '5567', 'visa', 6000, 9.99, '23', 9000] cursor.execute(query, data) 

According to PEP249 :

.execute(operation[,parameters])

Prepare and execute the database operation (query or command). Parameters can be represented as a sequence or mapping and will be bound to variables in the operation. Variables are specified in the database notation (for more details see the paramstyle attribute)

Check paramstyle :

 >>> import sqlite3 >>> print sqlite3.paramstyle qmark 

qmark means what are you using ? for parameters.

+9
source

All Articles