Forums

Python Script to Modify mySQL Database

I inputted several rows of information into a mySQL database. I realized that in one column where I put in URLS, all of them had "https" rather than "http".

I thought it would be easier to write a Python script to edit them. I'm using SQLAlchemy to interact with the database. I am a newbie at all of this, and was wondering if someone would point me to a description of how to do this.

Here are some questions I had:

  1. Do I still do "from flask import Flask" to begin?
  2. Do I still have the app = Flask(name)?
  3. Im my Flask_py.py file, I have the "class XXXX (db.Models):" and then describe the table. Do I do that in the second file (the script to modify the changes?
  4. Where can I find examples of code to do mySQL changes?

Sorry to ask so many questions. I appreciate any help.

If you're using the Flask-SQLAlchemy plugin in your Flask app, and you want the code to look as similar as possible to the code you're used to (which is a good idea!) then:

  1. Yes, you'll need that
  2. ...and that too. There's no harm in creating a Flask object outside a web app, and it's needed for the Flask-SQLAlchemy plugin.
  3. You can do that, but it would be better to move all of the database stuff out to its own module so that you can import that both from the website code and from the migration code. If that sounds really hard to you, and if this migration script is a throwaway thing that you're going to delete afterwards, then there's no real problem in the duplication for now. But if you're planning to keep it around then splitting things out into separate modules is probably the best way forward.
  4. That depends on the changes. Normally you just need to get an object using SQLAlchemy's XXXX.query function on your model class, then make the changes, then commit the change. Here's some example code from a Flask app I wrote a while back that stores translations of Portuguese words into English -- the code specifically gets a word using its database ID, and saves a new English translation:
    word = Word.query.filter_by(id=word_id).first()
    word.word = new_data["word"]
    word.english = new_data["english"]
    db.session.commit()
    

In fact, if you do decide to go down the multi-module route, the whole Flask app that I wrote for the Portuguese translations might be of some interest. Here's the GitHub repo.

Thanks for all your help. I have a simpler app, so I don't have an init.py file. I just have the flask_app.py file.

When I place my models in a models.py file and import them into the flask_app.py file, I get an error that "db" is not defined , with db being the instance of my SQLAlchemy. I don't know how to import db into the models.py file (that is, I know how to import files in general but I don't know what to import)

Sorry for all the basic questions. Still struggling to learn these frameworks and extensions.

We have a few import-error-debugging tips here: http://help.pythonanywhere.com/pages/DebuggingImportError - do they help?