繁体   English   中英

TypeError TypeError:connect() 参数 3 必须是 str,而不是 list

[英]TypeError TypeError: connect() argument 3 must be str, not list

我是 python、sqlalchemy 和 flask 的新手。 现在我正在尝试做一个简单的数据库插入程序,将艺术家名称插入到我已经创建的数据库中。

from flask import Flask, render_template, request, redirect
from flask_mysqldb import MySQL
import yaml
app = Flask(__name__)
db = yaml.load(open('db.yaml'))
app.config['MYSQL_HOST'] = db['mysql_host']
app.config['MYSQL_USER'] = db['mysql_user']
app.config['MYSQL_PASSWORD'] = ['mysql_password']
app.config['MYSQL_DB'] = ['mysql_db']
mysql = MySQL(app)
@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        # Fetch form data
        userDetails = request.form
        artist_name = userDetails['artist_name']
        
        cur = mysql.connection.cursor()
        cur.execute("INSERT INTO p_artist ( artist_name) VALUES (%s)", (artist_name))
        mysql.connection.commit()
        cur.close()
        return 'inserted'
        
    return render_template('index.html')
if __name__ == '__main__':
    app.run(debug=True)
HTML
 <form method="POST" action="">
    <br>
      artist_name <input type="text" name="artist_name" />
    <br>
      <input type="submit">
   </form>

db.yaml

mysql_host: 'localhost'
mysql_user: 'root'
mysql_password: 
mysql_db:** 

这是类型错误

TypeError
TypeError: connect() argument 3 must be str, not list
Traceback (most recent call last)
    File "C:\Users\luvch\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\flask\app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
    File "C:\Users\luvch\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\flask\app.py", line 2450, in wsgi_app
                try:
                    ctx.push()
                    response = self.full_dispatch_request()
                except Exception as e:
                    error = e
                    response = self.handle_exception(e)
                except:  # noqa: B001
                    error = sys.exc_info()[1]
                    raise
                return response(environ, start_response)
            finally:
    File "C:\Users\luvch\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\flask\app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
    File "C:\Users\luvch\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\flask\_compat.py", line 39, in reraise
    raise value
    File "C:\Users\luvch\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\flask\app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
    File "C:\Users\luvch\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\flask\app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
    File "C:\Users\luvch\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\flask\app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
    File "C:\Users\luvch\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\flask\_compat.py", line 39, in reraise
    raise value
    File "C:\Users\luvch\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\flask\app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
    File "C:\Users\luvch\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\flask\app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
    File "C:\Users\luvch\OneDrive\Desktop\New folder\hope.py", line 25, in index
    cur = mysql.connection.cursor()
    File "C:\Users\luvch\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\flask_mysqldb\__init__.py", line 94, in connection
    ctx.mysql_db = self.connect
    File "C:\Users\luvch\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\flask_mysqldb\__init__.py", line 81, in connect
    return MySQLdb.connect(**kwargs)
    File "C:\Users\luvch\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\MySQLdb\__init__.py", line 130, in Connect
    return Connection(*args, **kwargs)
    File "C:\Users\luvch\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\MySQLdb\connections.py", line 185, in __init__
    super().__init__(*args, **kwargs2)
    TypeError: connect() argument 3 must be str, not list
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.
You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:
    dump() show*emphasized text*s all variables in the frame
    dump(obj) dumps all that's known about the object

对此的任何帮助将不胜感激

看起来像一个简单的错字,您将数据库配置详细信息加载为db = yaml.load(open('db.yaml')但未在app.config中提及:

app.config['MYSQL_PASSWORD'] = ['mysql_password']
app.config['MYSQL_DB'] = ['mysql_db']

它应该是:

app.config['MYSQL_PASSWORD'] = db['mysql_password']
app.config['MYSQL_DB'] = db['mysql_db']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM