Python: Connecting to Databases Damian Gordon. Connecting to Databases Python allows you to connect to databases and run commands on the database. In.

Slides:



Advertisements
Similar presentations
Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved.
Advertisements

What is MySQL? MySQL is a relational database management system (A relational database stores data in separate tables rather than putting all the data.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
Databases Dan Otero Alex Loddengaard
Introduction To Databases IDIA 618 Fall 2014 Bridget M. Blodgett.
Computing for Bioinformatics Introduction to databases What is a database? Database system components Data types DBMS architectures DBMS systems available.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
MySQL + PHP.  Introduction Before you actually start building your database scripts, you must have a database to place information into and read it from.
Concepts of Database Management Seventh Edition
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
PHP meets MySQL.
Introduction to databases and SQL. What is a database?  A database is an organized way of holding together pieces of information  A database refers.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Working with MSSQL Server Code:G0-C# Version: 1.0 Author: Pham Trung Hai CTD.
MET280: Computing for Bioinformatics Introduction to databases What is a database? Not a spreadsheet. Data types and uses DBMS (DataBase Management System)
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
Installing and Using MySQL and phpMyAdmin. Last Time... Installing Apache server Installing PHP Running basic PHP scripts on the server Not necessary.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
Chapter 5 MYSQL Database. Introduction to MYSQL MySQL is the world's most popular open-source database. Open source means that the source code, the programming.
Most information comes from Chapter 3, MySQL Tutorial: 1 MySQL: Part.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Inserting Data.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
DATABASE TOOLS CS 260 Database Systems. Overview  Database accounts  Oracle SQL Developer  MySQL Workbench.
Access Forms and Queries. Entering Data in Your Table  You can add data to your table in Datasheet view, by typing in the columns and rows.  This.
What’s a database? Data stored in a structured format that lends itself to easy manipulation and recall.
Lesson 10 - Mail Merge and Reviewing Documents Advanced Microsoft Word.
Course FAQ’s I do not have any knowledge on SQL concepts or Database Testing. Will this course helps me to get through all the concepts? What kind of.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
DATA BASE ADMINISTRING DATABASE SERVICES IN RED HAT LINUX.
NSF DUE ; Wen M. Andrews J. Sargeant Reynolds Community College Richmond, Virginia.
Instructor: Pavlos Pavlikas1 How Data is Stored Chapter 8.
Access Queries and Forms. Adding a New Field  To insert a field after you have saved your table, open Access, and open the table  It is easier to add.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
Programming for the Web MySQL Command Line Using PHP with MySQL Dónal Mulligan BSc MA
Slide Set #24: Database security SY306 Web and Databases for Cyber Operations.
Introduction to Database Programming with Python Gary Stewart
Understanding Core Database Concepts Lesson 1. Objectives.
SQL Introduction SQL stands for “Structured Query Language” and can be pronounced as “SQL” or “sequel – (Structured English.
3 A Guide to MySQL.
ASP.NET Programming with C# and SQL Server First Edition
INTRODUCTION TO DATABASES (MICROSOFT ACCESS)
Whatcha doin'? Aims: To start using Python. To understand loops.
Introduction to Dynamic Web Programming
CS320 Web and Internet Programming SQL and MySQL
SQL and SQL*Plus Interaction
Introduction to MySQL.
A very brief introduction
Unix System Administration
Database application MySQL Database and PhpMyAdmin
Database Management  .
Introduction to Ms-Access Submitted By- Navjot Kaur Mahi
Client Access, Queries, Stored Procedures, JDBC
Aqua Data Studio.
Chapter 8 Working with Databases and MySQL
Developing a Model-View-Controller Component for Joomla Part 3
Introduction to Access
CS3220 Web and Internet Programming SQL and MySQL
Queries.
Chapter 11 Managing Databases with SQL Server 2000
CS3220 Web and Internet Programming SQL and MySQL
Understanding Core Database Concepts
SQL – Application Persistence Design Patterns
Lecuter-1.
CS4540 Special Topics in Web Development SQL and MS SQL
Presentation transcript:

Python: Connecting to Databases Damian Gordon

Connecting to Databases Python allows you to connect to databases and run commands on the database. In this lesson we’ll look at how to connect to a MySQL database and get a listing of values in the database.

Introduction to Databases

A database is a collection of data that is structured to allow for easy access. Examples of databases include MySQL, PostgreSQL, Microsoft SQL Server, and Oracle.

Introduction to Databases Let’s imagine that we had to store information about all of the students who register at DIT and we want to keep all of the information in a text file, it might look something like this:

Student Records Jane Smith is the first student and her student number is D , and she joined DIT on 01/09/2013. John Smith is the second student and his student number is D , and he joined DIT on the 10 th of September Jo Smith is the third student and her student number is D , and she joined DIT on 01/09/2014. Joe Smith is another student and his number is, and he joined DIT on 1 st of Sept

Introduction to Databases If I am searching for the student “Joe Smith”, in a text file I have to search each word, one at a time, first looking for the word “Joe” and then checking if the next word is “Smith”.

Student Records Jane Smith is the first student and her student number is D , and she joined DIT on 01/09/2013. John Smith is the second student and his student number is D , and he joined DIT on the 10 th of September Jo Smith is the third student and her student number is D , and she joined DIT on 01/09/2014. Joe Smith is another student and his number is, and he joined DIT on 1 st of Sept

Introduction to Databases There is also a consistency issue, sometimes the date is formatted differently in different sentences, as well as other information.

Student Records Jane Smith is the first student and her student number is D , and she joined DIT on 01/09/2013. John Smith is the second student and his student number is D , and he joined DIT on the 10 th of September Jo Smith is the third student and her student number is D , and she joined DIT on 01/09/2014. Joe Smith is another student and his number is, and he joined DIT on 1 st of Sept

Student Records Jane Smith is the first student and her student number is D , and she joined DIT on 01/09/2013. John Smith is the second student and his student number is D , and he joined DIT on the 10 th of September Jo Smith is the third student and her student number is D , and she joined DIT on 01/09/2014. Joe Smith is another student and his number is, and he joined DIT on 1 st of Sept

Student Records Jane Smith is the first student and her student number is D , and she joined DIT on 01/09/2013. John Smith is the second student and his student number is D , and he joined DIT on the 10 th of September Jo Smith is the third student and her student number is D , and she joined DIT on 01/09/2014. Joe Smith is another student and his number is, and he joined DIT on 1 st of Sept

Student Records Jane Smith is the first student and her student number is D , and she joined DIT on 01/09/2013. John Smith is the second student and his student number is D , and he joined DIT on the 10 th of September Jo Smith is the third student and her student number is D , and she joined DIT on 01/09/2014. Joe Smith is another student and his number is, and he joined DIT on 1 st of Sept

Introduction to Databases We can fix these two issues by putting this information into a table, as follows:

StudentRecords Family Name Given Name GenderOrder No. Student No. Date Joined SmithJaneF1D /09/2013 SmithJohnM2D /09/2013 SmithJoF3D /09/2014 SmithJoeM4NULL01/09/2014

Introduction to Databases The consistency problem is greatly reduced by having the “Order” column of 1, 2, 3, and 4 instead of the text very with “first student”, “second student”, “third student”, and “another student”. The date problem is also fixed, but simply creating the “Date Joined” column in such a way that it only accepts values in the format DD/MM/YYYY.

StudentRecords Family Name Given Name GenderOrder No. Student No. Date Joined SmithJaneF1D /09/2013 SmithJohnM2D /09/2013 SmithJoF3D /09/2014 SmithJoeM4NULL01/09/2014

Introduction to Databases Now lets look at how much faster the search is in a table:

StudentRecords Family Name Given Name GenderOrder No. Student No. Date Joined SmithJaneF1D /09/2013 SmithJohnM2D /09/2013 SmithJoF3D /09/2014 SmithJoeM4NULL01/09/2014

Introduction to Databases We will note that this table has a name, it’s called StudentRecords, it’s important that tables have names, because a database can have multiple tables in it.

Introduction to SQL

Databases need a special programming language to create and control them. The most common language is SQL (pronounced “sequel”). There are a lot of different versions of SQL, but they are all generally similar.

Introduction to SQL Let’s remember our table again:

StudentRecords Family Name Given Name GenderOrder No. Student No. Date Joined SmithJaneF1D /09/2013 SmithJohnM2D /09/2013 SmithJoF3D /09/2014 SmithJoeM4NULL01/09/2014

Introduction to SQL If we want to print out all of the student numbers, we use the following SQL statement: SELECT StudentNo FROM StudentRecords;

Introduction to SQL If we want to print out all of the student numbers, we use the following SQL statement: SELECT StudentNo FROM StudentRecords; SELECT [Field(s)] FROM [Table(s)];

Introduction to SQL If we just want to print out the student numbers of the first two students, we use the following SQL statement: SELECT StudentNo FROM StudentRecords WHERE OrderNo < 3;

Introduction to SQL If we just want to print out the student numbers of the first two students, we use the following SQL statement: SELECT StudentNo FROM StudentRecords WHERE OrderNo < 3; SELECT [Field(s)] FROM [Table(s)] WHERE [Condition(s)];

Setting up a Database

Before we look at the Python code to connect to the database, we need to set up the database, so that Python has something to connect to. So we need to install MySQL, and then create a database and table in the installation.

Setting up a Database To download the MySQL, visit the following webpage: Download, and install MySQL

Setting up a Database You are logged in as username ‘root’, if you are asked for a password, set it as something simple like ‘password’ You can add a new user by saying; CREATE USER NewUser;

Setting up a Database Only you have MySQL running, you need to create a new database as follows: CREATE DATABASE SampleDB; Now we need to tell MySQL we are going to use that database: USE SampleDB;

Setting up a Database To create the StudentRecords table: CREATE TABLE StudentRecords ( OrderNo int(3), FamilyName char(20), GivenName char(20), Gender char(1), StudentNo char(8), JoinDate date);

Setting up a Database To insert values into the table we say: INSERT INTO StudentRecords VALUES(1,'Smith','Joan','F','D ',' '); INSERT INTO StudentRecords VALUES(2,'Smith','John',‘M','D ',' ');

Setting up a Database To list the values in the table we say: SELECT * FROM StudentRecords;

Python Code

To connect to the database in Python we simply say: import mysql.connector cnx = mysql.connector.connect(host='localhost', database='SampleDB', user='root', password='password')

Python Code To run a SQL command we say: cursor = cnx.cursor() cursor.execute(""" select * from StudentRecords """) result = cursor.fetchall() print(result) cnx.close()

Python Code

etc.