Inserting a value with SQLite3 and Python/Flask
<p>I am working in a small website where I want to show, insert, edit and delete elements from the database</p>

<p>I accomplished showing the data with this route:</p>

<pre><code>@app.route('/objects')
def objects():
    g.db = sqlite3.connect(db_location)
    cur = g.db.execute('SELECT * FROM milkyway ORDER BY type')
    objects = [dict(id=row[0], type=row[1], name=row[2], description=row[3],  size=row[4], mass=row[5],distance=row[6], discoverer=row[7], image_url=row[8])   for row in cur.fetchall()]
    g.db.close()
    return render_template("objects.html",objects=objects)
</code></pre>

<p>And now I am trying to insert an element but I receive an error "AttributeError: '_AppCtxGlobals' object has no attribute 'db'"</p>

<pre><code>@app.route('/insert_value',methods = ['POST', 'GET'])
def insert_value():
    atype = request.form['type']
    name = request.form['name']
    description = request.form['description']
    size = request.form['size']
    mass = request.form['mass']
    distance = request.form['distance']
    discoverer = request.form['discoverer']
    image_url = request.form['image_url']

    g.db.execute('INSERT INTO milkyway  (type,name,description,size,mass,distance,discoverer,image_ur) VALUES (?,?,?,?,?,?,?,?)', [atype], [name], [description], [size], [mass], [distance], [discoverer], [image_url] )

    g.db.commit()
    return redirect(url_for('objects'))
</code></pre>

<p>I search everywhere but the thing is, there are so many different ways to do this and I cannot make it works. I followed for instance this example; <a href="http://opentechschool.github.io/python-flask/extras/databases.html" rel="nofollow">http://opentechschool.github.io/python-flask/extras/databases.html</a></p>