List of sql tables in pandas.read_sql

I would like to open a SQL 2005 database (the file has the extension .mdf), and I tried this as such:

import pandas as pd import pyodbc server = 'server_name' db = 'database_name' conn = pyodbc.connect('DRIVER={SQL Server};SERVER=' + server + ';DATABASE=' + db + ';Trusted_Connection=yes') sql = """ SELECT * FROM table_name """ df = pd.read_sql(sql, conn) 

Is there a way to query the database and list all the tables using Pandas or pyodbc? I have practically no experience with databases, so any help would be great.

+5
source share
2 answers

This answer may be useful: How to get a list of all tables in a database using TSQL?

Trying to change your SQL string to:

 sql = """ SELECT * FROM information_schema.tables """ 
+1
source
 import pyodbc as db import pandas as pd conn = db.connect("DRIVER={SQL Server}; SERVER=YourServerName; PORT=1433; DATABASE=YourDB; UID=User; PWD=Password;") cursor = conn.cursor() cursor.execute('''select * from sys.databases''') df=pd.DataFrame(cursor.fetchall()) 
-1
source

Source: https://habr.com/ru/post/1233257/


All Articles