Python And SQL Server - Halvorsen.blog

1y ago
25 Views
2 Downloads
1.89 MB
55 Pages
Last View : 20d ago
Last Download : 3m ago
Upload by : Macey Ridenour
Transcription

https://www.halvorsen.blogPythonand SQL ServerHans-Petter Halvorsen

Free Textbook with lots of Practical amming/python/

Additional Python ramming/python/

Contents Database SystemsSQL ServerSQL Server and PythonCRUD Python ExamplesDatalogging Example

What is a Database? A Database is a structured way to store lots of information. The information inside the database is stored in differenttables. - “Everything” today is stored in databases!Examples: Bank/Account systems Information in Web pages such as Facebook, Wikipedia,YouTube, etc. lots of other examples!

Database SystemsWe communicate with the Database using a Database ManagementSystem (DBMS). We use the Structured Query Language (SQL) inorder to communicate with the Database, i.e., Insert Data, RetrieveData, Update Data and Delete Data from the Database.DatabaseManagementSystem(DBMS)SQL – Structured Query LanguageSQLDatabase

Database Systems OracleMySQLMariaDBSybaseMicrosoft AccessMicrosoft SQL Server. (we have hundreds different DBMS)

SQL Server SQL Server consists of a Database Engine and aManagement Studio. The Database Engine has no graphical interface it is just a service running in the background ofyour computer (preferable on the server). The Management Studio is graphical tool forconfiguring and viewing the information in thedatabase. It can be installed on the server or onthe client (or both).

SQL Server SQL Server Express– Free version of SQL Server that has all we need for theexercises in this Tutorial SQL Server Express consist of 2 parts (separateinstallation packages):– SQL Server Express– SQL Server Management Studio (SSMS) – This software canbe used to create Databases, create Tables, Insert/Retrieveor Modify Data, etc. SQL Server Express Installation:https://youtu.be/hhhggAlUYo8

SQL Server Management Studio3Your SQL Server1Your Database4Write your Query here2YourTables5 The result from your Query

Structured Query Language Structured Query Language (SQL) is used towrite, read and update data from theDatabase System You can use SQL inside the “SQL ServerManagement Studio” or inside your Pythonscript. SQL Example: select * from SCHOOL

SQL ExamplesQuery Examples: insert into STUDENT (Name , Number, SchoolId)values ('John Smith', '100005', 1) select SchoolId, Name from SCHOOL select * from SCHOOL where SchoolId 100 update STUDENT set Name 'John Wayne' where StudentId 2 delete from STUDENT where SchoolId 3We have 4 different Query Types: INSERT, SELECT, UPDATE and DELETECRUD: C – Create or Insert Data, R – Retrieve (Select) Data, U – Update Data, D – Delete Data

https://www.halvorsen.blogPythonHans-Petter Halvorsen

Python Python is a fairly old Programming Language (1991)compared to many other Programming Languages like C#(2000), Swift (2014), Java (1995), PHP (1995). Python has during the last 10 years become more and morepopular. Today, Python has become one of the most popularProgramming Languages.Software used in this Tutorial: Anaconda Distribution (Python most usedLibraries/Packages are included) Spyder Python editor (included with Anaconda Distribution)

Python Drivers for SQL Server There are several python SQL drivers available:– pyodbc– pymssql These Drivers are not made made Microsoft but thePython Community. However, Microsoft places its testing efforts and itsconfidence in pyodbc driver. Microsoft contributes to the pyODBC open-sourcecommunity and is an active participant in therepository at n/python-driver-for-sql-server

pyodbc pyodbc is an open-source Pythonmodule that can access ODBCdatabases, e.g., SQL Server https://pypi.org/project/pyodbc/ Installation: pip install pyodbc

pyodbcpip install pyodbc

https://www.halvorsen.blogPython ExamplesHans-Petter Halvorsen

Database CRUDAll Database Systems supports CRUDC – Create or Insert DataR – Retrieve DataU – Update DataD – Delete DataLet's go through some Python examples

Python ExamplesNote! The examples provided can beconsidered as a “proof of concept” The sample code is simplified forclarity and doesn't necessarilyrepresent best practices.

SQL Server DatabaseLet's Create a simple Table called “BOOK”:CREATE TABLE [BOOK]([BookId] [int] IDENTITY(1, 1) NOT NULL PRIMARY KEY,[Title] [varchar](50) NOT NULL UNIQUE,[Author] [varchar](50) NOT NULL,[Category] [varchar](50) NOT NULL)GO

