Search code examples
pythonmysqlherokucleardb

putting python variables into cleardb


In Short

I have a heroku python setup and setup a cleardb database, but don't know how to put my python variables into that database. What lines of code, in python, connect to my db and add some variables into the database. OR Where is some example code of python-heroku-cleardb insertion code.

In Long

I have a python heroku setup and have setup cleardb as describe here: https://devcenter.heroku.com/articles/cleardb

But I am having trouble finding code concerning how to for instance place a python variable into my database. Also I am having difficulty finding any resources on how to build the mysql dtabase within the heroku/cleardb framework.

They supply code to put into my settings.py file but I dont have one. I have a main application file with this:

...
class ToDatabase(tornado.web.RequestHandler):
    def post(self):
        data_json = self.request.arguments
        print(data_json)
...

class Application(tornado.web.Application):
    def __init__(self, port=PORT):
        tornado.web.Application.__init__(self, debug=True)
        self.logger   = logging.getLogger()
        self.ioloop   = tornado.ioloop.IOLoop.instance()

        self.port = port
        self.address = 'www.examplesite.com'

        self.add_handlers('', [
            (r'/', IndexHandler),
            (r"/todatabase/",ToDatabase),

and data_json has some values that I want in my database.

NOTE: I have experience using MySQL-python and MySQL and building databases, but am fairly lost on how to do so within heroku.


Solution

  • This question is really the wrong question.

    heroku config outputs the host, database name, user and password for the remote cleardb database in the DATABASE_URL output.

    Use this information in a db = MySQLdb.connect(host="...", user = "...", passwd="...", db = "...") line to connect to it. Then just use basic MySQLdb code to insert into it.