lundi 5 janvier 2015

Python SQLite3 error with bindings; File not being processed line by line as specified

I am attempting to populate a SQLite3 database using Python code, but I am getting errors. Below is a snippet of the code. When I run this, I get "Error Incorrect number of bindings supplied. The current statement uses 3, and there are 10 supplied."


Basically, the entire file is being processed, and for each pass through, the list is being held in memory, such that 10 lists are being submitted in the last block, rather than one list at a time.


When I print this out, however, it LOOKS like the file is being processed as expected...line by line, such that at the end of the last code block, when it is trying to enter the items into the correct fields of the database, it is submitting one list of three items. I will past what is being printed to my screen so you may see this for yourselves.


If I modify this code, so that the list 'L2' is being iterated through item by item, I then get "Error Incorrect number of bindings supplied. The current statement uses 3, and there are 1 supplied."



import sqlite3

con = sqlite3.connect('Sequences.db')
cur = con.cursor()
cur.execute('DROP TABLE test2')
cur.execute('''CREATE TABLE test2
(seq_id TEXT, sequence TEXT, qual_score TEXT)''')

f = ('path to file')
motif = '@HWI'
flags = ['+', '<', ',', '?', '>']
l=0

with open(f, 'r') as df:
data = df.readlines()
L2 = list()

for line in data:
line = line.strip()
if motif in line:
l += 1
L2.append(line)
print l
elif not any(flag in line for flag in flags):
L2.append(line)
else:
if any(flag in line for flag in flags[1:]):
L2.append(line)
cur.executemany('INSERT into test2 VALUES (?,?,?)', L2)
del L2[:]
print 'END'
con.comitt()
con.close()
f.close()


What is being printed to my screen:
1
2
['@HWI-M01380_000000000-A7YDE:1:1101:17345:1596#0/2',
'TGCTTTAGAACACATTTCCTTCCTGCACTATTGCAATTCCCTGTTTAAGCA,
'A@CGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGFGGGGGGGGGGGGGGG]
END
1
2
['@HWI-M01380_000000000-A7YDE:1:1101:17345:1596#0/2',
'TCGAGAAAGAGCTATCAATCTGTCAATCCTTTCCGTTTCCGGGCCGGGTGA,
'CCCGGGGGGGGEGGDFEGGDEFEGGE@EGGE7CCEFGB@=F8>FADCACGG]
END

Aucun commentaire:

Enregistrer un commentaire