Column comparison SQL and Python -
i need compare 2 columns sqllite database (.db) , if found coincidence modify third column. i.e. :
b c ------- 11 1 f 21 3 f 54 21 v 65 4 f 96 9 f 82 5 f 71 11 v 53 5 f
if number of column "b" found in column "a" result in column "c" "v".
column "c" set f rows default before start comparison. i'm using python 2.7 , sqllite 3 don't know how kind of comparison.
update mytable set c = 'v' b in (select mytable)
here complete python program show works:
# __future__ import print_function # uncomment import python 2.7 import sqlite3 connection = sqlite3.connect(':memory:') cursor = connection.cursor() cursor.execute('create table mytable (a integer, b integer, c text)') initial_ab_vals = [(11, 1), (21, 3), (54, 21), (65, 4), (96, 9), (82, 5), (71, 11), (53, 5)] cursor.executemany("insert mytable(a, b, c) values (?, ?, 'f')", initial_ab_vals) print('before update') row in cursor.execute('select * mytable'): print(' {:2d} {:2d} {}'.format(*row)) cursor.execute("update mytable set c = 'v' b in (select mytable)") print() print('after update') row in cursor.execute('select * mytable'): print(' {:2d} {:2d} {}'.format(*row)) connection.commit() connection.close()
output
before update 11 1 f 21 3 f 54 21 f 65 4 f 96 9 f 82 5 f 71 11 f 53 5 f after update 11 1 f 21 3 f 54 21 v 65 4 f 96 9 f 82 5 f 71 11 v 53 5 f
Comments
Post a Comment