0

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

5
  • What trouble are you having? Commented Apr 27, 2014 at 7:20
  • The select funtion doesnt work when i try the look using the ('"+str(n[1]+"')") Commented Apr 27, 2014 at 12:57
  • What do you mean with "doesnt work"? Commented Apr 27, 2014 at 15:06
  • it doesnt read the above as a valid syntax. this is the error that i get Commented Apr 27, 2014 at 15:43
  • Traceback (most recent call last): File "<stdin>", line 1, in <module> File "upt.py", line 10 print cursor.fetchone()[0] ^ SyntaxError: invalid syntax Commented Apr 27, 2014 at 15:44

1 Answer 1

1

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],))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.