☰
Python Across Disciplines
with Python + AI Tool   
×
Table of Contents

1.1.   Introduction 1.2.   About the Author & Contact Info 1.3.   Book Conventions 1.4.   What (Who) is a Programmer? 1.5.   Programming Across Disciplines 1.6.   Foundational Computing Concepts 1.7.   About Python 1.8.   First Steps 1.8.1 Computer Setup 1.8.2 Python print() Function 1.8.3 Comments
2.1. About Data 2.2. Data Types 2.3. Variables 2.4. User Input 2.5. Data Structures (DS)         2.5.1. DS Concepts         2.5.2. Lists         2.5.3. Dictionaries         2.5.4. Others 2.6. Files         2.6.1. Files & File Systems         2.6.2. Python File Object         2.6.3. Data Files 2.7. Databases
3.1. About Processing 3.2. Decisions         3.2.1 Decision Concepts         3.2.2 Conditions & Booleans         3.2.3 if Statements         3.2.4 if-else Statements         3.2.5 if-elif-else Statements         3.2.6 In-Line if Statements 3.3. Repetition (a.k.a. Loops)         3.3.1  Repetition Concepts         3.3.2  while Loops         3.3.3  for Loops         3.3.4  Nested Loops         3.3.5  Validating User Input 3.4. Functions         3.4.1  Function Concepts         3.4.2  Built-In Functions         3.4.3  Programmer Defined Functions 3.5. Libraries         3.5.1  Library Concepts         3.5.2  Standard Library         3.5.3  External Libraries 3.6. Processing Case Studies         3.6.1  Case Studies         3.6.2  Parsing Data
4.1. About Output 4.2. Advanced Printing 4.3. Data Visualization   4.4  Sound
  4.5  Graphics
  4.6  Video
  4.7  Web Output
  4.8  PDFs & Documents
  4.9  Dashboards
  4.10  Animation & Games
  4.11  Text to Speech

5.1 About Disciplines 5.2 Accounting 5.3 Architecture 5.4 Art 5.5 Artificial Intelligence (AI) 5.6 Autonomous Vehicles 5.7 Bioinformatics 5.8 Biology 5.9 Bitcoin 5.10 Blockchain 5.11 Business 5.12 Business Analytics 5.13 Chemistry 5.14 Communication 5.15 Computational Photography 5.16 Computer Science 5.17 Creative Writing 5.18 Cryptocurrency 5.19 Cultural Studies 5.20 Data Analytics 5.21 Data Engineering 5.22 Data Science 5.23 Data Visualization 5.24 Drone Piloting 5.25 Economics 5.26 Education 5.27 Engineering 5.28 English 5.29 Entrepreneurship 5.30 Environmental Studies 5.31 Exercise Science 5.32 Film 5.33 Finance 5.34 Gaming 5.35 Gender Studies 5.36 Genetics 5.37 Geography 5.38 Geology 5.39 Geospatial Analysis ☯ 5.40 History 5.41 Humanities 5.42 Information Systems 5.43 Languages 5.44 Law 5.45 Linguistics 5.46 Literature 5.47 Machine Learning 5.48 Management 5.49 Marketing 5.50 Mathematics 5.51 Medicine 5.52 Military 5.53 Model Railroading 5.54 Music 5.55 Natural Language Processing (NLP) 5.56 Network Analysis 5.57 Neural Networks 5.58 Neurology 5.59 Nursing 5.60 Pharmacology 5.61 Philosophy 5.62 Physiology 5.63 Politics 5.64 Psychiatry 5.65 Psychology 5.66 Real Estate 5.67 Recreation 5.68 Remote Control (RC) Vehicles 5.69 Rhetoric 5.70 Science 5.71 Sociology 5.72 Sports 5.73 Stock Trading 5.74 Text Mining 5.75 Weather 5.76 Writing
6.1. Databases         6.1.1 Overview of Databases         6.1.2 SQLite Databases         6.1.3 Querying a SQLite Database         6.1.4 CRUD Operations with SQLite         6.1.5 Connecting to Other Databases
Built-In Functions Conceptss Data Types Date & Time Format Codes Dictionary Methods Escape Sequences File Access Modes File Object Methods Python Keywords List Methods Operators Set Methods String Methods Tuple Methods Glossary Index Appendices   Software Install & Setup
  Coding Tools:
  A.  Python    B.  Google CoLaboratory    C.  Visual Studio Code    D.  PyCharm IDE    E.  Git    F.  GitHub 
  Database Tools:
  G.  SQLite Database    H.  MySQL Database 


Python Across Disciplines
by John Gordon © 2023

Table of Contents

Table of Contents  »  Chapter 6.1.1 : Overview of Databases

Overview of Databases
Subscribe Contact


Contents

Overview

Databases are ubiquitous, which means, they are everywhere. There are databases on your smartphone. There are databases behind every social media platform (Facebook, Twitter, Snapchat, etc.). There are databases in nearly every modern computer game. There are databases used in nearly every business that exists today. And the list goes on and on where you will find databases. It is critical for programmers to gain an understanding of what databases are, how to work with them, and how software and businesses use them to manage their operations. In this section, we are going to learn how to connect to a database from Python, how to read data from the database, and how to add data to a database from Python as well.

What is a Database?

Databases were invented over 50 years ago. Today, there are many different types of databases and companies that provide database products and solutions. But what is a database?

Concept: Databases
Full Concepts List: Alphabetical  or By Chapter 

A database is a collection of information that is stored and organized in a manner that is easily accessed, managed, and updated.

This is a very generic definition of a database. It is important to understand that there are many types of databases, which means it is difficult to provide a single all-encompassing definition. So, rather than attempt to arrive at a detailed definition, we will focus on relational databases which are very common in business applications. Keep in mind though that while we will focus on relational, you may find yourself working with any of the other types in your career.

Relational Databases

Relational databases are called relational because they store data in structures called tables and the data in those tables are (usually) related in some way. For example, in a system that tracks students at a college, that might have a table called students that stores student information (name, address, etc.). We might also have another table called majors that stores majors that each students declare (majors here plural because a student can major in more than one field of study). We could also have a table called registration that tracks what classes each student has registered for each semester. The full system would contain many tables, all of which are related to each other in various ways. Those tables would be related by ID values (called keys) so that we can easily find any student, all of their majors, the classes they have registered for and may other detailed relationships. We can visualize this example using a diagramming method called an Entity Relationship Diagram (ERD) as seen below in Figure 1.



Figure 1: Example ERD

Structured Query Language (SQL)

Relational databases use a programming language called Structured Query Language (SQL) to create queries (questions) that we ask to obtain results from the database, like the example I used above of the customer and their orders. Inside Python (and other programming languagess), we can connect to a relational database, send it a SQL statement (query, update, insert, etc.) and the database will run that SQL statement and return any results to our program. This process is very common in software development for desktop applications, mobile applications, web applications, etc. That process looks like this:



Figure 2: Example of Python connecting to a database


In this example, the grading system data is stored in the grading_system.db SQLite database. In the Python code, the program makes a connection to the database and passes a SELECT statement (a query) to the database asking for all students with a last name of 'Parks'. The database processes the query and returns a result set, that is, a set of all records that match the query specification (last_name = 'Parks'). In this example, there is only one student with that last name so the result set contains one record.

Don't worry about understanding the above code yet, we will cover programmatically connecting to and querying database in detail over the next several sections.

Database Products

So far all of the references above to databases have been generic, but where do these databases come from? A database resides in a database engine (a.k.a. "database server") which is software that usually runs on a hardware server or in a cloud-based service. There are numerous vendors that produce database server software. Here is a short list of common database vendors and their database products. There are many other vendors and databases available, those listed below have versions available for free that you can download and practice with as you learn more about programming.

Vendor Product(s) *
Microsoft
SQL Server

