How to compare 2 lists and merge them in Python/MySQL?
Posted
by NJTechGuy
on Stack Overflow
See other posts from Stack Overflow
or by NJTechGuy
Published on 2010-04-09T19:48:30Z
Indexed on
2010/04/09
20:13 UTC
Read the original article
Hit count: 289
I want to merge data. Following are my MySQL tables. I want to use Python to traverse though a list of both Lists (one with dupe = 'x' and other with null dupes).
For instance :
a b c d e f key dupe
--------------------
1 d c f k l 1 x
2 g h j 1
3 i h u u 2
4 u r t 2 x
From the above sample table, the desired output is :
a b c d e f key dupe
--------------------
2 g c h k j 1
3 i r h u u 2
What I have so far :
import string, os, sys
import MySQLdb
from EncryptedFile import EncryptedFile
enc = EncryptedFile( os.getenv("HOME") + '/.py-encrypted-file')
user = enc.getValue("user")
pw = enc.getValue("pw")
db = MySQLdb.connect(host="127.0.0.1", user=user, passwd=pw,db=user)
cursor = db.cursor()
cursor2 = db.cursor()
cursor.execute("select * from delThisTable where dupe is null")
cursor2.execute("select * from delThisTable where dupe is not null")
result = cursor.fetchall()
result2 = cursor2.fetchall()
for cursorFieldname in cursor.description:
for cursorFieldname2 in cursor2.description:
if cursorFieldname[0] == cursorFieldname2[0]:
### How do I compare the record with same key value and update the original row null field value with the non-null value from the duplicate? Please fill this void...
cursor.close()
cursor2.close()
db.close()
Thanks guys!
© Stack Overflow or respective owner