SQL Server DatabaseLet’s insert some data into the BOOK Table:INSERT INTO BOOK (Title, Author, Category)VALUES ('Python Program','Knut Hamsun', 'Data’)GoINSERT INTO BOOK (Title, Author, Category)VALUES ('Music History','Elvis Presley', 'Music’)GOINSERT INTO BOOK (Title, Author, Category)VALUES ('Et Dukkehjem','Henrik Ibsen', 'Novel')GO

SQL Server DatabaseWe use SQL Server management Studio in order to create the Table and Data:

SQL Server DatabaseWe see that the Table and the Data have been created:

https://www.halvorsen.blogConnect to Databaseusing PythonHans-Petter Halvorsen

Connect to Database from PythonThe newest andrecommend driverimport pyodbcdriver "{ODBC Driver 17 for SQL Server}"server "xxxxxx"database "xxxxx"username "xxxxx"password "xxxxxx"conn pyodbc.connect("DRIVER " driver ";SERVER " server ";DATABASE " database ";UID " username ";PWD " password )

Connect to Database from PythonExample:import pyodbcServer NameIf Server is on your local PC,you can use LOCALHOSTdriver "{ODBC Driver 17 for SQL Server}"server “TESTPC\\SQLEXPRESS"database "BOOKSTORE"username "sa"password "Test123"conn pyodbc.connect("DRIVER " driver ";SERVER " server ";DATABASE " database ";UID " username ";PWD " password )Instance Name (you can havemultiple instances of SQL Serveron the same computer)Here is the built-in “sa” user (System Administrator) used to connect to the Database. Ingeneral, you should use another user than the sa user. The sa user is used here forsimplicity. You can easily create new user in SQL Server Management Studio

https://www.halvorsen.blogRetrieve Datausing PythonHans-Petter Halvorsen

Pythonimport pyodbcdriver "{ODBC Driver 17 for SQL Server}"server "NUCHPH\\SQLEXPRESS"database "BOOKSTORE"username "sa"password "xxxxxx"conn pyodbc.connect("DRIVER " driver ";SERVER " server ";DATABASE " database ";UID " username ";PWD " password )1 Python Program Knut Hamsun Data2 Music History Elvis Presley Music3 Et Dukkehjem Henrik Ibsen Novelcursor conn.cursor()for row in cursor.execute("select BookId, Title, Author, Category from BOOK"):print(row.BookId, row.Title, row.Author, row.Category)

Python - Alternativeimport pyodbcdriver "{ODBC Driver 17 for SQL Server}"server "NUCHPH\\SQLEXPRESS"database "BOOKSTORE"username "sa"password "xxxxxx"conn pyodbc.connect("DRIVER " driver ";SERVER " server ";DATABASE " database ";UID " username ";PWD " password )1 Python Program Knut Hamsun Data2 Music History Elvis Presley Music3 Et Dukkehjem Henrik Ibsen Novelcursor conn.cursor()cursor.execute("select BookId, Title, Author, Category from BOOK")row cursor.fetchone()while row:print(row[0], row[1], row[2], row[3])row cursor.fetchone()

“Hide” Connection StringYou can put the Connection string in a separate Python File, e.g., “database.py”:def GetConnectionString():driver "{ODBC Driver 17 for SQL Server}"server "NUCHPH\\SQLEXPRESS"database "BOOKSTORE"username "sa"password "xxxxxx"connectionString "DRIVER " driver ";SERVER " server ";DATABASE " database ";UID " username ";PWD " passwordreturn connectionStringimport pyodbcimport databaseconnectionString database.GetConnectionString()conn pyodbc.connect(connectionString)cursor conn.cursor()for row in cursor.execute("select BookId, Title, Author, Category from BOOK"):print(row.BookId, row.Title, row.Author, row.Category)

SELECT WHERE .Using a SELECT statement with a WHERE clauseExample:import pyodbcimport databaseconnectionString database.GetConnectionString()conn pyodbc.connect(connectionString)cursor conn.cursor()query "select BookId, Title, Author, Category from BOOK where Category 'Data'"for row in cursor.execute(query):print(row.BookId, row.Title, row.Author, row.Category)

Using Parameters- Avoid SQL Injection ODBC supports parameters using a question mark as aplace holder in the SQL. You provide the values for thequestion marks by passing them after the SQL This is safer than putting the values into the stringbecause the parameters are passed to the databaseseparately, protecting against SQL injection attacks. It is also be more efficient if you execute the same SQLrepeatedly with different iki/Getting-started

Using Parameters- Avoid SQL InjectionExample:import pyodbcimport databaseconnectionString database.GetConnectionString()conn pyodbc.connect(connectionString)cursor conn.cursor()query "select BookId, Title, Author, Category from BOOK where Category ?"parameters ['Data']for row in cursor.execute(query, parameters):print(row.BookId, row.Title, row.Author, row.Category)

