1565406275
There are always situations where I want to automate small tasks. I like using Python for these kind of things, you can quickly get something working without much hassle. I needed to perform some database changes in a Microsoft SQL Server database and wanted to connect to it using Python. On Windows this is usually pretty straight forward. But I use macOS as my main operating system and I had some hurdles along the way so here is a quick how to.
If Homebrew isn't installed yet let's do that first. It's an excellent package manager for macOS. Paste the following command in the terminal to install it:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Once finished run the following commands to install the Microsoft ODBC Driver 13.1 for SQL Server. This driver will be used to connect to the MS SQL database.
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release brew update brew install msodbcsql@13.1.9.2 mssql-tools@14.0.6.0
With the driver installed, we'll be using a Python package that uses the driver to connect to the database. There are two frequently used packages to connect in Python: pyodbc
and pypyodbc
. pypyodbc
is a pure Python re-implementation of pyodbc
. The main thing I took a way was that pypyodbc
is easier to set up on different platforms. I could not get pyodbc
working, it simply wouldn't connect to the database.
Installing pypyodbc
can be done using pip, the python package installer.
pip install pypyodbc
Now that the necessary components have been installed, let's open our favorite code editor and write code. The first thing that needs to be done is importing pypyodbc
. The script starts with this:
import pypyodbc
Then we need a connection to the database:
sqlConnection = pypyodbc.connect( "Driver={ODBC Driver 13 for SQL Server};" "Server=<server IP address>;" "Database=<database>;" "uid=<username>;pwd=<password>");
Note that you have to replace four values in this code: server IP address
, database
, username
and password
. The value for the driver is a hard coded value which indicates what driver to use to connect to the database, this value points to the driver that was installed earlier.
Now all what rests is use the connection and run a query.
cursor = sqlConnection.cursor() cursor.execute("select * from Values")
The cursor now contains the result of our query, the property cursor.rowcount
returns the number of rows the query returned. It's now possible to loop through the rows and access the different columns:
for row in cursor: print(cursor) # first column firstColumn = row[0] # ...
When we're done, we need to clean up the cursor and database connection by closing it.
cursor.close() sqlConnection.close()
And that's it, save the file and use the python <filename>.py
or python3 <filename.py>
command, this depends on your configuration, to run. Here is the entire script:
import pypyodbcsqlConnection = pypyodbc.connect(
“Driver={ODBC Driver 13 for SQL Server};”
“Server=<server IP address>;”
“Database=<database>;”
“uid=<username>;pwd=<password>”);cursor = sqlConnection.cursor()
cursor.execute(“select * from Values”)for row in cursor:
print(cursor)
# first column
firstColumn = row[0]
# …cursor.close()
sqlConnection.close()
The with
syntax can also be used to automatically close the cursor and the connection, this is another way of writing the same script:
import pypyodbcwith pypyodbc.connect(
“Driver={ODBC Driver 13 for SQL Server};”
“Server=<server IP address>;”
“Database=<database>;”
“uid=<username>;pwd=<password>”) as sqlConnection:with sqlConnection.cursor() as cursor: cursor.execute("select * from Values") for row in cursor: print(cursor) # first column firstColumn = row[0] # ...
If you’re looking for some more reading on the topic:
Thanks for reading. If you liked this post, share it with all of your programming buddies!
Further reading
☞ Python for Time Series Data Analysis
☞ Python Programming For Beginners From Scratch
☞ Python Network Programming | Network Apps & Hacking Tools
☞ Intro To SQLite Databases for Python Programming
☞ Ethical Hacking With Python, JavaScript and Kali Linux
☞ Beginner’s guide on Python: Learn python from scratch! (New)
☞ Python for Beginners: Complete Python Programming
#python #sql #database #macos #web-development
1625843760
When installing Machine Learning Services in SQL Server by default few Python Packages are installed. In this article, we will have a look on how to get those installed python package information.
When we choose Python as Machine Learning Service during installation, the following packages are installed in SQL Server,
#machine learning #sql server #executing python in sql server #machine learning using python #machine learning with sql server #ml in sql server using python #python in sql server ml #python packages #python packages for machine learning services #sql server machine learning services
1594369800
SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.
Models for SQL exist. In any case, the SQL that can be utilized on every last one of the major RDBMS today is in various flavors. This is because of two reasons:
1. The SQL order standard is genuinely intricate, and it isn’t handy to actualize the whole standard.
2. Every database seller needs an approach to separate its item from others.
Right now, contrasts are noted where fitting.
#programming books #beginning sql pdf #commands sql #download free sql full book pdf #introduction to sql pdf #introduction to sql ppt #introduction to sql #practical sql pdf #sql commands pdf with examples free download #sql commands #sql free bool download #sql guide #sql language #sql pdf #sql ppt #sql programming language #sql tutorial for beginners #sql tutorial pdf #sql #structured query language pdf #structured query language ppt #structured query language
1620633584
In SSMS, we many of may noticed System Databases under the Database Folder. But how many of us knows its purpose?. In this article lets discuss about the System Databases in SQL Server.
Fig. 1 System Databases
There are five system databases, these databases are created while installing SQL Server.
#sql server #master system database #model system database #msdb system database #sql server system databases #ssms #system database #system databases in sql server #tempdb system database
1621850444
When working in the SQL Server, we may have to check some other databases other than the current one which we are working. In that scenario we may not be sure that does we have access to those Databases?. In this article we discuss the list of databases that are available for the current logged user in SQL Server
#sql server #available databases for current user #check database has access #list of available database #sql #sql query #sql server database #sql tips #sql tips and tricks #tips
1600347600
This is part 3 of “MS SQL Server- Zero to Hero” and in this article, we will be discussing about the SCHEMAS in SQL SERVER. Before getting into this article, please consider to visit previous articles in this series from below,
In part one, we learned the basics of data, database, database management system, and types of DBMS and SQL.
#sql server #benefits of schemas #create schema in sql #database schemas #how to create schema in sql server #schemas #schemas in sql server #sql server schemas #what is schema in sql server