Fix various pyodbc related issues. Fixes bug 1590657.

Fixes: https://launchpad.net/bugs/1590657
This commit is contained in:
Tomas Groth 2016-06-14 21:11:57 +02:00
parent e89803a3ad
commit 41c0d3fcf9
3 changed files with 21 additions and 15 deletions

View File

@ -24,15 +24,17 @@ The :mod:`mediashout` module provides the functionality for importing
a MediaShout database into the OpenLP database. a MediaShout database into the OpenLP database.
""" """
# WARNING: See https://docs.python.org/2/library/sqlite3.html for value substitution # WARNING: See https://docs.python.org/3/library/sqlite3.html for value substitution
# in SQL statements # in SQL statements
import pyodbc import pyodbc
import logging
from openlp.core.lib import translate from openlp.core.lib import translate
from openlp.plugins.songs.lib.importers.songimport import SongImport from openlp.plugins.songs.lib.importers.songimport import SongImport
VERSE_TAGS = ['V', 'C', 'B', 'O', 'P', 'I', 'E'] VERSE_TAGS = ['V', 'C', 'B', 'O', 'P', 'I', 'E']
log = logging.getLogger(__name__)
class MediaShoutImport(SongImport): class MediaShoutImport(SongImport):
@ -44,17 +46,19 @@ class MediaShoutImport(SongImport):
""" """
Initialise the MediaShout importer. Initialise the MediaShout importer.
""" """
SongImport.__init__(self, manager, **kwargs) super(MediaShoutImport, self).__init__(manager, **kwargs)
#SongImport.__init__(self, manager, **kwargs)
def do_import(self): def do_import(self):
""" """
Receive a single file to import. Receive a single file to import.
""" """
try: try:
conn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)};DBQ={source};' conn = pyodbc.connect('DRIVER={{Microsoft Access Driver (*.mdb)}};DBQ={source};'
'PWD=6NOZ4eHK7k'.format(sorce=self.import_source)) 'PWD=6NOZ4eHK7k'.format(source=self.import_source))
except: except Exception as e:
# Unfortunately no specific exception type # Unfortunately no specific exception type
log.exception(e)
self.log_error(self.import_source, translate('SongsPlugin.MediaShoutImport', self.log_error(self.import_source, translate('SongsPlugin.MediaShoutImport',
'Unable to open the MediaShout database.')) 'Unable to open the MediaShout database.'))
return return
@ -63,17 +67,19 @@ class MediaShoutImport(SongImport):
songs = cursor.fetchall() songs = cursor.fetchall()
self.import_wizard.progress_bar.setMaximum(len(songs)) self.import_wizard.progress_bar.setMaximum(len(songs))
for song in songs: for song in songs:
topics = []
if self.stop_import_flag: if self.stop_import_flag:
break break
cursor.execute('SELECT Type, Number, Text FROM Verses WHERE Record = ? ORDER BY Type, Number', song.Record) cursor.execute('SELECT Type, Number, Text FROM Verses WHERE Record = ? ORDER BY Type, Number', float(song.Record))
verses = cursor.fetchall() verses = cursor.fetchall()
cursor.execute('SELECT Type, Number, POrder FROM PlayOrder WHERE Record = ? ORDER BY POrder', song.Record) cursor.execute('SELECT Type, Number, POrder FROM PlayOrder WHERE Record = ? ORDER BY POrder', float(song.Record))
verse_order = cursor.fetchall() verse_order = cursor.fetchall()
cursor.execute('SELECT Name FROM Themes INNER JOIN SongThemes ON SongThemes.ThemeId = Themes.ThemeId ' if cursor.tables(table='TableName', tableType='TABLE').fetchone():
'WHERE SongThemes.Record = ?', song.Record) cursor.execute('SELECT Name FROM Themes INNER JOIN SongThemes ON SongThemes.ThemeId = Themes.ThemeId '
topics = cursor.fetchall() 'WHERE SongThemes.Record = ?', float(song.Record))
topics = cursor.fetchall()
cursor.execute('SELECT Name FROM Groups INNER JOIN SongGroups ON SongGroups.GroupId = Groups.GroupId ' cursor.execute('SELECT Name FROM Groups INNER JOIN SongGroups ON SongGroups.GroupId = Groups.GroupId '
'WHERE SongGroups.Record = ?', song.Record) 'WHERE SongGroups.Record = ?', float(song.Record))
topics += cursor.fetchall() topics += cursor.fetchall()
self.process_song(song, verses, verse_order, topics) self.process_song(song, verses, verse_order, topics)

View File

@ -55,7 +55,7 @@ class OPSProImport(SongImport):
""" """
password = self.extract_mdb_password() password = self.extract_mdb_password()
try: try:
conn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)};DBQ={source};' conn = pyodbc.connect('DRIVER={{Microsoft Access Driver (*.mdb)}};DBQ={source};'
'PWD={password}'.format(source=self.import_source, password=password)) 'PWD={password}'.format(source=self.import_source, password=password))
except (pyodbc.DatabaseError, pyodbc.IntegrityError, pyodbc.InternalError, pyodbc.OperationalError) as e: except (pyodbc.DatabaseError, pyodbc.IntegrityError, pyodbc.InternalError, pyodbc.OperationalError) as e:
log.warning('Unable to connect the OPS Pro database {source}. {error}'.format(source=self.import_source, log.warning('Unable to connect the OPS Pro database {source}. {error}'.format(source=self.import_source,
@ -74,11 +74,11 @@ class OPSProImport(SongImport):
break break
# Type means: 0=Original, 1=Projection, 2=Own # Type means: 0=Original, 1=Projection, 2=Own
cursor.execute('SELECT Lyrics, Type, IsDualLanguage FROM Lyrics WHERE SongID = ? AND Type < 2 ' cursor.execute('SELECT Lyrics, Type, IsDualLanguage FROM Lyrics WHERE SongID = ? AND Type < 2 '
'ORDER BY Type DESC', song.ID) 'ORDER BY Type DESC', float(song.ID))
lyrics = cursor.fetchone() lyrics = cursor.fetchone()
cursor.execute('SELECT CategoryName FROM Category INNER JOIN SongCategory ' cursor.execute('SELECT CategoryName FROM Category INNER JOIN SongCategory '
'ON Category.ID = SongCategory.CategoryID WHERE SongCategory.SongID = ? ' 'ON Category.ID = SongCategory.CategoryID WHERE SongCategory.SongID = ? '
'ORDER BY CategoryName', song.ID) 'ORDER BY CategoryName', float(song.ID))
topics = cursor.fetchall() topics = cursor.fetchall()
try: try:
self.process_song(song, lyrics, topics) self.process_song(song, lyrics, topics)

View File

@ -49,7 +49,7 @@ class WorshipCenterProImport(SongImport):
Receive a single file to import. Receive a single file to import.
""" """
try: try:
conn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)};' conn = pyodbc.connect('DRIVER={{Microsoft Access Driver (*.mdb)}};'
'DBQ={source}'.format(source=self.import_source)) 'DBQ={source}'.format(source=self.import_source))
except (pyodbc.DatabaseError, pyodbc.IntegrityError, pyodbc.InternalError, pyodbc.OperationalError) as e: except (pyodbc.DatabaseError, pyodbc.IntegrityError, pyodbc.InternalError, pyodbc.OperationalError) as e:
log.warning('Unable to connect the WorshipCenter Pro ' log.warning('Unable to connect the WorshipCenter Pro '