Commit bd30faaf authored by Mattia Bondanza's avatar Mattia Bondanza

First basic implementation of a database with sqlite.

parent 51b6ab15
# -*- coding: utf-8 -*-
import sqlite3
import hashlib
import settings
def md5sum( path ):
f = open( path, "rb" )
out = hashlib.md5( f.read() )
f.close()
return out.hexdigest()
def db_init( db_file, analysis_directory ):
db_con = sqlite3.connect( db_file )
db_cur = db_con.cursor()
db_cur.execute( "CREATE TABLE library ( md5, path )" )
db_con.close()
def add_book( db_file, path ):
db_con = sqlite3.connect( db_file )
db = db_con.cursor()
md5 = md5sum( path )
db.execute( "SELECT * FROM library WHERE md5=:md5", { 'md5': md5 } )
query_results = db.fetchall()
if len( query_results ) > 0:
if settings.verbosity_level_numeric > 0:
print( "[database]\tOne file with the same md5 of {} are already present in the database, this probably means"
" that you have a duplicate file in your library.".format( path ))
db_con.close()
return False
else:
db.execute( "INSERT INTO library VALUES ( :md5, :path )", { 'md5':md5, 'path': path } )
db_con.commit()
db.execute( "CREATE TABLE {} ( author, title, year, edition, ISBN, found_ISBN )".format( '_'+md5 ) )
db.execute( "CREATE TABLE {}_isbn ( ISBN_string, ISBN, author, title, source )".format( '_'+md5 ) )
if settings.verbosity_level_numeric > 0:
print( "[database]\tFile {} inserted in the database.".format( path ))
db_con.close()
return True
def add_isbn( db_file, path, ISBN_string, ISBN, author, title, source ):
db_con = sqlite3.connect( db_file )
db = db_con.cursor()
md5 = md5sum( path )
db.execute( "INSERT INTO {}_isbn VALUES ( :isbnstring, :isbn, :auth, :title, :source )".format( '_'+md5 ),
{ 'isbnstring': ISBN_string, 'isbn': ISBN, 'auth': author, 'title': title, 'source': source } )
db_con.commit()
db_con.close()
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment