0

I have created a database in Flask Python

app = Flask(__name__)
app.config['SQALCHEMY_DATABASE_URI'] = "sqlite:///site.db"
db = SQLAlchemy(app)

class user(db.Model):
    ID = db.Column(db.Integer,primary_key=True)
    username = db.Column(db.String(20),unique=True,nullable=False)
    email = db.Column(db.String(20),unique=True,nullable=False)
    password = db.Column(db.String(60),nullable=False)


    def __repr__(self):
        return f"User Details \n Name:{self.username}\n Email:{self.email} \n"

class product(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    item_name = db.Column(db.String(10),nullable=False,unique=False)
    price = db.Column(db.Integer,unique=False,nullable=False)
    Currency = db.Column(db.String(3),unique=False,nullable=False)
    Category = db.Column(db.String(15),unique=False,nullable=True)
    product_image = db.Column(db.String(20),unique=False,nullable=False)

    def __repr__(self):
        return "Product Details \n Name: {self.item_name} \n price: {self.price}\n"

db.create_all()

I am currently creating an e-commerce site as a project and I'm following Corey Schafer's online tutorial on Flask to do so

A file named site.db is supposed to be created , I have searched my entire project repository by no file named site.db was created

Where did I go wrong? Please help me! I am beginner programmer trying to learn web development with flask

2
  • Have you tried creating the database with db.create_all() in the console? Commented Nov 11, 2021 at 15:41
  • @patrickyoder I did Commented Nov 11, 2021 at 15:50

1 Answer 1

1

You are not saving your database in your project directory, but at your user´s root level. Instead of this: sqlite:///site.db. You should use something like this: sqlite:////absolute/path/to/foo.db

From documentation

#Unix/Mac (note the four leading slashes)
sqlite:////absolute/path/to/foo.db

#Windows (note 3 leading forward slashes and backslash escapes)
sqlite:///C:\\absolute\\path\\to\\foo.db

#Windows (alternative using raw string)
r'sqlite:///C:\absolute\path\to\foo.db'

Sign up to request clarification or add additional context in comments.

4 Comments

I tried both of them. Still file not saving :(
both? you have to use the format according to your OS.
If you are in an unix system: sqlite:////~/foo.db Then check your home directory
By both I mean - sqlite:////absolute/path/to/foo.db ;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.