[英]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.