2014-11-11 4 views
10

Так что я знаю, что эта проблема не нова в колбе, и люди уже спрашивали ее раньше. Однако я все еще сталкиваюсь с проблемой при выполнении своих команд базы данных в bash, поскольку я новичок в python. Это то, что я сделалоперационная ошибка: база данных заблокирована

import sqlite3 
conn = sqlite.connect('/home/pjbardolia/mysite/tweet_count.db') 
c = conn.cursor() 

c.execute("create table count_twitter (count_id integer primary key autoincrement ,count_present integer not null,last_tweet not null)") 

c.execute(insert into count_twitter values('',10,10)) 

однако после выполнения вставки заявления я получаю оперативную ошибку: база данных заблокирована. Может ли кто-нибудь сказать простыми словами, что означает эта ошибка? и как его решить. Заранее спасибо

+0

Просто, чтобы проверить, но последняя строка в коде на самом деле: 'c.execute (" вставить в значения count_twitter („“, 10 , 10) ")'? [SQLite doc page] (http://www.sqlite.org/cvstrac/wiki?p=DatabaseIsLocked) для этой ошибки дает много рекомендаций. Одна вещь, которую нужно посмотреть в Flask, происходит из режима dev (используя «app.run()») в режиме производства (на сервере WSGI) вы переходите от одного к нескольким потокам, что может вызвать проблемы с SQLite и требует дополнительной обработки параллелизма. –

ответ

20

Это означает, что эта ошибка:

SQLite is meant to be a lightweight database, and thus can't support a high level of concurrency. OperationalError: database is locked errors indicate that your application is experiencing more concurrency than sqlite can handle in default configuration. This error means that one thread or process has an exclusive lock on the database connection and another thread timed out waiting for the lock the be released.

Python's SQLite wrapper has a default timeout value that determines how long the second thread is allowed to wait on the lock before it times out and raises the OperationalError: database is locked error.

If you're getting this error, you can solve it by:

Switching to another database backend. At a certain point SQLite becomes too "lite" for real-world applications, and these sorts of concurrency errors indicate you've reached that point.

Rewriting your code to reduce concurrency and ensure that database transactions are short-lived.

Increase the default timeout value by setting the timeout database option optionoption

Вероятно, у вас есть еще одно соединение в вашем коде, который не закрыт или не привержен и эту причину этой ошибки. В основном пытается сделать второй execute, когда он уже заблокирован другим. Если вы действительно хотите иметь свои параллельные транзакции, вам необходимо иметь RDBMS.

1

убедитесь, что вы фиксируете другие соединения, используя con.commit()

Смежные вопросы