import json, MySQLdb
from lib.dpattern import DP
from lib.mysql import connection

cursor = connection.cursor()
cursor2 = connection.cursor()
query = "SELECT `sno`,`data` FROM `data_new` WHERE 1 and `dp` is Null "
cursor.execute(query)

results = cursor.fetchall()
dp = DP(0.2)
for row in results :
	print row[0]
	dps = dp.d_patterns(json.loads(row[1]))
	query = "update `data_new` set dp = '%s' where `sno` = %s" %(json.dumps(dps).replace('\'', '\\\''), str(row[0]))
	try :
		cursor2.execute(query)
		connection.commit()
	except Exception as e:
		print e

import json, MySQLdb
from lib.dpattern import DP
from lib.mysql import connection

cursor = connection.cursor()
cursor2 = connection.cursor()
query = "select id, did, data from r21578 where type='train' and dp = ''"
cursor.execute(query)
cursor.scroll(0, 'absolute')
dp = DP(0.3)
while True:
	c = cursor.fetchone()
	if not c:
		break
	# print json.loads(c[-1]), c[1]
	# break
	print c[0],
	dps = dp.d_patterns(json.loads(c[-1]))
	# print dps
	query = "update r21578 set dp = '%s' where id = %s" %(json.dumps(dps).replace('\'', '\\\''), str(c[0]))
	cursor2.execute(query)
	connection.commit()