SQL Server is Microsoft's flagship database system very widely used in business. They make a developer edition available for free download. You can find it here--be sure to choose the Developer Edition from that download page. Also, they provide an Integrated Development Environment (IDE) for interactively working with the database server called SQL Server Management Studio (SSMS) which you can download for free here Also, to interact with SQL Server from Python you'll want to download and install the Python SQL Driver as well.




Open Source
MariaDB

MariaDB is an open source database system that is widely used and a good choice for developers and students. You can download MariaDB from here. For an IDE, you can use MySQL Workbench or dbForge Studio. Also, to interact with MariaDB from Python you'll want to download the Python connector as well.



Open Source
PostgreSQL

PostgreSQL is also a popular open source database system that is widely used and well supported. You can download PostgreSQL from here. The most common IDE for PostgreSQL is pgAdmin which you can download for free here.



Open Source
SQLite

SQLite is a database engine that is very commonly used by developers to embed database functionality into their applications. SQLite is not an application in the traditional sense that you download and install, rather you download a set of library files and then use those libraries from within a programming language. You can also use an additional tool called SQLite Studio to interact with a SQLite database using a GUI front end. You can download SQLite from here and SQLite Studio from here. Also, Note: I have placed more detailed documentation on this site for SQLite & SQLite Studio on the Tools Page here.



Oracle
MySQL

MySQL is a very popular database system, particularly for web applications. Most hosting companies include MySQL databases as a component part of the tools they make available when hosting websites on their platforms. You can download MySQL Community Edition (free) from here. Also, I would recommend using MySQL Workbench for working with MySQL datatabases, which you can download from here.




* Note: The links provided above were accurate as of April 2023.

Production Environments

The word environment is often used to refer to the combination of hardware and software working together for a particular purpose. A production environment is a term used to indicate the hardware and software used during the actual operations of a business or system. In production systems, databases are installed and configured on one or more servers on a network or in cloud-based systems. The database engine then usually runs 24/7 so that it is available to processes, users, and administrators at all times. Those systems are usually configured with tight security and access to it is restricted by Database Administrators to keep the data secure.

With the database engine running, applications can then interact with the database engine as needed and configured. Some examples of applications that might interact with the database include an accounting clerk using Microsoft Excel to calculate summaries of sales and generate graphs. Another may be a data analyst using Tableau to generate a management dashboard to display real-time trends based on transaction data in the operational database. A web developer writes Pythonscript code to process web form data entered by users that are added to the database. A Software Developer writes an application in Python used inside the company by the Marketing Department to allow sales agents to enter data about potential clients into the database. The company also maintains a data warehouse that is populated once per night by an automation process that collects daily transaction data from the operational database and loads the analytical data warehouse. Each morning an automated reporting process generates summary reports using Microsoft PowerBI that are automatically emailed to executives for review. As a programmer, you may be involved in the development of applications and processes similar to any of the examples given above, or others. Here is a visual representation of the scenarios described above:



Figure 3: Example Production Environment


Development Environments

In contrast to a production environment, a development environment is a combination of hardware and software used by software developers to write and test applications that they are creating. Development environments are often set up to simulate the production environment where the software developer's application will run once it is complete. We will create a development environment that will combine your computer (laptop or desktop) with a database engine and a SQL editor in which you will work with the database in conjunction with your Python code. All of this will be combined in your computer to create your development environment and with that configuration you will be able to create databases and interact with them, simulate a production environment similar to the diagram above. Development environments look something like these:



Figure 4: Example Development Environment w/MySQL

In this example, the development environment includes the Python programming language, Oracle MySQL database, its Workbench (GUI) tool, the PyCharm IDE, Tableau, Excel and LucidChart.



Figure 5: Example Development Environment w/SQL Server

In this example, the development environment includes the Python programming language, Microsoft SQL Server database, its Management Studio (GUI) tool, the PyCharm IDE, Tableau, Excel and LucidChart.







© 2023 John Gordon
Cascade Street Publishing, LLC