如題所說,小白一枚,寫了一組pytho代碼,基本上實作的就是通過sshtunnel, pymysql遠程連接資料庫查詢并獲取資料。
若運行環境為python2.7時沒問題,但換成python3.8就會一直不回傳查詢結果(也不報錯,長時間無輸出,直至強制停止運行),連接資料庫的代碼大至如下,有沒有大神幫忙看一下是為什么?
import pymysql as MySQLdb
from sshtunnel import SSHTunnelForwarder
def runSQLDB1(sql):
with SSHTunnelForwarder(
('11.11.11.11',2222),
ssh_username="$user",
ssh_password="$pass",
# ssh_pkey="/home/kk/key.pem" ,
remote_bind_address=('$address',1111)
) as server1:
DB1 = MySQLdb.connect(host='127.0.0.1',
port=server1.local_bind_port,
user='$user',
password='$pass',
db='$db',
charset='utf8',
cursorclass=MySQLdb.cursors.DictCursor)
try:
CUSOR = DB1.cursor()
CUSOR.execute(sql)
result = CUSOR.fetchall()
except BaseException as e:
print(e)
finally:
CUSOR.close()
return result
uj5u.com熱心網友回復:
大神,我是純小白。不過我知道,Python2, Python3 是不兼容的,所以環境不同,結果可能會也不同。uj5u.com熱心網友回復:
Python2, Python3不兼容!uj5u.com熱心網友回復:
我知道應該是版本兼容問題,但我想知道如果想在3.8下面運行,要怎么處理呢?uj5u.com熱心網友回復:
我也是純小白啊,代碼都是東拼西湊抄來的,所以一出問題就二眼一抹黑了~~~
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/23136.html
上一篇:python 讀寫共享檔案
