Python database WITHOUT using Django (for Heroku)

前端 未结 4 465
误落风尘
误落风尘 2021-02-02 15:54

To my surprise, I haven\'t found this question asked elsewhere. Short version, I\'m writing an app that I plan to deploy to the cloud (probably using Heroku), which will do var

相关标签:
4条回答
  • 2021-02-02 16:30

    I'd use MongoDB. Heroku has support for it, so I think it will be really easy to start and scale out: https://addons.heroku.com/mongohq

    About Python: MongoDB is a really easy database. The schema is flexible and fits really well with Python dictionaries. That's something really good.

    You can use PyMongo

    from pymongo import Connection
    connection = Connection()
    
    # Get your DB
    db = connection.my_database
    
    # Get your collection
    cars = db.cars
    
    # Create some objects
    import datetime
    car = {"brand": "Ford",
           "model": "Mustang",
           "date": datetime.datetime.utcnow()}
    
    # Insert it
    cars.insert(car)
    

    Pretty simple, uh?

    Hope it helps.

    EDIT:

    As Endophage mentioned, another good option for interfacing with Mongo is mongoengine. If you have lots of data to store, you should take a look at that.

    0 讨论(0)
  • 2021-02-02 16:42

    Create a standalone Heroku Postgres database. http://postgres.heroku.com

    0 讨论(0)
  • 2021-02-02 16:43

    You can get a database provided from Heroku without requiring your app to use Django. To do so:

    heroku addons:add heroku-postgresql:dev
    

    If you need a larger more dedicated database, you can examine the plans at Heroku Postgres

    Within your requirements.txt you'll want to add:

    psycopg2
    

    Then you can connect/interact with it similar to the following:

    import psycopg2
    import os
    import urlparse
    
    urlparse.uses_netloc.append('postgres')
    url = urlparse.urlparse(os.environ['DATABASE_URL'])
    
    conn = psycopg2.connect("dbname=%s user=%s password=%s host=%s " % (url.path[1:], url.username, url.password, url.hostname))
    cur = conn.cursor()
    
    query = "SELECT ...."
    cur.execute(query)
    
    0 讨论(0)
  • 2021-02-02 16:43

    I did this recently with Flask. (https://github.com/HexIce/flask-heroku-sqlalchemy).

    There are a couple of gotchas:

    1. If you don't use Django you may have to set up your database yourself by doing:

    heroku addons:add shared-database
    

    (Or whichever database you want to use, the others cost money.)

    2. The database URL is stored in Heroku in the "DATABASE_URL" environment variable. In python you can get it by doing.

    dburl = os.environ['DATABASE_URL']
    

    What you do to connect to the database from there is up to you, one option is SQLAlchemy.

    0 讨论(0)
提交回复
热议问题