I
I
IM_NIK2020-08-02 00:23:07
SQLite
IM_NIK, 2020-08-02 00:23:07

SQLite python database not being created?

I created my own project on flask (it seems that I did everything correctly (the project did not give any errors))
It was necessary to create a sqlite database
I go to the project folder,
I write "python", I
import the db variable (

app = Flask(__name__)
app.config.from_object(Configuration)
app.config['SQLALCHEMY_DATEBASE_URI'] = 'sqlite///blog.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db=SQLAlchemy(app)

) and a class with the required data (
class Article(db.Model):
# SETTINGS
id = db.Column(db.Integer,primary_key=True)
  title = db.Column(db.String(140) ,nullable=False)
  text = db.Column(db.Text , nullable=False)
  date = db.Column(db.DateTime, default=datetime.now())
  #funcshon
  def  __repr__(self):
    return '<Article %r>' % self.id

)
from app import db
from models import Article
db.create_all()

.
BUT the database was not created, I don't know why?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anatoliy Sukhov, 2020-08-02
@ayaout

You need to import the sqlite3 module and create a database connection.
#import sqlite3
#from flask import g
#DATABASE = '/path/to/database.db'

R
Roman Mirilaczvili, 2020-08-02
@2ord

The correct Sqlite connection prefix is ​​(colon missing):

sqlite://

# Unix/Mac - 4 initial slashes in total
engine = create_engine('sqlite:////absolute/path/to/foo.db')


https://docs.sqlalchemy.org/en/13/core/engines.htm...
If there are 3 slashes, then the current application path is set.

I
IM_NIK, 2020-08-02
@IM_NIK

did everything according to this lesson
https://www.youtube.com/watch?v=G-si1WbtNeM
The base was not created after a complete alteration of the block and checking for blots.
app.py

from flask import Flask , request,redirect , url_for
from configurasion import  Configuration
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
#!!! file
app = Flask(__name__)
app.config.from_object(Configuration)
db=SQLAlchemy(app)

main.py
from app import db
from app import app
import view
if __name__ == "__main__":
    app.run()

view.py
from app import app
from flask import render_template
from models import Article

@app.route("/",methods=['POST','GET'])
def index():
  return render_template('test.html')
  if request.method == 'POST':
    pass
    title = request.form['title']
    text = request.form['text']
    article = Post(title=title,text=text)
    try:
      db.session.add(article)
      db.session.commit()
      return redirect('/')
    except:
      return render_template('404.html'),404
  else:
    return render_template('test.html')
@app.route("/snq")
def snq():
  return "SNQ OLEG MOLCHANOV"
@app.route("/ADMIN")
def admin():
  return render_template('ADMIN.html')
@app.errorhandler(404)
def page_not_faund(e):
  return render_template('404.html'),404
@app.route('/comment',methods=['POST','GET'])
def comment():
  articles = Article.query.order_by(Article.date).all()
  return render_template('Comment.html',articles=articles)

Configuration,py
class Configuration():
  DEBUG=True;
  SQLALCHEMY_TRACK_MODIFICATIONS = False
  SQLALCHEMY_DATEBASE_URI = 'sqlite:///blog.db'

models.py
from app import db
from datetime import datetime
import re

class  Article(db.Model):
  # SETTINGS
  __tablename__ = 'posts'
  id = db.Column(db.Integer,primary_key=True)
  title = db.Column(db.String(140) ,nullable=False)
  text = db.Column(db.Text , nullable=False)
  date = db.Column(db.DateTime, default=datetime.utcnow)
  #funcshon
  def  __repr__(self):
    return '<Article %r>' % self.id

db.create_all()
HELP ME PLEASE!!!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question