简体   繁体   English

我开始使用 sqlalchemy。 我在处理和粘贴示例代码时遇到操作错误

[英]I'm getting started with sqlalchemy. I'm getting operational errors while coping and pasting the example code

I am just trying to get started using sqlalchemy.我只是想开始使用 sqlalchemy。 For whatever reason I can't get anything to work.无论出于何种原因,我都无法正常工作。

I installed sqlalchemy the import alone works.我安装了 sqlalchemy 单独导入工作。 I tried to start following the code on this site:我试着开始关注这个网站上的代码:

https://www.pythoncentral.io/introductory-tutorial-python-sqlalchemy/ https://www.pythoncentral.io/introductory-tutorial-python-sqlalchemy/

The code is as follows:代码如下:

import os
import sys
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine

Base = declarative_base()

class Person(Base):
    __tablename__ = 'person'
    # Here we define columns for the table person
    # Notice that each column is also a normal Python instance attribute.
    id = Column(Integer, primary_key=True)
    name = Column(String(250), nullable=False)

class Address(Base):
    __tablename__ = 'address'
    # Here we define columns for the table address.
    # Notice that each column is also a normal Python instance attribute.
    id = Column(Integer, primary_key=True)
    street_name = Column(String(250))
    street_number = Column(String(250))
    post_code = Column(String(250), nullable=False)
    person_id = Column(Integer, ForeignKey('person.id'))
    person = relationship(Person)

# Create an engine that stores data in the local directory's
# sqlalchemy_example.db file.
engine = create_engine('sqlite:///sqlalchemy_example.db')

# Create all tables in the engine. This is equivalent to "Create Table"
# statements in raw SQL.
Base.metadata.create_all(engine)

I copied and pasted the code to create a table and I'm getting the following error我复制并粘贴了代码以创建表,但出现以下错误

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file (Background on this error at: http://sqlalche.me/e/e3q8 ) sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) 无法打开数据库文件(此错误的背景: http : //sqlalche.me/e/e3q8

I went to http://sqlalche.me/e/e3q8 and it seems to believe that adding pool_pre_ping=True to the engine would help resolve issue.我去了http://sqlalche.me/e/e3q8 ,似乎相信将 pool_pre_ping=True 添加到引擎将有助于解决问题。 It mentions a connection issues, but don't really understand how that can be since it's just creating the sqlite database.它提到了连接问题,但并不真正理解这是怎么回事,因为它只是在创建 sqlite 数据库。

I would really appreciate any advice on how I can fix this issue.我非常感谢有关如何解决此问题的任何建议。

Edit: I put the specific code into my question.编辑:我将特定代码放入我的问题中。

Also I tried performing the code in pythonanywhere and it works as expected.我也尝试在 pythonanywhere 中执行代码,它按预期工作。 Any guidance on what could be wrong with my machine would be appreciated.任何有关我的机器可能出现问题的指导将不胜感激。

So for whatever reason I needed to designate the absolute path of where the database needed to be.因此,无论出于何种原因,我都需要指定数据库所在位置的绝对路径。 I updated my engine to be:我将我的引擎更新为:

sqlite:///C:\\user\\file_path\\test.db sqlite:///C:\\user\\file_path\\test.db

this allowed it to create the database.这允许它创建数据库。 However I'd really prefer it just create the database in the current directory.但是我真的更喜欢它只是在当前目录中创建数据库。 If someone knows what I need to do to get that to work that would be great.如果有人知道我需要做什么才能让它发挥作用,那就太好了。

暂无
暂无

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

相关问题 我在尝试通过 sqlalchemy 连接到远程 postgreSQL 服务器时遇到操作错误,如下所述 - I'm getting an operational error, as described below, when trying to connect to a remote postgreSQL server through sqlalchemy 我在仅访问记录中的 django 管理中遇到操作错误 - I'm getting an operational error in django administration in access records only 我正在尝试开始使用 API 并开始做一些简单的事情,但我最终遇到了多个错误 - I'm trying to get started with APIs and started doing something simple, but I end up getting multiple errors 我刚刚开始使用PythonCard… - I'm just getting started with PythonCard… 我正在使用不同的类似更长的代码,但即使在执行以下代码时,我也会收到错误 - I'm using a different similar longer code, but even while executing the following code I'm getting an error 我用pyglet键盘出错了 - I'm getting errors with pyglet keyboard 刚开始学习django - 我在VS Code中得到“未定义的变量'auth'”而在服务器上出现了另一个错误 - Just started learning django - I'm getting “undefined variable 'auth'” in VS Code and another error on the server 我正在尝试使用 sqlalchemy 连接到我机器上的 SQL Express 服务器,但出现错误 - I'm trying to connect to a SQL Express Server on my machine using sqlalchemy but I'm getting an error 我的代码没有写入文本文件,但我没有收到任何错误 - My code isn't writing to the text file but I'm not getting any errors 我无法打开 snowsql,因为它收到类似 ModuleNotFoundError 的错误 - I'm unable to open snowsql as it is getting errors like ModuleNotFoundError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM