ThinkCode
ThinkCode

Reputation: 7961

Pythonic way of intelligently loading data using field mapping table?

I have a complicated table (few hundred columns) and I get data in csv files every now and then. All I have to do is update the table with that of the data in the csv file.

This is what I use right now : nothing fancy (not using Python power I guess?). If someone can translate this to something Pythonic, that will be awesome!

The idea is to match column names from MyTable and Field_Mapping and make the updates accordingly. If the column has a multiply value, it will multiply by that value while inserting/updating.

Sorry for the messy Python code, tried my best to make it readable.

MyDB.MyTable
------------

PK DateAdded  Firm Addr1     Website
------------------------------------
1  2011-01-01 ABC  1 Main St abc.com

MyDB.Field_Mapping
------------------

SourceColumns TargetTableColumns Multiply
-----------------------------------------
PK          PK           
webaddr       Website
address       Addr1
assets        value              x1000

Python code :

import string, os, sys

# DB Conn String here

cursor = db.cursor()
cursor2 = db.cursor()
cursor3 = db.cursor()

TableName = sys.argv[1]

cursor.execute("select * from `" + TableName + "` limit 1")

for cursorFieldname in cursor.description:
    cursor2.execute("select TargetTableColumns from MyDB.Field_Mapping where FDIC = \"" + cursorFieldname[0] + "\"")
    row = cursor2.fetchone()

    if row > -1:
       cursor2.execute("alter table `" + TableName + "` change `" + cursorFieldname[0] + "` `" + str(row[0]) + "` varchar(255)")

cursor3.execute("create index PKIndx on `" + filename + "`(PK);")
cursor3.execute("insert ignore into MyDB.MyTable (PK, dateadded) select PK, now() from `" + TableName + "`;")
cursor3.execute("select count(*) from `" + filename + ";")
row2 = cursor3.fetchone()

if str(row2[0]) > "1000": #Deleting PKs > 1000
    cursor3.execute("delete from MyDB.MyTable where PK < \"a\" and PK not in (select PK from `" + filename + "`);")

cursor.execute("select * from `" + filename + "` limit 1")

for cursorFieldname in cursor.description:
    cursor2.execute("select * from MyDB.MyTable limit 1")
    for cursorFieldname2 in cursor2.description:
        if cursorFieldname[0].lower() == cursorFieldname2[0].lower():
         cursor3.execute("select multiply from MyDB.Field_Mapping where TargetTableColumns = \"" + cursorFieldname[0] + "\"")
         row2 = cursor3.fetchone()

         if str(row2[0]) == "x1000":
            cursor3.execute("update MyDB.MyTable as a, `" + filename + "` as b set a.`" + cursorFieldname[0] + "` = b.`" + cursorFieldname[0] + "`*1000 where a.PK = b.PK;")

         elif str(row2[0]) == "%":
            cursor3.execute("update MyDB.MyTable as a, `" + filename + "` as b set a.`" + cursorFieldname[0] + "` = round(b.`" + cursorFieldname[0] + "`, 2) where a.PK = b.PK;")

         else:
            if cursorFieldname[0] == "addr1":
            cursor3.execute("update MyDB.MyTable as a, `" + filename + "` as b set a.`" + cursorFieldname[0] + "` = b.`" + cursorFieldname[0] + "` where a.PK = b.PK and b.`" + cursorFieldname[0] + "` != \"Main Street\";")

        elif cursorFieldname[0] != "PK":
            if cursorFieldname[0].lower() == "website":
                cursor3.execute("update `" + filename + "` set website = lcase(website)")
            cursor3.execute("update MyDB.MyTable as a, `" + filename + "` as b set a.`" + cursorFieldname[0] + "` = b.`" + cursorFieldname[0] + "` where a.PK = b.PK;")

cursor.close()
cursor2.close()
cursor3.close()
db.close()

Upvotes: 2

Views: 179

Answers (1)

Jon Cage
Jon Cage

Reputation: 37500

I would start by expressing the code above as a plain english algorithm. Breaking a problem down into the fundamental bits of what you want to get done rather then how you want to do them often simplifies things considerably.

Have a look into database abstraction layers if you want to simplify access. SQLAlchemy is pretty good from what I've heard.

Personally, if that code you've got there works (it's not exactly a lot of code), why change it? :-)

Upvotes: 1

Related Questions