https://www.halvorsen.blogInsert Datausing PythonHans-Petter Halvorsen

INSERTBasic Example:import pyodbcimport databaseconnectionString database.GetConnectionString()conn pyodbc.connect(connectionString)cursor conn.cursor()query "INSERT INTO BOOK (Title, Author, Category) VALUES ('Python for Experts', 'Halvorsen', 'Data')"cursor.execute(query)cursor.commit()

INSERT with Row CountWith Row Count: You often want to know how many records were inserted. Thenyou can use the Cursor rowcount attribute:import pyodbcimport databaseconnectionString database.GetConnectionString()conn pyodbc.connect(connectionString)cursor conn.cursor()query "INSERT INTO BOOK (Title, Author, Category) VALUES ('Python for Fun', 'Halvorsen', 'Data')"count ('Rows inserted: ' str(count))

INSERT with ParametersIn this example, you see how to run an INSERT statement safely, and pass parameters. Theparameters protect your application from SQL injection.import pyodbcimport databaseconnectionString database.GetConnectionString()conn pyodbc.connect(connectionString)cursor conn.cursor()query "INSERT INTO BOOK (Title, Author, Category) VALUES (?,?,?)"parameters 'Python for Beginners', 'Hans-Petter Halvorsen', 'Data'count cursor.execute(query, parameters).rowcountcursor.commit()print('Rows inserted: ' str(count))

https://www.halvorsen.blogModify Datausing PythonHans-Petter Halvorsen

UPDATEimport pyodbcimport databaseconnectionString database.GetConnectionString()conn pyodbc.connect(connectionString)cursor conn.cursor()query "UPDATE BOOK SET Title 'Python Tutorial' WHERE BookId 5"cursor.execute(query)cursor.commit()

UPDATE with Row Countimport pyodbcimport databaseconnectionString database.GetConnectionString()conn pyodbc.connect(connectionString)cursor conn.cursor()query "UPDATE BOOK SET Title 'Python Tutorial' WHERE BookId 5"count ('Rows updated: ' str(count))

UPDATE with Parameterimport pyodbcimport databaseconnectionString database.GetConnectionString()conn pyodbc.connect(connectionString)cursor conn.cursor()query "UPDATE BOOK SET Title 'Python Tutorial' WHERE BookId ?"bookId 5parameters bookIdcount cursor.execute(query, parameters).rowcountcursor.commit()print('Rows updated: ' str(count))

https://www.halvorsen.blogDelete Datausing PythonHans-Petter Halvorsen

DELETEimport pyodbcimport databaseconnectionString database.GetConnectionString()conn pyodbc.connect(connectionString)cursor conn.cursor()query "DELETE FROM BOOK WHERE BookId 10"cursor.execute(query)cursor.commit()

DELETE with Row Countimport pyodbcimport databaseconnectionString database.GetConnectionString()conn pyodbc.connect(connectionString)cursor conn.cursor()query "DELETE FROM BOOK WHERE BookId 8"count ('Rows deleted: ' str(count))

DELETE with Parameterimport pyodbcimport databaseconnectionString database.GetConnectionString()conn pyodbc.connect(connectionString)cursor conn.cursor()query "DELETE FROM BOOK WHERE BookId ?"parameters 12count cursor.execute(query, parameters).rowcountcursor.commit()print('Rows deleted: ' str(count))

https://www.halvorsen.blogDatalogging ExampleHans-Petter Halvorsen

Datalogging Example We can log data from a DAQ device orsimilar We start by creating a simple RandomGenerator that simulates a TemperatureSensor and log these data to the SQLServer database Then we will in another script read thedata from the database and plot them.

SQL Server DatabaseLet's create a New Database called, e.g., “LOGGINGSYSTEM”We insert the following Table:CREATE TABLE [MEASUREMENTDATA]([MeasurmentId] [int] IDENTITY(1, 1) NOT NULL PRIMARY KEY,[SensorName] [varchar](50) NOT NULL,[MeasurementValue] float NOT NULL,[MeasurementDateTime] datetime NOT NULL)GO

Logging Dataimport pyodbcimport randomimport timefrom datetime import datetimeimport database# Connect to DatabaseconnectionString database.GetConnectionString()conn pyodbc.connect(connectionString)cursor conn.cursor()query "INSERT INTO MEASUREMENTDATA (SensorName, MeasurementValue, MeasurementDateTime) VALUES (?,?,?)"sensorName "Temperature"Ts 10 # Sampling TimeN 20for k in range(N):# Generate Random DataLowLimit 20UpperLimit 25measurementValue random.randint(LowLimit, UpperLimit)#Find Date and Timenow datetime.now()datetimeformat "%Y-%m-%d %H:%M:%S"measurementDateTime now.strftime(datetimeformat)# Insert Data into Databaseparameters sensorName, measurementValue, measurementDateTimecursor.execute(query, parameters)cursor.commit()# Waittime.sleep(Ts)

