我正在编写一段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 中的另一个数据库。