将行从数据库插入到另一个

时间:2018-08-10 19:26:46

标签: python mysql pymysql

我正在尝试使用pymysql将数据库服务器中的行插入另一个环境中的另一数据库服务器

prod_conn = pymysql.connect(
        prod_common_db_endpoint,
        prod_user,
        prod_password,
        prod_common_table_name)

prod_cursor = prod_conn.cursor()


prod_ConnectFirm_table = prod_cursor.execute("select * from Common.ConnectFirm where FirmId = " + prod_source_firm + ";")
for row in prod_ConnectFirm_table:
    ConnectFirmId = int(row[1])
    ConnectFirmName = (row[2])
    FirmId = int(row[3])
    isDeleted = (row[4])
    tags = (row[5])
    createdDate = (row[6])
    createdBy = int(row[7])
    editedDate = (row[8])
    editedBy = int(row[9])


conn = pymysql.connect(target_common_db_endpoint, user, password, common_schema_name)

cursor = conn.cursor()

cursor.execute("INSERT INTO Common.ConnectFirm(ConnectFirmId, ConnectFirmName, editedDate) VALUES(" + ConnectFirmId + ", " + ConnectFirmName + ", " + editedDate + ", ")")
conn.commit()
print("error inserting")

无法给我这个错误

Traceback (most recent call last):
  File "sql_test_3_d.py", line 109, in <module>
    for row in prod_orionConnectFirm_table:
TypeError: 'int' object is not iterable

1 个答案:

答案 0 :(得分:0)

prod_ConnectFirm_table将包含执行状态,该状态是您无法覆盖的整数值。因此,在执行查询后,如下所示:

prod_ConnectFirm_table = prod_cursor.execute("select * from Common.ConnectFirm where FirmId = " + prod_source_firm + ";")

您需要从游标中获取数据:

data = prod_cursor.fetchall()

“数据”将包含您分析过的元组形式的查询结果。