#!/usr/local/bin/python# -*- coding: utf-8 -*-# importMySQLdb# connect to the MySQL server and select the databasesdbhost='localhost'dbuser='user'dbpasswd='password'try:# connect to origin origin=MySQLdb.connect(host=dbhost,user=dbuser,passwd=dbpasswd,db="db1")# connect to sourcesource=MySQLdb.connect(host=dbhost,user=dbuser,passwd=dbpasswd,db="db2")exceptMySQLdb.Error,e:print"Error %s"%esys.exit(1)select_promoCode_records="""SELECT oppc_id, limitedDate FROM ookoodoo_partner_promoCode"""update_promoCode_record="""UPDATE ookoodoo_partner_promoCode SET limitedDate =%s WHERE oppc_id =%s"""org=origin.cursor()src=source.cursor()org.execute(select_promoCode_records)results=org.fetchall()try:forrowinresults:oppc_id,date=row#print int(oppc_id)printoppc_idsrc.execute(update_promoCode_record,(int(date),int(oppc_id)))source.commit()except:print"Error: enable to put data"# bye!origin.close()source.close()
can this be made better. basically i have two databases where i want to pull
data from one and insert into another, i am unsure if i need two connections?
thanks
norman
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
One connection will work. Just preface the tables in the second db with the db
name like:
db2.ookoodoo_partner_promoCode
or for consistency's sake, preface all the table names with the appropriate db
name.
You might also consider reworking your SQL into a single UPDATE statement
involving tables from both databases, so that all the work occurs on the
server instead of pulling records to the client and sending records back.
Unfortunately, I can't help you there, I am running late.
--Eric
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
hello,
i have this code:
can this be made better. basically i have two databases where i want to pull
data from one and insert into another, i am unsure if i need two connections?
thanks
norman
One connection will work. Just preface the tables in the second db with the db
name like:
db2.ookoodoo_partner_promoCode
or for consistency's sake, preface all the table names with the appropriate db
name.
You might also consider reworking your SQL into a single UPDATE statement
involving tables from both databases, so that all the work occurs on the
server instead of pulling records to the client and sending records back.
Unfortunately, I can't help you there, I am running late.
--Eric