我在应用程序中遇到数据库锁定错误。 (Django,Sqlite3,Window 10)
problem.parent_contest.add(Contest.objects.select_for_update().get(pk=contest))
此语句是否使数据库锁定错误?我不明白为什么该代码会导致错误。请帮助我。
有我的代码和错误回溯。谢谢。
for problem_id in range(1000, 19999, 1):
current_problem = parse_problem(problem_id)
db_lock.acquire()
problem, created = Problem.objects.get_or_create(
id=current_problem['id'],
defaults={
'title': current_problem['title'],
'can_submit': current_problem['can_submit']
}
)
problem.title = current_problem['title']
problem.can_submit = current_problem['can_submit']
problem.save()
for contest in current_problem['parent']:
problem.parent_contest.add(Contest.objects.select_for_update().get(pk=contest))
db_lock.release()
time.sleep(600)
class Contest(models.Model):
id = models.IntegerField(primary_key=True)
title = models.TextField()
parent_category = models.ForeignKey(Category, on_delete=models.CASCADE)
class Meta:
ordering = ('id',)
def __str__(self):
return 'Contest %s: %s' % (self.id, self.title)
class Problem(models.Model):
id = models.IntegerField(primary_key=True)
title = models.TextField()
can_submit = models.BooleanField()
parent_contest = models.ManyToManyField(Contest)
class Meta:
ordering = ('id',)
def __str__(self):
return 'Problem %s: %s' % (self.id, self.title)
Traceback (most recent call last):
File "C:\Users\JongBeom Kim\.virtualenvs\backend-3j_oU1lC\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\JongBeom Kim\.virtualenvs\backend-3j_oU1lC\lib\site-packages\django\db\backends\sqlite3\base.py", line 296, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: database is locked
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "c:\python36\Lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "c:\python36\Lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "C:\msys64\home\JongBeomKim\WEB\ps.kajebiii.com\backend\boj\apps.py", line 150, in parse_all_problem
problem.parent_contest.add(Contest.objects.select_for_update().get(pk=contest))
File "C:\Users\JongBeom Kim\.virtualenvs\backend-3j_oU1lC\lib\site-packages\django\db\models\fields\related_descriptors.py", line 926, in add
self._add_items(self.source_field_name, self.target_field_name, *objs)
File "C:\Users\JongBeom Kim\.virtualenvs\backend-3j_oU1lC\lib\site-packages\django\db\models\fields\related_descriptors.py", line 1093, in _add_items
for obj_id in new_ids
File "C:\Users\JongBeom Kim\.virtualenvs\backend-3j_oU1lC\lib\site-packages\django\db\models\query.py", line 465, in bulk_create
ids = self._batched_insert(objs_without_pk, fields, batch_size)
File "C:\Users\JongBeom Kim\.virtualenvs\backend-3j_oU1lC\lib\site-packages\django\db\models\query.py", line 1152, in _batched_insert
self._insert(item, fields=fields, using=self.db)
File "C:\Users\JongBeom Kim\.virtualenvs\backend-3j_oU1lC\lib\site-packages\django\db\models\query.py", line 1133, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Users\JongBeom Kim\.virtualenvs\backend-3j_oU1lC\lib\site-packages\django\db\models\sql\compiler.py", line 1285, in execute_sql
cursor.execute(sql, params)
File "C:\Users\JongBeom Kim\.virtualenvs\backend-3j_oU1lC\lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "C:\Users\JongBeom Kim\.virtualenvs\backend-3j_oU1lC\lib\site-packages\django\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\JongBeom Kim\.virtualenvs\backend-3j_oU1lC\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\JongBeom Kim\.virtualenvs\backend-3j_oU1lC\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\JongBeom Kim\.virtualenvs\backend-3j_oU1lC\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\JongBeom Kim\.virtualenvs\backend-3j_oU1lC\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\JongBeom Kim\.virtualenvs\backend-3j_oU1lC\lib\site-packages\django\db\backends\sqlite3\base.py", line 296, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: database is locked