Hi Guys!

I am a Python noob. I wanted to merge data and eliminate duplicates. I have the solution. Now I am stuck at dumping the dict contents to MySQL. Any help is really appreciated.

Data :

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

Desired output :

a b c d e f key dupe
--------------------
2 g c h k j 1
3 i r h u u 2

import string, os, sys
import csv
import MySQLdb
import pickle
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 myTable")
rows = cursor.fetchall()
data = dict()
for row in rows:
	key, primary = row[0], row[1]
	if key not in data:
		data[key] = list(row[:-1])
	else:
		for i in range(len(row)-1):
			if data[key][i] is None or (not primary and row[i] is not None):
				data[key][i] = row[i]

print data displays something like this :

{'1': , '2': , '3': }

How to insert this data into MySQL? Thanks a lot!

Got it from StackOverFlow instead!

sql='''\
INSERT INTO temp (id, field1, field2, field3, field4, field5, field6, field7)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
'''
cursor.executemany(sql, data.values())
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.