0

I'm trying to write a Python (2) script that manages a SQLite3 database. I'm having trouble getting all of the rows from the table and looping through them. My table has 218 rows (according to PHP and sqlite3.exe) yet Python only loops through 8.

import sqlite3 as sql

db = sql.connect('database.db')
c = db.cursor()
n = 0

for row in c.execute('select * from table'):
    n += 1

print n

What am I doing wrong? Is there some extra step that I need to take to get Python to loop through all of the rows?

2
  • Which eight records are looped through? (Print out some value in the loop.) Commented Feb 19, 2014 at 8:18
  • The records seem to be randomly selected from the database - they aren't alphabetical or in the order SELECT * FROM table results in in sqlite3.exe Commented Feb 20, 2014 at 3:19

1 Answer 1

1

I do something like this:

conn     = sqlite3.connect(filename)
cursor   = conn.cursor()
cursor.execute('SELECT * from tablename')
results  = cursor.fetchall()
print '\nindividual records'
for result in results:
    print result
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.