2

I want to connect to MySQL database using Flask. Below is my code:

app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'admin'
app.config['MYSQL_PASSWORD'] = '********'
app.config['MYSQL_DB'] = 'abc'

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://admin:********@localhost/abc'

api = Api(app)
db = SQLAlchemy(app)
ma = Marshmallow(app)

db.create_all()

class Video(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    videoid = db.Column(db.String(80), unique=True)
    video_timestamp = db.Column(db.Integer, unique=True)

    def __init__(self, videoid, video_timestamp):
        self.videoid = videoid
        self.video_timestamp = video_timestamp

and I met this error:

sqlalchemy.exc.ProgrammingError: (pymysql.err.ProgrammingError) (1146, "Table 'abc.video' doesn't exist") [SQL: 'SELECT video.id AS video_id, video.videoid AS video_videoid, video.video_timestamp AS video_video_timestamp \nFROM video']

I know how to connect to SQLite database, but it seems difficult to connect to MySQL database?

Anyone can help me, please?

3
  • 3
    not 100% sure, could it be that you call db.create_all() before the Video model is declared? Commented Jan 4, 2018 at 8:09
  • I try to remove this line and in terminal I try to open python3 and use the following code: from my_app import db but it doesn't work Commented Jan 4, 2018 at 8:14
  • How exactly isn't it working? Commented Jan 5, 2018 at 18:14

2 Answers 2

0

You don't have a problem to connect to your database but it looks like your table are not created yet. so remove the db.create_all() in your code and check this from the sql_alchemy official documentation :

To create the initial database, just import the db object from an interactive Python shell and run the SQLAlchemy.create_all() method to create the tables and database:

from yourapplication import db
db.create_all()
Sign up to request clarification or add additional context in comments.

4 Comments

I tried but the same error occurs. I know this way to create tables manually.
After removing the db.create_all() in your code you run the and run it in python console it refuse?
@detuvoldo I've added those infos but I'm unable to reproduce your error
No, I have not. I am trying to connect to MariaDB
0

The code to connect to Mysql:

from flaskext.mysql import MySQL

cursorclass, and autocommit to make life easier

mysql = MySQL(autocommit=True, cursorclass= DictCursor)
app.config['SECRET_KEY'] = 'Thisissupposedtobesecret!'
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'root'
app.config['MYSQL_DATABASE_DB'] = 'db'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
app.config['MYSQL_DATABASE_PORT'] = 8889
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
mysql.init_app(app)

connect cursor 
cursor = conn.cursor()

You can download xxamp or Mamp and host database online at phpmyadmin (localhost)

3 Comments

I'm trying to use ORM :D Thanks for your help
Autocommit very much makes life a pain.
Even for small projects?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.