user3319895
user3319895

Reputation: 111

Calculation using a sqlite db in python

i've generated a database file running sqlite3 inside a python script. now i'm trying to make some calculation with the data inside the db but i'm having trouble running a loop that get a name from a txt file to help in the data range selection. anyone can help me with this please or indicate what would be the best way to make calculations with the database data.

This is what i have in the text file and i want to use the name "Candy" and Cannon's Roar as a filter for the data selection
GPB, Candy, 14 4.333 GPB, Cannon's Roar, 5

and here is what i'm trying to run

import sqlite3
import re
import sqlite3

conn = sqlite3.connect('raceinfo.db')
cursor = conn.cursor()
base = open("base/dognames.txt").read()
nome = re.findall(re.compile('(.+?), (.+?), (.+?)'),base)

for n in name:
  cursor.execute("SELECT AVG(FINALPLACE) from BASEINICIAL WHERE name =('"+str(n[1]+"')")
  print cursor.fetchone()[0]

many thanks

Upvotes: 0

Views: 192

Answers (1)

CL.
CL.

Reputation: 180060

You forgot a closing parenthesis after n[1]:

cursor.execute("... WHERE name =('"+str(n[1])+"')")

To avoid formatting problems like this, and escaping problems when there is a name containing a quote, better use parameters:

cursor.execute("... WHERE name = ?", (n[1],))

Upvotes: 1

Related Questions