Sharing database instance between DatabaseLibrary and custom library

I already found an option, adding it here for other’s reference:

To “share” the same sqlite3 instance between both libraries I used the following (inspired by Passing an object to another class - #12 by Mavnus04 )

import pandas as pd
from robot.libraries.BuiltIn import BuiltIn

def import_csv_into(self, table_name, csv_path):
  databaseLibrary = BuiltIn().get_library_instance(‘DatabaseLibrary’)
  conn = databaseLibrary.connection_store.get_connection(None).client

  csv_df = pd.read_csv(csv_path)
  csv_df.to_sql(table_name, conn, if_exists='replace', index=False)

It will get the default/ currently selected database connection and passes it to read_csv. BTW, this also abstracts the new keyword from sqlite3.

General question: Is this the right way to do this or is there any better way which does not require to access the internal structure of the DatabaseLibrary?

Thanks,
Christian

1 Like