SQL Interactions & Functions
Database Interactions
We have implemented the following functionality to interact with the database:
kitab.db.sql_interactions
This module contains the functions for interacting with the SQL database.
SqlHandler
Source code in kitab\db\sql_interactions.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | |
close_cnxn(verbose=False)
Close the connection to the database.
Examples:
>>> from kitab.db.sql_interactions import SqlHandler
>>> from kitab.db.db_credentials import DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME
>>> db = SqlHandler(DB_NAME, user=DB_USER, password=DB_PASSWORD, host=DB_HOST, port=DB_PORT)
>>> db.close_cnxn()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
verbose |
bool
|
Whether to print verbose output. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in kitab\db\sql_interactions.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
drop_table(table_name, verbose=False)
Drops a table from the database if it exists.
Examples:
>>> from kitab.db.sql_interactions import SqlHandler
>>> from kitab.db.db_credentials import DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME
>>> db = SqlHandler(DB_NAME, user=DB_USER, password=DB_PASSWORD, host=DB_HOST, port=DB_PORT)
>>> db.drop_table("Book")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name |
str
|
The name of the table to be dropped. |
required |
verbose |
bool
|
Whether to print verbose output. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in kitab\db\sql_interactions.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | |
execute_commands(commands, verbose=False)
Executes a list of commands in the database.
Examples:
>>> from kitab.db.sql_interactions import SqlHandler
>>> from kitab.db.db_credentials import DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME
>>> db = SqlHandler(DB_NAME, user=DB_USER, password=DB_PASSWORD, host=DB_HOST, port=DB_PORT)
>>> commands = [...]
>>> db.execute_commands(commands)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
commands |
list
|
A list of SQL commands to be executed. |
required |
verbose |
bool
|
Whether to print verbose output. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in kitab\db\sql_interactions.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
get_table(table_name, conditions=None, verbose=False)
Retrieve data from the database table.
Examples:
>>> from kitab.db.sql_interactions import SqlHandler
>>> from kitab.db.db_credentials import DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME
>>> db = SqlHandler(DB_NAME, user=DB_USER, password=DB_PASSWORD, host=DB_HOST, port=DB_PORT)
>>> list_of_conditions = [...]
>>> db.get_table("Book", list_of_conditions)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name |
str
|
The name of the table to retrieve data from. |
required |
conditions |
dict
|
A dictionary representing the conditions to filter records. Defaults to None. |
None
|
verbose |
bool
|
Whether to print verbose output. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: A DataFrame containing the retrieved data. |
Source code in kitab\db\sql_interactions.py
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | |
get_table_columns(table_name, verbose=False)
Retrieves the columns of a table in the database.
Examples:
>>> from kitab.db.sql_interactions import SqlHandler
>>> from kitab.db.db_credentials import DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME
>>> db = SqlHandler(DB_NAME, user=DB_USER, password=DB_PASSWORD, host=DB_HOST, port=DB_PORT)
>>> db.get_table_columns("Book")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name |
str
|
The name of the table whose columns are to be retrieved. |
required |
verbose |
bool
|
Whether to print verbose output. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
A list of column names in the table. |
Source code in kitab\db\sql_interactions.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
insert_many(df, table_name, verbose=False)
Inserts data from a DataFrame into a table in the database.
Examples:
>>> from kitab.db.sql_interactions import SqlHandler
>>> from kitab.db.db_credentials import DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME
>>> db = SqlHandler(DB_NAME, user=DB_USER, password=DB_PASSWORD, host=DB_HOST, port=DB_PORT)
>>> df = pd.DataFrame(...)
>>> db.insert_many(df, "Book")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
DataFrame
|
The DataFrame containing the data to be inserted. |
required |
table_name |
str
|
The name of the table to be dropped. |
required |
verbose |
bool
|
Whether to print verbose output. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in kitab\db\sql_interactions.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
insert_records(table_name, values_list, verbose=False)
Insert one or more records into the database table.
Examples:
>>> from kitab.db.sql_interactions import SqlHandler
>>> from kitab.db.db_credentials import DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME
>>> db = SqlHandler(DB_NAME, user=DB_USER, password=DB_PASSWORD, host=DB_HOST, port=DB_PORT)
>>> list_of_books = [...]
>>> db.insert_records("Book", list_of_books)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values_list |
List[Dict]
|
A list of dictionaries containing column names as keys and their values as values. |
required |
verbose |
bool
|
Whether to print verbose output. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in kitab\db\sql_interactions.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |
remove_records(table_name, conditions_list, verbose=False)
Remove records from the database table based on multiple conditions.
Examples:
>>> from kitab.db.sql_interactions import SqlHandler
>>> from kitab.db.db_credentials import DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME
>>> db = SqlHandler(DB_NAME, user=DB_USER, password=DB_PASSWORD, host=DB_HOST, port=DB_PORT)
>>> list_of_conditions = [...]
>>> db.remove_records("Book", list_of_conditions)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name |
str
|
The name of the table to remove records from. |
required |
conditions_list |
list[dict]
|
A list of dictionaries representing conditions for selecting records to remove. The conditions inside each dictionary are concatenated using AND, and the dictionaries inside the list are concatenated using OR. |
required |
verbose |
bool
|
Whether to print verbose output. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in kitab\db\sql_interactions.py
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | |
truncate_table(table_name, verbose=False)
Truncates a table from the database.
Examples:
>>> from kitab.db.sql_interactions import SqlHandler
>>> from kitab.db.db_credentials import DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME
>>> db = SqlHandler(DB_NAME, user=DB_USER, password=DB_PASSWORD, host=DB_HOST, port=DB_PORT)
>>> db.truncate_table("Book")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name |
str
|
The name of the table to be truncated. |
required |
verbose |
bool
|
Whether to print verbose output. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in kitab\db\sql_interactions.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
update_records(table_name, updated_values, condition, verbose=False)
Update records in the database table based on a given condition.
Examples:
>>> from kitab.db.sql_interactions import SqlHandler
>>> from kitab.db.db_credentials import DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME
>>> db = SqlHandler(DB_NAME, user=DB_USER, password=DB_PASSWORD, host=DB_HOST, port=DB_PORT)
>>> updated_values = {...}
>>> conditions = {...}
>>> db.update_records("Book", updated_values, conditions)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name |
str
|
The name of the table to update records in. |
required |
condition |
dict
|
A dictionary representing the condition for selecting records to update. |
required |
updated_values |
dict
|
A dictionary containing column names as keys and their updated values as values. |
required |
verbose |
bool
|
Whether to print verbose output. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in kitab\db\sql_interactions.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | |
Functions
kitab.db.functions
This module contains tailored functions for interacting with the database. These are used by the API and the recommendation model.
add_book_db(book, verbose=False)
Adds a book to the database.
Examples:
>>> from kitab.db.functions import add_book_db
>>> add_book_db({
"isbn": "1442942355",
"title": "The Ghostly Rental",
"description": "Employing the subtle methods of presenting mysterious ghost stories in the backdrop of psychological troubles, the novel presents the life of James. The troubles that he faces, combined with the baffling events around him give an aura to the novel that is almost unsurpassable",
"available": False,
"authors": [
"Henry James"
],
"genres": [
"Horror",
"Short Stories",
"The United States Of America"
]
})
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
book |
dict
|
A dictionary containing the book information. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the book was successfully added, False otherwise. |
Source code in kitab\db\functions.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
add_recommendation_log(description, recommendation_isbn, successful, verbose=False)
Adds a recommendation log to the history table.
Examples:
>>> from kitab.db.functions import add_recommendation_log
>>> add_recommendation_log(description="In a masterful blend of psychological intrigue and spectral disturbances, this novel unfurls the complex life of Clara. Her internal struggles are mirrored by eerie, inexplicable occurrences, weaving a tale that is both deeply personal and chillingly atmospheric, offering an unparalleled exploration of the human psyche shadowed by the paranormal.", recommendation_isbn="1442942355", successful=True)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
description |
str
|
The description of the recommendation. |
required |
recommendation_isbn |
str
|
The ISBN of the recommended book. |
required |
successful |
bool
|
Whether the recommendation was successful or not. |
required |
verbose |
bool
|
Whether to print verbose output. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the recommendation log was successfully added, False otherwise. |
Source code in kitab\db\functions.py
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 | |
get_authors(ISBNs, verbose=False)
Get the authors for the given list of ISBNs.
Examples:
>>> from kitab.db.functions import get_authors
>>> get_authors(ISBNs=["1442942355", "1613720211"])
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ISBNs |
list[str]
|
A list of ISBNs. |
required |
verbose |
bool
|
Whether to print verbose output. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str:list]
|
dict[str:list]: A dictionary containing the authors for each ISBN. |
Source code in kitab\db\functions.py
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | |
get_book_by_ISBN(ISBN, verbose=False)
Retrieves a book from the database based on its ISBN.
Examples:
>>> from kitab.db.functions import get_book_by_ISBN
>>> get_book_by_ISBN("1442942355")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ISBN |
str
|
The ISBN of the book to retrieve. |
required |
verbose |
bool
|
Whether to print verbose output. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[dict]
|
tuple[dict]: A tuple containing the book information, authors, and genres if found, or None if no book is found. |
Source code in kitab\db\functions.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
get_book_by_title(title, verbose=False)
Retrieves a book from the database based on its title.
Examples:
>>> from kitab.db.functions import get_book_by_title
>>> get_book_by_title("The Ghostly Rental")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title |
str
|
The title of the book to retrieve. |
required |
verbose |
bool
|
Whether to print verbose output. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[dict]
|
tuple[dict]: A tuple containing the book information, authors, and genres if found, or None if no book is found. |
Source code in kitab\db\functions.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
get_genres(ISBNs, verbose=False)
Get the genres for the given list of ISBNs.
Examples:
>>> from kitab.db.functions import get_genres
>>> get_genres(ISBNs=["1442942355", "1613720211"])
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ISBNs |
list[str]
|
A list of ISBNs. |
required |
verbose |
bool
|
Whether to print verbose output. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str:list]
|
dict[str:list]: A dictionary containing the genres for each ISBN. |
Source code in kitab\db\functions.py
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 | |
get_history_by_recommendation_isbn(recommendation_isbn, verbose=False)
Get the history of recommendations for a book with the given ISBN.
Examples:
>>> from kitab.db.functions import get_history_by_recommendation_isbn
>>> get_history_by_recommendation_isbn(recommendation_isbn="1442942355")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
recommendation_isbn |
str
|
The ISBN of the recommended book. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A dictionary containing the history of recommendations for the book. |
Source code in kitab\db\functions.py
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 | |
get_table_from_db(table_name, conditions=None, verbose=False)
Retrieves a table from the database.
Examples:
>>> from kitab.db.functions import get_table_from_db
>>> get_table_from_db("book", conditions={"available": True})
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name |
str
|
The name of the table to retrieve. |
required |
conditions |
dict
|
A dictionary of conditions to filter the table. |
None
|
verbose |
bool
|
Whether to print verbose output. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: A DataFrame containing the table information. |
Source code in kitab\db\functions.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
update_book_db(ISBN, new_book, verbose=False)
Updates a book in the database.
Examples:
>>> from kitab.db.functions import update_book_db
>>> update_book_db("1442942355", {
"available": True,
"genres": [
"Horror",
"Short Stories",
"Mystery"
]
})
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ISBN |
str
|
The ISBN of the book to update. |
required |
new_book |
dict
|
A dictionary containing the updated book information. |
required |
verbose |
bool
|
Whether to print verbose output. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the book was successfully updated, False otherwise. |
Source code in kitab\db\functions.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | |
Loading Data Into the Database
kitab.db.get_data
get_full_data(folder_path='data', verbose=False)
Retrieves and combines data from multiple CSV files and corresponding pickle files.
Examples:
>>> from kitab.db.get_data import get_full_data
>>> get_full_data(folder_path="data")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
folder_path |
str
|
The path to the directory containing the CSV and pickle files. |
'data'
|
verbose |
bool
|
Whether to display logs. Default is False. |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: A DataFrame containing the combined data with an additional 'embedding' column. |
Source code in kitab\db\get_data.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
load_data(folder_path='data', verbose=False)
Load data from a specified folder path and insert it into the database.
Examples:
>>> from kitab.db.get_data import load_data
>>> load_data(folder_path="data")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
folder_path |
str
|
The path to the folder containing the data files. Default is "data". |
'data'
|
verbose |
bool
|
Whether to display logs. Default is False. |
False
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in kitab\db\get_data.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |