(Diary 1) How to create, reference, and register data in the SQL database of Microsoft Azure service with python

Preface

I want to use Microsoft Azure SQL database to advance my school project, It would be even more convenient if the program could access Microsoft's SQL database. I thought, I created it.

Caution

This article is a Microsoft Azure service and assumes you are creating a SQL database. (I made the default settings with sample data) スクリーンショット 2020-07-08 22.43.48.png

Required python modules

** pydbc ** ... A module that can access the database using a python program (The explanation of pydbc is just a simple explanation. I'm sorry if you want to know more details, but please check it on the web etc.)

If pydbc is MacOS, you can easily install it with pip, but there are differences in the installation method and driver file settings depending on the OS type, so I'm sorry if you want to know more details, but on the web etc. Please take a research on it.

Sample program

** Caution You can execute the program by setting XXXX in the program as follows. スクリーンショット 2020-07-08 23.27.59.png スクリーンショット 2020-07-08 23.28.16.png ・ First of XXXX, enter the character string written in the red part of the SQL database summary screen of your Microsoft Azure.

-In the second of XXXX, enter the character string written in the green part of the SQL database summary screen of your Microsoft Azure.

・ Third of XXXX, enter the character string to be entered in the login of the query editor screen of your Microsoft Azure SQL database. (It is the server administrator login created when creating the database)

-In the fourth of XXXX, enter the character string to be entered in the password of the query editor screen of your Microsoft Azure SQL database. (This is the password created when creating the database)

sample.py


import pyodbc


server = 'XXXX'
database = 'XXXX'
username = 'XXXX'
password = 'XXXX'
driver= '{ODBC Driver 17 for SQL Server}'

#Make a connection to the database
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()

#Creating table data
cursor.execute("CREATE TABLE [dbo].[Sample3](NumberNOTNULL,HeightfloatNOTNULL,WeightfloatNOTNULL);")

#Data registration
cursor.execute("INSERT INTO [dbo].[Sample3]VALUES(1,181.3,90.8)")

#Reference of all data
cursor.execute("SELECT * FROM [dbo].[Sample3]")

#Data output
data = cursor.fetchone()
print(data)

#End the connection to the database
cursor.close()
cnxn.commit()
cnxn.close()

result

スクリーンショット 2020-07-08 23.47.58.png

There was a table in the database that was created on the local PC and registered the data!

Recommended Posts

(Diary 1) How to create, reference, and register data in the SQL database of Microsoft Azure service with python
How to get the date and time difference in seconds with python
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
How to get the number of digits in Python
How to identify the element with the smallest number of characters in a Python list?
[Note] How to write QR code and description in the same image with python
How to count the number of occurrences of each element in the list in Python with weight
It is easy to execute SQL with Python and output the result in Excel
Try scraping the data of COVID-19 in Tokyo with Python
[Homology] Count the number of holes in data with Python
Comparison of how to use higher-order functions in Python 2 and 3
How to create dataframes and mess with elements in pandas
[Introduction to Python] How to get data with the listdir function
How to log in to AtCoder with Python and submit automatically
Overview of Python virtual environment and how to create it
[Super easy! ] How to display the contents of dictionaries and lists including Japanese in Python
I tried to create a Python script to get the value of a cell in Microsoft Excel
How to determine the existence of a selenium element in Python
How to change the log level of Azure SDK for Python
How to know the internal structure of an object in Python
How to check the memory size of a variable in Python
[Python] How to get the first and last days of the month
Output the contents of ~ .xlsx in the folder to HTML with Python
How to configure CORS settings for Azure storage service in Python
How to create a large amount of test data in MySQL? ??
The 14th offline real-time how to write reference problem in python
Create a function to get the contents of the database in Go
The 18th offline real-time how to write reference problem in Python
How to display bytes in the same way in Java and Python
[Python] Create a program to delete line breaks in the clipboard + Register as a shortcut with windows
Part 1 I wrote the answer to the reference problem of how to write offline in real time in Python
[Python] How to specify the window display position and size of matplotlib
The 17th offline real-time how to write reference problem implemented in Python
How to write the correct shebang in Perl, Python and Ruby scripts
The 16th offline real-time how to write reference problem to solve with Python
Try to image the elevation data of the Geographical Survey Institute with Python
I tried to open the latest data of the Excel file managed by date in the folder with Python
How to put OpenCV in Raspberry Pi and easily collect images of face detection results with Python
How to plot galaxy visible light data using OpenNGC database in python
I tried to get and analyze the statistical data of the new corona with Python: Data of Johns Hopkins University
Python code to train and test with Custom Vision of Cognitive Service
The 19th offline real-time how to write reference problem to solve with Python
How to use is and == in Python
Read the data of the NFC reader connected to Raspberry Pi 3 with Python and send it to openFrameworks with OSC
An example of the answer to the reference question of the study session. In python.
Extract the Azure SQL Server data table with pyodbc and try to make it numpy array / pandas dataframe
Part 1 I wrote an example of the answer to the reference problem of how to write offline in real time in Python
How to download all photos of egao school photo service with python base
How to pass the execution result of a shell command in a list in Python
How to get started with the 2020 Python project (windows wsl and mac standardization)
How to pass the path to the library built with pyenv and virtualenv in PyCharm
Summary of how to read numerical data with python [CSV, NetCDF, Fortran binary]
Execute raw SQL using python data source with redash and display the result
[Python] How to name table data and output it in csv (to_csv method)
How to count the number of elements in Django and output to a template
[Azure] I tried to create a Linux virtual machine in Azure of Microsoft Learn
How to use Service Account OAuth and API with Google API Client for python
How to find the coefficient of the trendline that passes through the vertices in Python
I'm addicted to the difference in how Flask and Django receive JSON data
Note: How to get the last day of the month with python (added the first day of the month)