Python 将行从一个数据库复制到另一个数据库并更改某些值

Python 将行从一个数据库复制到另一个数据库并更改某些值,python,mysql,pymysql,Python,Mysql,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 beta2_Comm

我试图将行从一个数据库服务器复制到另一个数据库服务器,然后为某些字段分配不同的值

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 beta2_Common.Firm where id = " + prod_source_firm + ";")
data = prod_cursor.fetchall()
for row in data:
    oef_name = row[1]
    oef_path = row[2]
    oef_username = row[5]
    oef_password = row[6]
    oef_poolLimit = row[7]
    oef_database = row[8]
    oef_port = row[9]
    oef_serverGroupId = row[10]
    oef_threadWeight = row[11]
    oef_isDeleted = row[12]
    oef_tags = row[13]
    oef_createdBy = row[14]
    oef_createdDate = row[15]
    oef_editedBy = row[16]
    oef_editedDate = row[17]

    if env == "dev":
        oef_server = 'dev-firmdb1-cluster.cluster-xxxxxxxxxxx.us-east-1.rds.amazonaws.com'
        oef_roServer = 'dev-firmdb1-cluster.cluster-ro-xxxxxxxxxxx.us-east-1.rds.amazonaws.com'
    elif env == "beta":
        oef_server = 'beta-cluster.cluster-xxxxxxxxxxx.us-east-1.rds.amazonaws.com'
        oef_roServer = 'beta-cluster.cluster-ro-xxxxxxxxxxx.us-east-1.rds.amazonaws.com'


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

cursor = conn.cursor()


add_Firm_command = ("INSERT INTO Common.Firm "
            "(id, name, path, server, roServer, username, password, poolLimit, database, port, serverGroupId, threadWeight, \
            isDeleted, tags, createdBy, createdDate, editedBy, editedDate) "
            "VALUES (%(oef_last_id)s, %(oef_name)s, %(oef_path)s, %(oef_server)s, %(oef_roServer)s, %(oef_username)s, %(oef_password)s, \
            %(oef_poolLimit)s, %(oef_database)s, %(oef_port)s, %(oef_serverGroupId)s, %(oef_threadWeight)s, %(oef_isDeleted)s, %(oef_tags)s, \
            %(oef_createdBy)s, %(oef_createdDate)s, %(oef_editedBy)s, %(oef_editedDate)s)")
oef_last_id = conn.insert_id()

Firm_values = {
    'oef_last_id' : oef_last_id,
    'oef_name' : oef_name,
    'oef_path' : oef_path,
    'oef_server' : oef_server,
    'oef_roServer' : oef_roServer,
    'oef_username' : oef_username,
    'oef_password' : oef_password,
    'oef_poolLimit' : oef_poolLimit,
    'oef_database' : oef_database,
    'oef_port' : oef_port,
    'oef_serverGroupId' : oef_serverGroupId,
    'oef_threadWeight' : oef_threadWeight,
    'oef_isDeleted' : oef_isDeleted,
    'oef_tags' : oef_tags,
    'oef_createdBy' : oef_createdBy,
    'oef_createdDate' : oef_createdDate,
    'oef_editedBy' : oef_editedBy,
    'oef_editedDate' : oef_editedDate
}


cursor.execute(add_Firm_command, Firm_values)

conn.commit()
cursor.close()
conn.close()
conn.rollback()
print("error inserting")
我得到的错误是“您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以了解在第1行“database、port、serverGroupId、threadWeight”附近使用的正确语法”:

回溯(最近一次呼叫最后一次):
文件“sql_test_4_a.py”,第264行,在
execute(add_orionEclipseFirm_命令,orionEclipseFirm_值)
文件“/usr/local/lib/python3.6/site packages/pymysql/cursors.py”,执行中的第170行
结果=self.\u查询(查询)
文件“/usr/local/lib/python3.6/site packages/pymysql/cursors.py”,第328行,在查询中
连接查询(q)
文件“/usr/local/lib/python3.6/site packages/pymysql/connections.py”,查询中的第516行
self.\u受影响的行=self.\u读取\u查询\u结果(无缓冲=无缓冲)
文件“/usr/local/lib/python3.6/site packages/pymysql/connections.py”,第727行,在读取查询结果中
结果。读取()
文件“/usr/local/lib/python3.6/site packages/pymysql/connections.py”,第1066行,已读
第一个\u数据包=self.connection.\u读取\u数据包()
文件“/usr/local/lib/python3.6/site packages/pymysql/connections.py”,第683行,在读取数据包中
packet.check_error()
文件“/usr/local/lib/python3.6/site packages/pymysql/protocol.py”,第220行,检查错误
err.raise\u mysql\u异常(self.\u数据)
raise\u mysql\u异常中的文件“/usr/local/lib/python3.6/site packages/pymysql/err.py”,第109行
提高错误等级(errno,errval)
pymysql.err.ProgrammingError:(1064,“您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,以了解在第1行“database,port,serverGroupId,threadWeight”附近使用的正确语法”)

知道如何修复此错误吗?

数据库驱动程序正在告诉您

“数据库、端口、服务器组ID、线程权重,”

查看您的SQL语句,您在语句字符串中包含反斜杠(可能是针对continuaton行)

您不需要这些字符。您可以将语句用括号括起来(正如您已经做的那样),并使语句的每一行成为一个单独的字符串。python解释器将自动连接它们。您需要在每一行的末尾留一个空格,以避免连接单独的单词

例如:

>>> s = ("foo"
...      "bar")
>>> s
'foobar'    # <- words concatenated
>>> s = ("foo "
...      "bar")
>>> s
'foo bar'   # <- words separated
>>> s = ("foo"
...      "bar")
>>> s
'foobar'    # <- words concatenated
>>> s = ("foo "
...      "bar")
>>> s
'foo bar'   # <- words separated
add_Firm_command = ("INSERT INTO Common.Firm "
            "(id, name, path, server, roServer, username, password, poolLimit, database, port, serverGroupId, threadWeight, "
            "isDeleted, tags, createdBy, createdDate, editedBy, editedDate) "
            "VALUES (%(oef_last_id)s, %(oef_name)s, %(oef_path)s, %(oef_server)s, %(oef_roServer)s, %(oef_username)s, %(oef_password)s, "
            "%(oef_poolLimit)s, %(oef_database)s, %(oef_port)s, %(oef_serverGroupId)s, %(oef_threadWeight)s, %(oef_isDeleted)s, %(oef_tags)s, "
            "%(oef_createdBy)s, %(oef_createdDate)s, %(oef_editedBy)s, %(oef_editedDate)s)")