SQLAlchemy sqlite3.operational错误

时间:2016-03-07 12:11:00

标签: python sqlite

我正在我的远程ubuntu服务器上开发flask项目。我也使用SQLAlchemy。但是,当我尝试运行此脚本时,我收到sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file错误。

这是我的models.py

import sys
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine

Base = declarative_base()

class Puppy(Base):
    __tablename__ = 'puppy'


    name =Column(String(80), nullable = False)
    id = Column(Integer, primary_key = True)
    description = Column(String(250))

    @property
    def serialize(self):
       """Return object data in easily serializeable format"""
       return {
           'id': self.id,
           'name': self.name,
           'description' : self.description
       }

engine = create_engine('sqlite:///puppies.db')
Base.metadata.create_all(engine)

我关注了Udacity fullstack web基础教程,它在我的本地机器上运行。但是,我无法在服务器上处理它

1 个答案:

答案 0 :(得分:0)

路径可能无法正确解析。尝试:

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

BASE_DIR = os.path.abspath(os.path.dirname(__file__))
DATABASE_PATH = os.path.join(BASE_DIR, 'puppies.db')

Base = declarative_base()

class Puppy(Base):
    __tablename__ = 'puppy'


    name =Column(String(80), nullable = False)
    id = Column(Integer, primary_key = True)
    description = Column(String(250))

    @property
    def serialize(self):
       """Return object data in easily serializeable format"""
       return {
           'id': self.id,
           'name': self.name,
           'description' : self.description
       }

engine = create_engine('sqlite:///' + DATABASE_PATH)
Base.metadata.create_all(engine)