import sqlite3 conn = sqlite3.connect('qr_results1.db3') c = conn.cursor out = open('results.txt', 'w') n = 1 for x in range(1, 30): for y in range(1, 30): c.execute('select value from i where row=? and column=?', (x, y)) i = c.fetchone()[0] c.execute('select value from j where row=? and column=?', (x, y)) j = c.fetchone()[0] out.write('constraint %d: %s < %s\n' % (n, i, j)) n += 1 out.close() conn.close()