[英]Getting error from copying row from table to another table
我正在編寫一段python代碼,將一個表從一個mysql db復制到另一個mysql db。
我遇到了一些問題,比如它首先讀取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.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中。
根據vql 5.7的mysql手冊,既沒有DATETIME功能(即使有日期時間TYPE)也沒有包支持(datetime。 * )。 我假設它是python str,它從源數據庫中datetime的二進制表示生成datetime.datetime
問題是:你正在嘗試使用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這樣的方式是非常糟糕和不安全的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.