简体   繁体   中英

sqlite3 database is locked(using bottle)

I'm trying to use Bottle framework in python with sqlite3. Then I made a Todo List application but when I tried to post a data at the first time the error happened differently from above. The second time 'database is locked' happened.

Can anyone help?

#_*_ coding:utf-8- _*_

import os, sqlite3
from bottle import route, run, get, post, request, template

#sqlite from here----------------
dbname = "todo.db"
connection = sqlite3.connect(dbname)
dbcontrol = connection.cursor()

#Making table from here--------------------

create_table = '''create table todo_list (todo text)'''

@route("/")
def index():
    todo_list = get_todo()
    return template("index", todo_list=todo_list)

I think I need more specific code here.

@route("/enter", method=["POST"])
 def enter():
 conn = sqlite3.connect("todo.db")
 todo=request.POST.getunicode("todo_list")
 save_todo(todo)
 return redirect("/")




def save_todo(todo):
 connection = sqlite3.connect('todo.db')
 dbcontrol = connection.cursor()
 insert="insert into todo_list(todo) values('{0}')".format(todo)
 dbcontrol.execute(insert)
 connection.commit()

def get_todo():
 connection=sqlite3.connect('todo.db')
 dbcontrol = connection.cursor()
 select = "select * from todo_list"
 dbcontrol.execute(select)
 row = dbcontrol.fetchall()
 return row

run(host="localhost", port=8080, debug=True)

Can you check this link

Install the package with one of the following commands

$ pip install bottle-sqlite
$ easy_install bottle-sqlite

An example from the plugin

import bottle

app = bottle.Bottle()
plugin = bottle.ext.sqlite.Plugin(dbfile='/tmp/test.db')
app.install(plugin)

@app.route('/show/:item')
def show(item, db):
    row = db.execute('SELECT * from items where name=?', item).fetchone()
    if row:
        return template('showitem', page=row)
    return HTTPError(404, "Page not found")

Important notes from the plugin

Routes that do not expect a db keyword argument are not affected.

The connection handle is configured so that sqlite3.Row objects can be accessed both by index (like tuples) and case-insensitively by name. At the end of the request cycle, outstanding transactions are committed and the connection is closed automatically. If an error occurs, any changes to the database since the last commit are rolled back to keep the database in a consistent state.

Do not forget the checkout the CONFIGURATION page

Hope this helps!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM