简体   繁体   English

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

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

I am new to python, sqlalchemy and flask.我是 python、sqlalchemy 和 flask 的新手。 Right now i am trying to do a simple database insert program where artist_name is inserted into a database I have already created.现在我正在尝试做一个简单的数据库插入程序,将艺术家名称插入到我已经创建的数据库中。

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:** 

this is the type error这是类型错误

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

Any help with this will be much appreciated对此的任何帮助将不胜感激

Looks like a simple typo, you're loading your database config details as db = yaml.load(open('db.yaml') but not referring to that in the app.config :看起来像一个简单的错字,您将数据库配置详细信息加载为db = yaml.load(open('db.yaml')但未在app.config中提及:

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

It should be:它应该是:

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

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

相关问题 TypeError:strptime()参数1必须是str,而不是list - TypeError: strptime() argument 1 must be str, not list TypeError: join() 参数必须是 str 或 bytes,而不是 'list' - TypeError: join() argument must be str or bytes, not 'list' TypeError:必须为str,而不是list - TypeError: must be str, not list 如何修复:TypeError:normalize()参数2必须为str,而不是列表 - How to fix : TypeError: normalize() argument 2 must be str, not list TypeError:write()参数必须是str,而不是写入文件时的列表 - TypeError: write() argument must be str, not list while writing to file Unicodedata.normalize : TypeError: normalize() 参数 2 必须是 str,而不是列表 - Unicodedata.normalize : TypeError: normalize() argument 2 must be str, not list 类型错误:write() 参数必须是 str,而不是 HTTPResponse - TypeError: write() argument must be str, not HTTPResponse 类型错误:write() 参数 1 必须是 unicode,而不是 str - TypeError: write() argument 1 must be unicode, not str 类型错误:join() 参数必须是 str 或字节,而不是“PosixPath” - TypeError: join() argument must be str or bytes, not 'PosixPath' TypeError: write() 参数必须是 str,而不是 Entry - TypeError: write() argument must be str, not Entry
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM