Newbie: Database technique

Joe 'shmengie' Brown joe at overdrivepc.com
Fri Dec 20 21:10:37 EST 2002


Currently rewriting an aplication using Python and MySql.  The Previous
incarnation was written in Oracle/Oracle Forms.  Since I'm a newbie to
Python, I'm writing some kludgy code, and have yet to stumble across a
cleaner way.

There is a bunch of tables to convert and am changing some field names in
the process.  So this kludgy code is appearing in my editor. :o

fields=("User_Role", "PRN_User", "Name", "Home_Phone", "Office_phone",
"Mobile_phone", "Pager", "Fax_No", "Active_user","User_ID")
oc.execute("SELECT " + ( ", ".join(fields)) + " FROM my_users")

i = 1
while 1:
    rec = oc.fetchone()
    if rec == None: break  #my_users

    fields = ("Organization_ID", "UserID", "Name", "Home_Phone",
    "Office_Phone", "Mobile_Phone", "Pager", "Fax_No", "Active_User",
    "User_Role")

    mc.execute("INSERT INTO my_users (" + ",".join(fields) + ") values
    (" + ("""%s,""" * (len(fields)-1)) + """%s)""", [1,] + rec[1:9] +
    list( role_id) ) print "%5d %-20s %-20s transferred" % (i, rec[1],
    rec[3]) id=mc.insert_id()

Suggestions to make this insert operation more readable?  Moslty I've used
the tuple 'fields' to count the number of columns selected for insert
statements...

I'll be writing my frist classes soon :)

Haven't used a generic db class for dynamic access, considering SQLDict.  I
would appreciate recommendations.

TIA,
-Joe





More information about the Python-list mailing list