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

时间:2018-08-16 13:54:01

标签: 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_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'附近使用的正确语法” )“:

    Traceback (most recent call last):
  File "sql_test_4_a.py", line 264, in <module>
    cursor.execute(add_orionEclipseFirm_command, orionEclipseFirm_values)
  File "/usr/local/lib/python3.6/site-packages/pymysql/cursors.py", line 170, in execute
    result = self._query(query)
  File "/usr/local/lib/python3.6/site-packages/pymysql/cursors.py", line 328, in _query
    conn.query(q)
  File "/usr/local/lib/python3.6/site-packages/pymysql/connections.py", line 516, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/usr/local/lib/python3.6/site-packages/pymysql/connections.py", line 727, in _read_query_result
    result.read()
  File "/usr/local/lib/python3.6/site-packages/pymysql/connections.py", line 1066, in read
    first_packet = self.connection._read_packet()
  File "/usr/local/lib/python3.6/site-packages/pymysql/connections.py", line 683, in _read_packet
    packet.check_error()
  File "/usr/local/lib/python3.6/site-packages/pymysql/protocol.py", line 220, in check_error
    err.raise_mysql_exception(self._data)
  File "/usr/local/lib/python3.6/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'database,             port,             serverGroupId,             threadWeight,' at line 1")

任何想法,如何解决此错误?

1 个答案:

答案 0 :(得分:0)

数据库驱动程序告诉您

  

“数据库,端口,serverGroupId,threadWeight”附近

在您的SQL语句中,您将在语句字符串中包含反斜线(大概是行连续性)。

您不需要这些字符。您可以将语句包装在括号中(就像已经完成的一样),然后将语句的每一行都设置为单独的字符串。 python解释器将自动连接它们。您需要在每行的末尾留一个空格,以避免串联单独的单词。

例如:

>>> 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)")