**錯誤類型:無法迭代的'NoneType'物件**
P粉141035089
P粉141035089 2024-04-06 10:26:38
0
1
367

我想使用 ssh 隧道來存取 Mysql 資料庫。 不幸的是,此程式碼的體系結構似乎不支援我的查詢格式,因為該函數似乎與我的查詢格式不相容。

/Users/peter/PycharmProjects/MySqlLocalhostTest/venv/lib/python3.10/site-packages/paramiko/transport.py:236: CryptographyDeprecationWarning: Blowfish has been deprecated
  "class": algorithms.Blowfish,
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 96, in <module>
  File "<input>", line 65, in run_query
  File "/Users/peter/PycharmProjects/MySqlLocalhostTest/venv/lib/python3.10/site-packages/pandas/io/sql.py", line 399, in read_sql_query
    return pandas_sql.read_query(
  File "/Users/peter/PycharmProjects/MySqlLocalhostTest/venv/lib/python3.10/site-packages/pandas/io/sql.py", line 2081, in read_query
    columns = [col_desc[0] for col_desc in cursor.description]
TypeError: 'NoneType' object is not iterable

像 (Select * From Table) 這樣的簡單語句可以完美地運作。

我必須在 mysql 語法或函數中更改什麼才能使其工作?

抱歉,我是個菜鳥。我將不勝感激任何建議或幫助。

import pandas as pd
import pymysql
import logging
import sshtunnel
from sshtunnel import SSHTunnelForwarder


ssh_host = '172.xxx.xxx.xxx'
ssh_username = 'root'
ssh_password = 'xxxxxxx'
database_username = 'root'
database_password = 'xxxxxxxx'
database_name = 'analysis-db'
localhost = '127.0.0.1'


def open_ssh_tunnel(verbose=False):
    """Open an SSH tunnel and connect using a username and password.

    :param verbose: Set to True to show logging
    :return tunnel: Global SSH tunnel connection
    """

    if verbose:
        sshtunnel.DEFAULT_LOGLEVEL = logging.DEBUG

    global tunnel
    tunnel = SSHTunnelForwarder(
        (ssh_host, 22),
        ssh_username=ssh_username,
        ssh_password=ssh_password,
        remote_bind_address=('127.0.0.1', 3306)
    )

    tunnel.start()


def mysql_connect():
    """Connect to a MySQL server using the SSH tunnel connection

    :return connection: Global MySQL database connection
    """

    global connection

    connection = pymysql.connect(
        host='127.0.0.1',
        user=database_username,
        passwd=database_password,
        db=database_name,
        port=tunnel.local_bind_port
    )


def run_query(sql):
    """Runs a given SQL query via the global database connection.

    :param sql: MySQL query
    :return: Pandas dataframe containing results
    """

    return pd.read_sql_query(sql, connection)


def mysql_disconnect():
    """Closes the MySQL database connection.
    """

    connection.close()


def close_ssh_tunnel():
    """Closes the SSH tunnel connection.
    """

    tunnel.close

day_of_test = "24.01.22"
gold_close = 1000
sap_close= 1000
bonds_close= 1000
btc_est= 1000
btc_actual= 1000
actual_difference = 1000

open_ssh_tunnel()
mysql_connect()

query = ("INSERT INTO `ML1`(`day_of_test`, `gold_close`, `sap_close`, `bonds_close`, `btc_est`, `btc_actual`,`actual_difference`) VALUES (%s, %s, %s, %s, %s, %s, %s);"%
        (day_of_test, gold_close, sap_close, bonds_close, btc_est, btc_actual, actual_difference))

df = run_query(query)
      

print(df)
df.head()
mysql_disconnect()
close_ssh_tunnel()

P粉141035089
P粉141035089

全部回覆(1)
P粉358281574

您的 df = run_query(...) 行中有一個拼字錯誤。

你使用了逗號;但你應該使用%。

df = run_query("INSERT INTO `ML1`(`day_of_test`, `gold_close`, `sap_close`, `bonds_close`, `btc_est`, `btc_actual`,`actual_difference`) VALUES (%s, %s, %s, %s, %s, %s, %s);" %
        (day_of_test, gold_close, sap_close, bonds_close, btc_est, btc_actual, actual_difference))
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!