Newbie: Database technique
Rich Holm
rholm at mcs.com
Sun Jan 5 13:00:58 EST 2003
Have a look at
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/163605
This class (resultset) really helps to clean up SQL code.
Cheers,
Rich
In article <mailman.1040436689.26831.python-list at python.org>,
joe at overdrivepc.com says...
> 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