1

我正在编写一段python代码来将一个表从一个mysql db复制到另一个mysql db。

我遇到了一些问题,比如首先它读取 null、空值作为“无”,我必须将其转换为“NULL”。

现在它显示以下错误 -

pymysql.err.InternalError: (1630, u"FUNCTION datetime.datetime does not exist. 
Check the 'Function Name Parsing and Resolution' section in the Reference Manual")

当我打印该行时,我可以看到 datetime.datetime(2014, 8, 25, 8, 24, 51) 的条目。我尝试通过用 datetime.time ( http://pymotw.com/2/datetime/ ) 替换 datetime.datetime 来解决这个问题,但这也失败了。

我的代码如下:

import re
from db import conn_main ### database from where to copy
from db import conn ### database where to copy
import datetime

curr1 = conn_main.cursor()
curr2 = conn.cursor()

query = 'SELECT * FROM mytable limit 10'
curr1.execute(query)
for row in curr1:
    if row[0] is None or not row[0]:
            print "error: empty row",row[0]
            continue
    else:
            print "ROW - %s\n" % str(row)
            row = re.sub('None','NULL',str(row))
            query = 'replace into mytable values ' + str(row)
            curr2.execute(query)

curr2.commit()
curr1.close()
curr2.close()

回溯和输出行:

ROW - (1, '501733938','xyz.emails@gmail.com', None, 'https://www.facebook.com/xyz',
 None, None, None, None, None, '2014-08-10T06:06:33+0000', None, 'xyz', None,
 datetime.datetime(2014, 8, 25, 8, 24, 51), None, None, None, None, None, None, None, 
 None, None, None, None, None, None, None, None, None)

Traceback (most recent call last):
  File "MY_PYTHON_CODE_FILE_PATH", line 390, in <module> curr2.execute(query)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/cursors.py", line 132, in execute result = self._query(query)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/cursors.py", line 271, in _query conn.query(q)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 726, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 861, in _read_query_result result.read()
  File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 1064, in read first_packet = self.connection._read_packet()
  File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 826, in _read_packet packet.check_error()
  File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 370, in check_error raise_mysql_exception(self._data)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/err.py", line 116, in raise_mysql_exception _check_mysql_exception(errinfo)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/err.py", line 112, in _check_mysql_exception raise InternalError(errno, errorvalue)

有人可以帮助消除此错误...或建议任何其他更好的方法来将表从一个数据库复制到 python 中的另一个数据库。

4

2 回答 2

2

根据 mysql 手册直到 v 5.7,既没有功能 DATETIME(即使有 datetime 类型)也没有包支持(datetime. *)。我假设它是 python str 从源数据库中日期时间的二进制表示生成 datetime.datetime

于 2014-09-23T07:30:29.697 回答
1

问题是:您正在尝试将 some_object 的 python 字符串表示形式用作 SQL。那是错误的。 str(datetime)看起来像 datetime.datetime(year, month, day, hour, minute, seconds, milliseconds)

它不是有效的 sql 字符串。如果您知道此列号是什么,则可以将其替换为字符串值,如下所示:

row[dt_index] = row[dt_index].isoformat()

或使用您的数据库接受的具体格式,例如:

row[dt_index] = row[dt_index].strftime('%Y-%m-%d %H:%M:%S')

但我建议使用一些库或参数化查询。以这种方式构建 SQL 是非常糟糕且不安全的解决方案。

于 2014-09-23T07:28:26.383 回答