Microsoft SQL Server
This example demonstrates how to use the Microsoft SQL Server connector in ydata-sdk.
Example Code
"""Example file to run the Microsoft SQL Server and Azuere SQL Server
connector."""
from ydata.connectors import AzureSQLConnector
if __name__ == "__main__":
USERNAME = "insert-username"
PASSWORD = "insert-password"
HOSTNAME = "insert-hostname"
PORT = "1433"
DATABASE_NAME = "berka"
conn_str = {
"hostname": HOSTNAME,
"username": USERNAME,
"password": PASSWORD,
"port": PORT,
"database": DATABASE_NAME,
}
query = "SELECT * FROM dbo.district"
# Create the cnnection to the database
conn = AzureSQLConnector(conn_string=conn_str)
##Getting database schema
print(conn.get_database_schema(schema_name="berka"))
print(f"Trying to connect to database: {conn.connection.connect()} ")
database = conn.read_database(lazy=False, schema_name="dbo")
print(database)
print(f"Printing district table from the database: {database['district'].head()}")
# Read a given table from AzureSQL selecte schema
table_account = conn.get_table(table="account",
schema_name="berka")
print(f"Testing get table method: {table_account}")
# Read a sample of the database
table_sample = conn.get_table_sample(table='account',
schema_name='berka')
result_query = conn.query(query="SELECT * FROM berka.account;")
print(f"Testing a query: {result_query}")
# Write the content of a Dataset into a new table
# Only creation of new tables is supported
# If trying to write the information into an already existing table, an exception will be raised
print('writing the table')
conn.write_table(data=result_query,
name="new_account",
schema_name="dbo",
if_exists='replace')