2014-09-23 22 views
1

我正在寫一塊python代碼,將一個表從一個mysql數據庫複製到另一個mysql數據庫。從表複製行到另一個表的錯誤

我碰到像起初它被讀空,空值「無」,這我不得不轉換爲「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替換datetime.datetime來解決這個問題(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','[email protected]', 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中的另一個。

+0

請問您是否提供完整的回溯? – Nilesh 2014-09-23 06:22:22

+0

'如果row [0]是None'還不夠? 「或不行[0]」代表什麼?你也試過'如果行[0] == None'? – 2014-09-23 06:24:25

+0

@Lafada增加了追蹤 – user2952821 2014-09-23 06:32:56

回答

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這種方式非常糟糕,並且不安全解決方案。

2

根據MySQL手冊至v 5.7,既沒有DATETIME(即使有datetime TYPE)也沒有包支持(datetime。*)。我認爲這是從源數據庫中datetime的二進制表示生成datetime.datetime的python str

相關問題