removed some code duplications, renamed method prepare to loadQuery
Added transactions for database

Signed-off-by: Alexander Book <redchrome@gmx.de>