Python Forum
New to Python, please help with checking Azure SQL DB for a table
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to Python, please help with checking Azure SQL DB for a table
#1
Hi,
I've just decided to try and learn Python, I've got very limited experience with programming languages, but have been working with PowerShell for a while so hoping this well help me.

So basically I'm trying to learn Python but developing my own App, I'm trying to get the app to check my Azure SQL DB for a table and if it doesn't exist, create it.

Im trying to use a IF statement to query the DB using the below, Ive excluded the connection details, as i can get the Table created so i know the connction details are good.
sql_check = """
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = N'employee'"""
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password)
def check_database_table():
    cursor = cnxn.cursor()
    r = cursor.execute(sql_check)
    print(r)
And then i was going to do something like if r = null then do something, but i do get a value back from r

Hope this makes sense.
Reply
#2
you can use 'create table if not exists' it won't destroy current table if already exists.
Reply
#3
pyodbc implements python's DB api 2.0, which is documented here, in pep 249. The api says that the return value of cursor.execute() is unspecified. After this statement, you can read the rows of the result by using cursor.fetchone() or cursor.fetchall() for example.
Reply
#4
Ok, thanks for the replies. I don't really understand when to use the
cursor.fetchall()
when I try I don't get any output.
Reply
#5
After cursor.execute(request) you write rows = cursor.fetchall() for example.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Upload Files to Azure Storage Container phillyfa 6 581 Dec-22-2023, 06:11 AM
Last Post: Pedroski55
  azure TTS from text files to mp3s mutantGOD 2 1,638 Jan-17-2023, 03:20 AM
Last Post: mutantGOD
  Get Azure activity logs using python script raham3406 4 3,514 Apr-27-2021, 05:10 AM
Last Post: raham3406
  Wanted to tag azure resources with creator name raham3406 0 1,423 Apr-25-2021, 02:24 PM
Last Post: raham3406
  How to save CSV file data into the Azure Data Lake Storage Gen2 table? Mangesh121 0 2,079 Jun-26-2020, 11:59 AM
Last Post: Mangesh121
  Checking Hyperthreding status on python Fletcher 0 1,545 Apr-19-2020, 11:45 PM
Last Post: Fletcher
  Python Development Server in Azure phpMick 1 1,825 Nov-18-2019, 03:08 PM
Last Post: snippsat
  Upload files to Google/Azure/AWS or cloud drives using python tej7gandhi 0 1,882 May-11-2019, 03:02 PM
Last Post: tej7gandhi
  Python & Azure Log Analytics SitoRBJ 0 3,220 Jun-12-2018, 01:38 PM
Last Post: SitoRBJ
  Checking connectivity for sockets in Python pikkip 1 3,003 Apr-27-2017, 05:02 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020