下面提到的是我在python中創建的數據庫配置文件。在執行代碼時我沒有收到restaurant.db文件(它是在執行時自動創建的)。請檢查我的代碼。謝謝無法在python中創建restaurant.db文件
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 Restaurant(Base):
__tablename__ = 'restaurant'
id = Column(Integer, primary_key=True)
name = Column(String(250), nullable=False)
class MenuItem(Base):
__tablename__ = 'menu_item'
name = Column(String(80), nullable=False)
id = Column(Integer, primary_key=True)
description = Column(String(250))
price = Column(String(8))
course = Column(String(250))
restaurant_id = Column(Integer, ForeignKey('restaurant.id'))
restaurant = relationship(Restaurant)
engine = create_engine('sqlite:///restaurantmenu.db')
Base.metadata.create_all(engine)
這路徑「sqlite:///restaurantmenu.db」表示你的數據庫文件應該在文件系統的根文件夾中創建。你有寫權限嗎?或者,也許你想寫一些像「sqlite:///home/user/restaurantmenu.db」? – MihanEntalpo
Did not get you ...請再次解釋我.. – hatim
你想在哪創建restaurantmenu.db文件?在什麼文件夾?如果你想創建在「當前」文件夾中,你的路徑應該是:「sqlite:// {cur_path} /restaurantmenu.db」.format(cur_path = os.path.dirname(os.path.realpath(__ file__)) ) – MihanEntalpo