DirectDBConnection#

class astropathdb.db_io.dbconnect.DirectDBConnection#

Connect directly to an AstroPath SQL Server database to write tables.

Writing requires database write permissions. Fill in creds with the ‘username’, ‘password’, ‘IP’, and ‘port’ of the SQL Server before connecting.

Examples

Write a table to the ‘test’ database:

from astropathdb.db_io import DirectDBConnection

db = DirectDBConnection()
db.creds = {
    "username": "<username>",
    "password": "<password>",
    "IP": "<server>",
    "port": 1433,
}
db.open_connection(database="test")
db.write_to_database(df, table_name="my_table")

DirectDBConnection Attributes#

DirectDBConnection Methods#

open_connection([database])

Connect to a database with creds.

write_to_database(data, table_name[, if_exists, index])

Write a dataframe to a database table.

DirectDBConnection References#

Attribute Reference#

attribute dbengine = None#
attribute dbmeta = None#
attribute creds#

Method Reference#

method open_connection(database='test')#

Connect to a database with creds.

Sets dbengine to the SQLAlchemy engine and dbmeta to the database’s reflected table metadata.

Parameters:

database (str) – The database to connect to.

Return type:

None

method write_to_database(data, table_name, if_exists='append', *, index=False)#

Write a dataframe to a database table.

Connects to the ‘test’ database if no connection is open. Geometries in a GeoDataFrame are written as well-known text.

Parameters:
  • data (DataFrame | GeoDataFrame) – The data to write.

  • table_name (str) – The table to write to.

  • if_exists (str) – What to do if the table exists: ‘fail’, ‘replace’, or ‘append’.

  • index (bool) – Write the dataframe index as a column.

Return type:

None