peewee.OperationalError: no such table: - JerryMotov - Dec-10-2017
Python version: Python 3.6.3
I am using flask, peewee and sqlite
Hi, I am trying to connect my Python Flask application with a sqlite database.
The idea is that it creates a database file if it doesn't excists and then it will add data to that file when needed.
Main.py, the main script
from flask import Flask, render_template, request
import sqlite3
from peewee import *
from functions import *
from sql import *
app = Flask(__name__, template_folder='.')
@app.route('/')
def no_mail():
return render_template('index.html', email = "Enter email here")
@app.route('/', methods=['POST'])
def check_input():
email = request.form['text']
outcome = check_email(email)
set_data(email, outcome)
result_set = get_data()
print(result_set + "------------")
return render_template('index.html', result = outcome)
@app.route('/<email>')
def send_check_mail(email):
outcome = check_email(email)
set_data(email, outcome)
result_set = get_data()
print(result_set)
return render_template('index.html', result = outcome)
if __name__ == "__main__":
app.run(host="0.0.0.0", port="8000")
init_database() Sql.py, The file that is suspose to create the database and add data when needed
"""
This is the file that creates the database
"""
from peewee import *
import sqlite3
# Create the database
db = SqliteDatabase('check_history.db')
class Check_history(Model):
"""Create the columns"""
email = CharField(max_length = 100)
valid_or_invalid = CharField(max_length = 10)
class Meta:
"""
Tell the class Check_history that
The data belongs to the database check_history
"""
database = db
def set_data(email, valid_invalid):
#init_database()
data = Check_history.create(email = email,
valid_or_invalid = valid_invalid)
data.save()
def get_data():
#init_database()
data_list = []
for email in Check_history.select():
data_list.append([Check_history.email, Check_history.valid_or_invalid])
return data_list
def init_database():
db.connect()
db.create_tables([Check_history], safe = True)
db.close() Unfortunately it throws an error now that says that the database file doesn't exist. However the file does exist. (I
Error: Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/lib/python3/dist-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/usr/lib/python3/dist-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/lib/python3/dist-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib/python3/dist-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/lib/python3/dist-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/lib/python3/dist-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib/python3/dist-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/jeroen/github/python/validate-email/main.py", line 27, in send_check_mail
set_data(email, outcome)
File "/home/jeroen/github/python/validate-email/sql.py", line 26, in set_data
valid_or_invalid = valid_invalid)
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 4977, in create
inst.save(force_insert=True)
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 5170, in save
pk_from_cursor = self.insert(**field_dict).execute()
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 3584, in execute
cursor = self._execute()
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 2939, in _execute
return self.database.execute_sql(sql, params, self.require_commit)
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 3837, in execute_sql
self.commit()
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 3656, in __exit__
reraise(new_type, new_type(*exc_args), traceback)
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 135, in reraise
raise value.with_traceback(tb)
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 3830, in execute_sql
cursor.execute(sql, params or ())
peewee.OperationalError: no such table: check_history
I have tried to change the link at db = SqliteDatabase(link_to_db_file_here)
then it throws me another error (Deleting the file doesn't fix it either):
Error: Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/lib/python3/dist-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/usr/lib/python3/dist-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/lib/python3/dist-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib/python3/dist-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/lib/python3/dist-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/lib/python3/dist-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib/python3/dist-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/jeroen/github/python/validate-email/main.py", line 27, in send_check_mail
set_data(email, outcome)
File "/home/jeroen/github/python/validate-email/sql.py", line 26, in set_data
valid_or_invalid = valid_invalid)
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 4977, in create
inst.save(force_insert=True)
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 5170, in save
pk_from_cursor = self.insert(**field_dict).execute()
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 3584, in execute
cursor = self._execute()
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 2939, in _execute
return self.database.execute_sql(sql, params, self.require_commit)
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 3837, in execute_sql
self.commit()
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 3656, in __exit__
reraise(new_type, new_type(*exc_args), traceback)
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 135, in reraise
raise value.with_traceback(tb)
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 3828, in execute_sql
cursor = self.get_cursor()
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 3774, in get_cursor
return self.get_conn().cursor()
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 3763, in get_conn
self.connect()
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 3738, in connect
self._local.conn = self._create_connection()
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 3768, in _create_connection
return self._connect(self.database, **self.connect_kwargs)
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 3656, in __exit__
reraise(new_type, new_type(*exc_args), traceback)
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 135, in reraise
raise value.with_traceback(tb)
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 3768, in _create_connection
return self._connect(self.database, **self.connect_kwargs)
File "/home/jeroen/.local/lib/python3.6/site-packages/peewee.py", line 4023, in _connect
conn = sqlite3.connect(database, **kwargs)
peewee.OperationalError: unable to open database file
RE: peewee.OperationalError: no such table: - JerryMotov - Dec-10-2017
Whoops the __name__ == "__main__": Didn't run for some reason so my database never got initialized
Moved the init_database out of the if __name__ == "__main__": and now it's working!!
RE: peewee.OperationalError: no such table: - Windspar - Dec-10-2017
Try this. I believe you have it in the wrong order.
if __name__ == "__main__":
init_database()
app.run(host="0.0.0.0", port="8000")
RE: peewee.OperationalError: no such table: - JerryMotov - Dec-10-2017
(Dec-10-2017, 04:57 PM)Windspar Wrote: Try this. I believe you have it in the wrong order.
if __name__ == "__main__":
init_database()
app.run(host="0.0.0.0", port="8000")
That worked!
Thanks windspar!
|