我正在编写一段 python 代码,将一个表从一个 mysql 数据库复制到另一个 mysql 数据库。
我遇到了一些问题,比如首先它读取 null,空值作为“None”,我必须将其转换为“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.datetime 替换为 datetime.time ( http://pymotw.com/2/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 将表从一个数据库复制到另一个数据库的任何其他更好的方法。
最佳答案
根据直到 v 5.7 的 mysql 手册,既没有 DATETIME 函数(即使有 datetime TYPE)也没有包支持(datetime.*)。我假设是从源数据库中日期时间的二进制表示生成 datetime.datetime 的 python str
关于python - 将行从表复制到另一个表时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25988162/