Forum Discussion
Best ways to write a Pandas Dataframe on Fabric SQL Database
- 11 months ago
Hi dpombal,
Connect to Your SQL Database - Microsoft Fabric | Microsoft Learn
Connect with Python Notebook
Fabric Python Notebooks (preview) offer the ability to run T-SQL code with the T-SQL magic command. In the following steps, connect to a SQL database in Fabric using the %%tsql magic command:
Create a notebook in your workspace with the language set to Python.
In a cell, use the %%tsql magic command. The cell type automatically changes to T-SQL.
In the following sample, replace <databasename> with the name of your SQL database in Fabric. The -type parameter should be SQLDatabase.
PythonCopy%%tsql -artifact <databasename> -type SQLDatabaseThen include your T-SQL command. For example, to run a query from a database named Contoso:
PythonCopy%%tsql -artifact Contoso -type SQLDatabase SELECT * FROM SalesLT.Customer;You can also bind the results to a dataframe with the -bind argument:
PythonCopy%%tsql -artifact Contoso -type SQLDatabase -bind df2
For more possibilities to query your data with T-SQL inside Python Notebooks, see Run T-SQL code in Fabric Python notebooks. To see the full syntax, use the %tsql? command. This command displays the help information for the T-SQL magic command, including the available parameters and their descriptions.
If you found this helpful, consider giving some Kudos. If I answered your question or solved your problem, mark this post as the solution
- 11 months ago
Hi dpombal
You can convert pandas dataframe to lakehouse/warehouse using python programming language.
SQL Database = Lakehouse = Fabric Warehouse = Power BI Direct Lake Semantic Model. They all are same. Simple and Sweet.
Hi dpombal,
See this blog post: Connect to your SQL database in Fabric using Python Notebook | Microsoft Fabric Blog | Microsoft Fabric
If you add a T-SQL cell to your notebook and add
“%%tsql -artifact <databasename> -type SQLDatabase”
you should be able to connect to your SQL database.
If you found this helpful, consider giving some Kudos. If I answered your question or solved your problem, mark this post as the solution
- dpombal11 months agoPost Partisan
Yes but how can I save a dataframe from pandas into a SQL database table, my question remains.
- tayloramy11 months agoSuper User
Hi dpombal,
Connect to Your SQL Database - Microsoft Fabric | Microsoft Learn
Connect with Python Notebook
Fabric Python Notebooks (preview) offer the ability to run T-SQL code with the T-SQL magic command. In the following steps, connect to a SQL database in Fabric using the %%tsql magic command:
Create a notebook in your workspace with the language set to Python.
In a cell, use the %%tsql magic command. The cell type automatically changes to T-SQL.
In the following sample, replace <databasename> with the name of your SQL database in Fabric. The -type parameter should be SQLDatabase.
PythonCopy%%tsql -artifact <databasename> -type SQLDatabaseThen include your T-SQL command. For example, to run a query from a database named Contoso:
PythonCopy%%tsql -artifact Contoso -type SQLDatabase SELECT * FROM SalesLT.Customer;You can also bind the results to a dataframe with the -bind argument:
PythonCopy%%tsql -artifact Contoso -type SQLDatabase -bind df2
For more possibilities to query your data with T-SQL inside Python Notebooks, see Run T-SQL code in Fabric Python notebooks. To see the full syntax, use the %tsql? command. This command displays the help information for the T-SQL magic command, including the available parameters and their descriptions.
If you found this helpful, consider giving some Kudos. If I answered your question or solved your problem, mark this post as the solution
- dpombal11 months agoPost Partisan
This option is perfect for writing from a table to a dataframe df2
%%tsql -artifact Contoso -type SQLDatabase -bind df2But I am looking for the inverse option writing from a pandas dataframe into a SQL Database table.
thanks for your help/suggestions