How to cre and Sqlite3 database and tables

53 views Asked by At

I am trying to create a web app using python and flask.

I got my routing parts done, and now I am trying to set up a Sqlite3 database to store users information.

The following is my code.

from flask import Flask, render_template, redirect, request

import sqlite3
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin

from flask_login import LoginManager
login_manager = LoginManager()

app = Flask(__name__)
#To connect the database with the app
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///tekken.db'

db = SQLAlchemy(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), nullable=False)
    email = db.Column(db.String(20), nullable=False)
    password = db.Column(db.String(80), nullable=False)

I saved the code and opened flask shell in my terminal, and then I ran the following command.

db.create_all()

After running the command, I opened another terminal window and ran the following command.

sqlite 3 tekken.db .tables;

However, it says "Error: in prepare, near "tables": syntax error (1)", and it seems like the users table was never created.

What am I missing in my code?

--Updated on 02/07

I ran flask run command, and then opened flask shell and ran db.created_all(), but I did not see any error on my terminal and flask shell. So I can't even tell what might hove gone wrong. enter image description here

enter image description here

0

There are 0 answers