add_book.ipynb

This notebook guides in the action of adding books to your database.

The notebook is divided in two parts:

  1. In the first part we create a minimal database with a single book. We save it as database.db in the current folder.

  2. In the second part we load the database database.db and we add the book contained in the file newbook.xlsx (created using mbp.create_excel_template).

import polars as pl

import bookhoarder as bh
%opts magic unavailable (pyparsing cannot be imported)
%compositor magic unavailable (pyparsing cannot be imported)

Create minimal database

book = bh.new_book(
    {
        "isbn": "9783866473256",
        "author": "Karl Marx",
        "title": "Das Kapital",
        "year": "2009",
        "publisher": "Anaconda",
        "full_price": 7.95,
        "min_price": 7.95,
        "buecher": "https://www.buecher.de/artikel/buch/das-kapital/25646129/",
        "osiander": "https://www.osiander.de/shop/home/artikeldetails/A1006759980",
    }
)
df = pl.DataFrame(book, schema_overrides=bh.schema())
df
shape: (1, 21)
isbnauthortitleyearpublisherlanguageformatfull_pricemin_priceadelphibuecherfeltrinellihoeplihugendubelibslibcooplibracciolibunimondadoriosianderrizzoli
strstrstrstrstrstrstrf64f64strstrstrstrstrstrstrstrstrstrstrstr
"9783866473256""Karl Marx""Das Kapital""2009""Anaconda"nullnull7.957.95null"https://www.buecher.de/artikel…nullnullnullnullnullnullnullnull"https://www.osiander.de/shop/h…null
bh.write_database(
    df,  # dataframe
    "books",  # table name
    url="sqlite:///database.db",  # database url
    if_table_exists="replace",  # behavior if table already exists
)

Add new book to database

We load the new book from the file newbook.xlsx:

df_2 = bh.read_excel("newbook.xlsx", schema=bh.schema())
df_2
shape: (1, 21)
isbnauthortitleyearpublisherlanguageformatfull_pricemin_priceadelphibuecherfeltrinellihoeplihugendubelibslibcooplibracciolibunimondadoriosianderrizzoli
strstrstrstrstrstrstrf64f64strstrstrstrstrstrstrstrstrstrstrstr
"9781857988826""Ursula K. Le Guin""The Dispossessed""1999""Orion Publishing Co"nullnull13.013.0null"https://www.buecher.de/artikel…"https://www.lafeltrinelli.it/d…nullnull"https://www.ibs.it/dispossesse…nullnullnullnull"https://www.osiander.de/shop/h…null

Then, we simply write it to the database specifying the attribute 'append':

bh.write_database(
    df_2,
    table_name="books",
    url="sqlite:///database.db",
    if_table_exists="append",
)

Note: if we wanted to make sure to not add duplicates (with respect to the ISBN), we can specify a modify_df_before_writing function as an argument to write_database. For example, we can use the function mbp.delete_known_books, or the following function:

def modify_df(**kwargs):
    df_db = pl.read_database(
        query=f"SELECT * FROM {kwargs['table_name']}",
        connection=kwargs["engine"],
        )
    return df_2.join(
        df_db,
        on="isbn",
        how="anti"
    )

We check now what has been saved into the database:

bh.read_database(
    table_name="books",
    url="sqlite:///database.db",
    schema=bh.schema(),
)
shape: (2, 21)
isbnauthortitleyearpublisherlanguageformatfull_pricemin_priceadelphibuecherfeltrinellihoeplihugendubelibslibcooplibracciolibunimondadoriosianderrizzoli
strstrstrstrstrstrstrf64f64strstrstrstrstrstrstrstrstrstrstrstr
"9783866473256""Karl Marx""Das Kapital""2009""Anaconda"nullnull7.957.95null"https://www.buecher.de/artikel…nullnullnullnullnullnullnullnull"https://www.osiander.de/shop/h…null
"9781857988826""Ursula K. Le Guin""The Dispossessed""1999""Orion Publishing Co"nullnull13.013.0null"https://www.buecher.de/artikel…"https://www.lafeltrinelli.it/d…nullnull"https://www.ibs.it/dispossesse…nullnullnullnull"https://www.osiander.de/shop/h…null