Python Forum
SQL Alchemy, database does not work
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SQL Alchemy, database does not work
#1
Hallowings,

I am trying to make my very first database app according to this tutorial - https://www.youtube.com/watch?v=0FsvqwRh...HV&index=7

The Python code is

from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy


app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://sql11200793:[email protected]/sql11200793'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)

class Comments(db.Model):
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(20))
	comment = db.Column(db.String(1000))

@app.route('/')
def index():
	result = Comments.query.all()

	return render_template('index.html', result=result)

@app.route('/sign')
def sign():
	return render_template('sign.html')

@app.route('/process', methods=['POST'])
def process():
	name = request.form['name']
	comment = request.form['comment']

	signature = Comments(name=name, comment=comment)
	db.session.add(signature)
	db.session.commit()

	return redirect(url_for('index'))

@app.route('/home', methods=['GET', 'POST'])
def home():
	links = ['https://www.youtube.com', 'https://www.bing.com', 'https://www.python.org', 'https://www.enkato.com']
	return render_template('example.html', links=links)

if __name__ == '__main__':
	app.run(debug=True)
(I don't think HTML's templates would hold anything interesting....)

and it does not work: When I hit run it PyCharm says it's ready on 127.0.... but when I go there I get error :

File "C:\Language\Python3.6\lib\site-packages\flask\app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Language\Python3.6\lib\site-packages\flask\app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "C:\Language\Python3.6\lib\site-packages\flask\app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Language\Python3.6\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Language\Python3.6\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "C:\Language\Python3.6\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Language\Python3.6\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Language\Python3.6\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Language\Python3.6\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Language\Python3.6\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\temp\example1\venv\app\IntroToFlask.py", line 18, in index
result = Comments.query.all()
File "C:\Language\Python3.6\lib\site-packages\flask_sqlalchemy\__init__.py", line 514, in __get__
return type.query_class(mapper, session=self.sa.session())
File "C:\Language\Python3.6\lib\site-packages\sqlalchemy\orm\scoping.py", line 78, in __call__
return self.registry()
File "C:\Language\Python3.6\lib\site-packages\sqlalchemy\util\_collections.py", line 990, in __call__
return self.registry.setdefault(key, self.createfunc())
File "C:\Language\Python3.6\lib\site-packages\sqlalchemy\orm\session.py", line 2867, in __call__
return self.class_(**local_kw)
File "C:\Language\Python3.6\lib\site-packages\flask_sqlalchemy\__init__.py", line 143, in __init__
bind = options.pop('bind', None) or db.engine
File "C:\Language\Python3.6\lib\site-packages\flask_sqlalchemy\__init__.py", line 877, in engine
return self.get_engine()
File "C:\Language\Python3.6\lib\site-packages\flask_sqlalchemy\__init__.py", line 896, in get_engine
Open an interactive python shell in this framereturn connector.get_engine()
File "C:\Language\Python3.6\lib\site-packages\flask_sqlalchemy\__init__.py", line 559, in get_engine
self._engine = rv = sqlalchemy.create_engine(info, **options)
File "C:\Language\Python3.6\lib\site-packages\sqlalchemy\engine\__init__.py", line 391, in create_engine
return strategy.create(*args, **kwargs)
File "C:\Language\Python3.6\lib\site-packages\sqlalchemy\engine\strategies.py", line 80, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "C:\Language\Python3.6\lib\site-packages\sqlalchemy\dialects\mysql\mysqldb.py", line 110, in dbapi
return __import__('MySQLdb')

next, it tells me to open cmd and type "from guestbook import db". When I hit enter, cmd says "no module called guestbook".



when I go to http://www.phpmyadmin.co I see no database I would create....




....so... I don't know, can somebody help plz? :( How do I create this database, or what module am I missing or... what us wrong, plz? :(

Thanx.

Peter the Noob Noob

wait!

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://sql11200793:[email protected]/sql11200793'
I have no MySQL database (there is some error in system and MySQL won't install)

Could be this the problem?
Reply
#2
(Oct-23-2017, 03:18 PM)Peter_EU Wrote: I have no MySQL database (there is some error in system and MySQL won't install) Could be this the problem?
You most have MySQL installed,and the MySQL server/service running.

Can use SQLite that comes with Python,it's simpler and it make one database file.
Has this URI:
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///my_database.db'
Example i use your file,the only change is line over:
C:\all_flask\2017
λ python
Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
# Create database
>>> from app1 import db
>>> db.create_all()

# Insert into database
>>> from app1 import Comments
>>> name = Comments(name='Clark Kent', comment='Superman')
>>> space = Comments(name='Space invander', comment='Old game')
>>> db.session.add(name)
>>> db.session.add(space)
>>> db.session.commit()
>>> exit()
C:\all_flask\2017
λ sqlite3
SQLite version 3.20.1 2017-08-24 16:21:36
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .open my_database.db
sqlite> .tables
comments

sqlite> .mode column
sqlite> .headers on
sqlite> SELECT * FROM comments;
id          name        comment
----------  ----------  ----------
1           Clark Kent  Superman
2           Space inva  Old game
Reply
#3
(Oct-23-2017, 09:09 PM)snippsat Wrote:
(Oct-23-2017, 03:18 PM)Peter_EU Wrote: I have no MySQL database (there is some error in system and MySQL won't install) Could be this the problem?
You most have MySQL installed,and the MySQL server/service running.

Can use SQLite that comes with Python,it's simpler and it make one database file.
Has this URI:
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///my_database.db'
Example i use your file,the only change is line over:
C:\all_flask\2017
λ python
Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
# Create database
>>> from app1 import db
>>> db.create_all()

# Insert into database
>>> from app1 import Comments
>>> name = Comments(name='Clark Kent', comment='Superman')
>>> space = Comments(name='Space invander', comment='Old game')
>>> db.session.add(name)
>>> db.session.add(space)
>>> db.session.commit()
>>> exit()
C:\all_flask\2017
λ sqlite3
SQLite version 3.20.1 2017-08-24 16:21:36
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .open my_database.db
sqlite> .tables
comments

sqlite> .mode column
sqlite> .headers on
sqlite> SELECT * FROM comments;
id          name        comment
----------  ----------  ----------
1           Clark Kent  Superman
2           Space inva  Old game


Thank you very much but as I said here: https://python-forum.io/Thread-Stuck-wit...e-plz-HELP

I am stuck with db and db.create_all() command Cry
Reply
#4
Just reading the thread, the part you claim to be stuck on, "db and db.create_all()" would be solved if you had a db application (like SQLite for example) installed AND running. If that is not the case, I could imagine you would receive errors.

I agree with what snippsat said, give it a try.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020