运行后一直报错,找不到错误在哪里?

from sqlalchemy.dialects.mysql import INTEGER,VARCHAR
from sqlalchemy import Column
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
Base=declarative_base()


# Users表结构
class Users(Base):
    __tablename__='users'
    id = Column(INTEGER , primary_key = True)
    name = Column(VARCHAR(256) , nullable = False)
    age = Column(INTEGER)
    place = Column (VARCHAR(256),nullable = False)

    def __init__(self , id , name , age , place):
        self.id = id
        self.name = name
        self.age = age
        self.place = place

def init_db():
    engine = create_engine(
        "mysql+pymysql ://root :123456 @localhost/hjwzd",
        encoding = "utf-8",
        echo = True,
        autocommit=True     #设置自动提交
    )
    Base.metadata.create_all(engine)
    print('Create table successfully!')
    #建立数据库连接


if __name__=='__main__':
    init_db()
PS C:\Users\86181\Desktop\violeter> & c:/Users/86181/Desktop/violeter/.venv/Scripts/python.exe "c:/Users/86181/Desktop/violeter/Face recognition/create_table.py"
c:\Users\86181\Desktop\violeter\Face recognition\create_table.py:10: MovedIn20Warning: The ``declarative_base()`` function is now available as sqlalchemy.orm.declarative_base(). (deprecated since: 2.0) (Background on SQLAlchemy 2.0 at: https://sqlalche.me/e/b8d9)
  Base=declarative_base()
Traceback (most recent call last):
  File "c:\Users\86181\Desktop\violeter\Face recognition\create_table.py", line 40, in <module>
    init_db()
  File "c:\Users\86181\Desktop\violeter\Face recognition\create_table.py", line 28, in init_db
    engine = create_engine(
             ^^^^^^^^^^^^^^
  File "<string>", line 2, in create_engine
  File "C:\Users\86181\Desktop\violeter\.venv\Lib\site-packages\sqlalchemy\util\deprecations.py", line 281, in warned
    return fn(*args, **kwargs)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\86181\Desktop\violeter\.venv\Lib\site-packages\sqlalchemy\engine\create.py", line 546, in create_engine
    u = _url.make_url(url)
        ^^^^^^^^^^^^^^^^^^
  File "C:\Users\86181\Desktop\violeter\.venv\Lib\site-packages\sqlalchemy\engine\url.py", line 842, in make_url
    return _parse_url(name_or_url)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\86181\Desktop\violeter\.venv\Lib\site-packages\sqlalchemy\engine\url.py", line 908, in _parse_url
    raise exc.ArgumentError(
sqlalchemy.exc.ArgumentError: Could not parse SQLAlchemy URL from string 'mysql+pymysql ://root :123456 @localhost/hjwzd'
PS C:\Users\86181\Desktop\violeter> 
阅读 921
avatarAI BotBETA

答案

在提供的代码中,数据库连接字符串中的URL格式不正确。具体来说,mysql+pymysql ://root :123456 @localhost/hjwzd 中密码部分与用户名之间的空格以及冒号后的空格都应该被移除。正确的格式应该使用冒号(:)来分隔用户名和密码,并且用户名和密码之间不应该有空格。

修正后的代码如下:

from sqlalchemy.dialects.mysql import INTEGER, VARCHAR
from sqlalchemy import Column
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

# ... (其余代码保持不变)

def init_db():
    engine = create_engine(
        "mysql+pymysql://root:123456@localhost/hjwzd",  # 修正后的URL
        encoding="utf-8",
        echo=True,
        autocommit=True  # 设置自动提交
    )
    Base.metadata.create_all(engine)
    print('Create table successfully!')

# ... (其余代码保持不变)

此外,还需要注意,如果你的SQLAlchemy版本是2.0或更高,declarative_base() 已经被视为过时的调用方式,但在这个上下文中,它仍然可以工作,但可能会在未来的版本中完全移除。为了兼容性,建议按照警告信息中提供的链接查看SQLAlchemy 2.0的文档,并更新你的代码。

解释

错误发生在尝试使用create_engine函数创建数据库引擎时,因为它无法解析提供的数据库URL。这通常是因为URL格式不正确。在提供的URL中,用户名(root)和密码(123456)之间的空格以及密码后的空格导致了无法正确解析。移除这些空格后,URL就可以被正确解析了。

2 个回答
新手上路,请多包涵

create_engine的参数不能加空格

sqlalchemy.exc.ArgumentError: Could not parse SQLAlchemy URL from string 'mysql+pymysql ://root :123456 @localhost/hjwzd'

这一行说明了你这个数据库url就有问题呀,去掉所有的空格试试:

mysql+pymysql://root:123456@localhost/hjwzd
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