Logged Data

Plotting Dataimport pyodbcimport matplotlib.pyplot as pltimport databasesensorName "Temperature"# Connect to DatabaseconnectionString database.GetConnectionString()conn pyodbc.connect(connectionString)cursor conn.cursor()query "SELECT MeasurementValue, MeasurementDateTime FROM MEASUREMENTDATA WHERE SensorName ?"parameters [sensorName]t []; data []# Retrieving and Formatting Datafor row in cursor.execute(query, parameters):measurementValue row.MeasurementValuemeasurementDateTime e)t.append(measurementDateTime)# Plottingplt.plot(t, data, 'o-')plt.title('Temperature')plt.xlabel('t [s]')plt.ylabel('Temp [degC]')plt.grid()plt.show()

Plotted Data

Additional Python ramming/python/

Hans-Petter HalvorsenUniversity of South-Eastern Norwaywww.usn.noE-mail: hans.p.halvorsen@usn.noWeb: https://www.halvorsen.blog

SQL Server SQL Server Express -Free version of SQL Server that has all we need for the exercises in this Tutorial SQL Server Express consist of 2 parts (separate installation packages): -SQL Server Express -SQL Server Management Studio (SSMS) -This software can be used to create Databases, create Tables, Insert/Retrieve or Modify Data, etc.

Related Documents:

Server 2005 , SQL Server 2008 , SQL Server 2008 R2 , SQL Server 2012 , SQL Server 2014 , SQL Server 2005 Express Edition , SQL Server 2008 Express SQL Server 2008 R2 Express , SQL Server 2012 Express , SQL Server 2014 Express .NET Framework 4.0, .NET Framework 2.0,

MS SQL Server: MS SQL Server 2017, MS SQL Server 2016, MS SQL Server 2014, MS SQL Server 2012, MS SQL Server 2008 R2, 2008, 2008 (64 bit), 2008 Express, MS SQL Server 2005, 2005 (64 bit), 2005 Express, MS SQL Server 2000, 2000 (64 bit), 7.0 and mixed formats. To install the software, follow the steps: 1. Double-click Stellar Repair for MS SQL.exe.

SQL Server 2005 SQL Server 2008 (New for V3.01) SQL Server 2008 R2 (New for V3.60) SQL Server 2012 (New for V3.80) SQL Server 2012 R2 (New for V3.95) SQL Server 2014 (New for V3.97) SQL Server 2016 (New for V3.98) SQL Server 2017 (New for V3.99) (Recommend Latest Service Pack) Note: SQL Server Express is supported for most situations. Contact .

install the SQL Server Reporting Services 2019 or 2017 installation program. For more information, see Installing SQL Server Reporting Services for SQL Server 2019 and SQL Server 2017 To use the SQL Installer to install a full version of SQL Server software (for example, SQL Server 2019 Standard editio

4395 querying data with transact -sql (m20761) microsoft sql server 6552 querying microsoft sql server 2014 (m20461) microsoft sql server 1833 sql server performance tuning and optimization (m55144) microsoft sql server 4394 updating your skills to sql server 2016 (m10986) microsoft sql server

4. To upgrade SQL Server 2008 SP3 Express to SQL Server 2012 SP2 Express, launch th e installer for SQL Server 2012 SP2 Express, and then follow the wizard. 5. Select the Upgrade from SQL Server 2005, SQL Server 2008 or SQL Server 2008R2 option under the Installation option. The Upgrade to SQL

Python Programming for the Absolute Beginner Second Edition. CONTENTS CHAPTER 1 GETTING STARTED: THE GAME OVER PROGRAM 1 Examining the Game Over Program 2 Introducing Python 3 Python Is Easy to Use 3 Python Is Powerful 3 Python Is Object Oriented 4 Python Is a "Glue" Language 4 Python Runs Everywhere 4 Python Has a Strong Community 4 Python Is Free and Open Source 5 Setting Up Python on .

Brian Nicholas Harris, CC Citizen and Glazier . Barbara Anne Bear Citizen and Musician Maureen Sutherland Smith a Public Relations Consultancy Chairman 78 Hamilton Terrace, St Johns Wood, Westminster The Rt Hon The Lord Mayor Barbara Anne Bear Citizen and Musician Michael John Henesy a Meat and Fish Specialist 49 Hatton House, St Georges Estate, Stepney, Tower Hamlets Susan Margaret Hughes .