Fixes error when trying to import openlp.org v1 bible or songs db from a file with non-ascii characters. Also some notes about error handling.

This commit is contained in:
Mattias Põldaru 2011-12-21 00:44:35 +02:00
parent e974df4c54
commit 35665ff336
2 changed files with 21 additions and 4 deletions

View File

@ -27,6 +27,7 @@
import logging
import sqlite
import sys
from openlp.core.lib import Receiver
from openlp.core.ui.wizard import WizardStrings
@ -53,9 +54,14 @@ class OpenLP1Bible(BibleDB):
connection = None
cursor = None
try:
connection = sqlite.connect(self.filename)
connection = sqlite.connect(
self.filename.encode(sys.getfilesystemencoding()))
cursor = connection.cursor()
except:
except sqlite.DatabaseError:
log.exception(u'File "%s" is encrypted or not a sqlite database, '
'therefore not an openlp.org 1.x database either' % self.filename)
# Please add an user error here!
# This file is not an openlp.org 1.x bible database.
return False
#Create the bible language
language_id = self.get_language(bible_name)
@ -63,7 +69,17 @@ class OpenLP1Bible(BibleDB):
log.exception(u'Importing books from "%s" failed' % self.filename)
return False
# Create all books.
cursor.execute(u'SELECT id, testament_id, name, abbreviation FROM book')
try:
cursor.execute(
u'SELECT id, testament_id, name, abbreviation FROM book')
except sqlite.DatabaseError as error:
log.exception(u'DatabaseError: %s' % error)
if error == 'no such table: book':
# Please add an user error here!
# This file is not an openlp.org 1.x bible database.
return False
else:
raise sqlite.DatabaseError(error)
books = cursor.fetchall()
self.wizard.progressBar.setMaximum(len(books) + 1)
for book in books:

View File

@ -165,7 +165,8 @@ class OpenLP1SongImport(SongImport):
Detect character encoding of an openlp.org 1.x song database.
"""
# Connect to the database.
connection = sqlite.connect(self.importSource, mode=0444)
connection = sqlite.connect(self.importSource.encode(
sys.getfilesystemencoding()), mode=0444)
cursor = connection.cursor()
detector = UniversalDetector()