Menu

code correction

Help
2010-04-23
2012-09-19
  • norman khine

    norman khine - 2010-04-23

    hello,

    i have this code:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    #!/usr/local/bin/python
    # -*- coding: utf-8 -*-
    # 
    import MySQLdb
    # connect to the MySQL server and select the databases
    dbhost = 'localhost'
    dbuser = 'user'
    dbpasswd = 'password'
    
    try:
    # connect to origin 
        origin = MySQLdb.connect (host = dbhost,
                                    user = dbuser, 
                                    passwd = dbpasswd,
                                    db = "db1")
    # connect to source
        source = MySQLdb.connect (host = dbhost,
                                    user = dbuser, 
                                    passwd = dbpasswd,
                                    db = "db2")
    
    except MySQLdb.Error, e:
        print "Error %s" % e
        sys.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:    
        for row in results:
            oppc_id, date = row 
            #print int(oppc_id)
            print oppc_id
            src.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

     
  • Eric Strand

    Eric Strand - 2010-04-23

    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

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.