Skip to content

Instantly share code, notes, and snippets.

@chintanop
Created July 4, 2014 20:56
Show Gist options
  • Select an option

  • Save chintanop/d0c189c2eebd34ce6b68 to your computer and use it in GitHub Desktop.

Select an option

Save chintanop/d0c189c2eebd34ce6b68 to your computer and use it in GitHub Desktop.

Revisions

  1. chintanop revised this gist Jul 4, 2014. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -15,8 +15,6 @@ def copy_table(self, table_name, source_conn, target_conn):
    "INSERT into "+table_name+"( "+columns+" ) "
    "values ("+values_q+")"
    )
    print insrt_sql
    print row
    qdict = {}
    for indx, row_val in enumerate(row):
    qdict["key"+str(indx)] = row_val
  2. chintanop created this gist Jul 4, 2014.
    24 changes: 24 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    def copy_table(self, table_name, source_conn, target_conn):
    """ copies all records from table_name from source to target db """

    scursor = source_conn.cursor()
    tcursor = target_conn.cursor()

    scursor.execute("SELECT * from "+table_name)

    desc = scursor.description
    columns = ",".join([col[0] for col in desc])
    values_q = ",".join(["%(key"+str(indx)+")s" for indx, col in enumerate(desc)])

    for row in scursor.fetchall():
    insrt_sql = (
    "INSERT into "+table_name+"( "+columns+" ) "
    "values ("+values_q+")"
    )
    print insrt_sql
    print row
    qdict = {}
    for indx, row_val in enumerate(row):
    qdict["key"+str(indx)] = row_val

    tcursor.execute(insrt_sql, qdict)