**TypeError:“NoneType”对象不可迭代**

PetersServer1

我想使用 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()
窝旺

您的df = run_query(...)行中有错字。

你用了逗号;但你应该使用 %.

IE


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))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TypeError:“NoneType”对象不可迭代

Python - TypeError:“NoneType”对象不可迭代

TypeError:“ NoneType”对象在Python中不可迭代

TypeError:“ NoneType”对象不可迭代pyscreeze

TypeError:“ NoneType”对象不可迭代WTForms FieldSelect

无法解决“NoneType”对象不可迭代的TypeError

-- TypeError: 无法解包不可迭代的 NoneType 对象

NoneType 对象不可迭代

“ NoneType”对象不可迭代

TypeError: 'NoneType' 对象不可迭代(h5 文件)

列表的Python问题(TypeError:“ NoneType”对象不可迭代)

Scrapy代码引发TypeError:“ NoneType”对象不可迭代

TypeError: 'NoneType' 对象在覆盖旧值时不可迭代

TypeError:无法解压缩不可迭代的NoneType对象

Python递归函数:TypeError:'NoneType'对象不可迭代

`TypeError:`subprocess.call` 中的“NoneType”对象不可迭代

TypeError:使用zip_longest时,“ NoneType”对象不可迭代

Python TypeError:“ NoneType”对象不可迭代,但定义了每个变量

熊猫read_sql中的“ TypeError:'NoneType'对象不可迭代”

遍历Python列表获取TypeError:'NoneType'对象不可迭代

TypeError: 'NoneType' 对象不可迭代从初学者 2

NoneType对象不可迭代错误

TypeError对象不可迭代

在ExtraTreesClassifier中显示为NoneType的feature_importances_:TypeError:'NoneType'对象不可迭代

Flask TypeError:“ NoneType”类型的参数不可迭代

如何修复 TypeError:“NoneType”不可迭代?

TypeError:“ RegexValidator”对象不可迭代

TypeError:“ ShuffleSplit”对象不可迭代

TypeError:“ KFold”对象不可迭代