☰
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 2 : Data (Input) : Databases

Databases

Subscribe Contact


Overview

Databases are ubiquitous; that is, 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. Databases are used in almost every business that exists today. The list goes on and on about where you will find databases. As a professional today it is critical 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 module, we will 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. These skills are valuable for professionals in the Information Systems industry.

What is a Database?

Databases were invented over 50 years ago. Today, many different companies produce database products and solutions. But what is a database?

Concept: Database
Full Concepts List: Alphabetical  or By Chapter 

In programming, a database is a systematic collection of data organized and stored to facilitate efficient access, retrieval, management, and updating of information. Databases are essential for handling large amounts of data, providing a structured framework to store data in tables, records, and fields. This structure allows for easy querying and manipulation of the data using specialized database management systems (DBMS), such as SQL (Structured Query Language), used to interact with relational databases. There are various types of databases, including relational databases, which organize data into interrelated tables, and NoSQL databases, which are designed for unstructured data and offer more flexibility regarding data models (like document, key-value, graph, or wide-column stores). Databases are fundamental in a wide range of applications, from simple websites to complex enterprise systems, as they enable efficient data handling, consistency, security, and scalability. They are integral to data analysis, customer relationship management, inventory tracking, and beyond, serving as the backbone for data-driven decision-making and operations.

This is a very generic definition of a database. It is essential to understand that there are many types of databases, it is difficult to provide a single all-encompassing definition. So, rather than arrive at a detailed definition, we will focus on relational databases, which are very common in business applications. Remember 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 store data in structures called tables, and the data in those tables are (usually) related in some way. For example, a system that tracks students at a college might have a table called students that stores student information (name, address, etc.). We might also have another table called majors that holds majors that each student declares (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 entire system would contain many tables, all 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, their majors, the classes they have registered for, and other detailed relationships. We can visualize this example in a diagramming method called an Entity Relationship Diagram (ERD like this:

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 languages), 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 widespread in software development for desktop applications, mobile applications, web applications, etc. That process looks like this:


In this example, the grading system data is stored in the grading_system.db SQLite database. In the Python code, the program connects 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, a set of all records that match the query specification (last_name = 'Parks'). In this example, 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 databases in detail throughout the book.

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. Numerous vendors 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 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 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, 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 developers commonly use 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.



Oracle
MySQL

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



* Note: The links provided above were accurate as of January 2024.

Production Environments

The word environment is 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 always available to processes, users, and administrators. Those systems are typically configured with tight security and access to them 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 sales summaries and generate graphs. Another may be a data analyst using Tableau to create a management dashboard to display real-time trends based on transaction data in the operational database. A web developer writes Python script 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 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 which are automatically emailed to executives for review. As a programmer, you may be involved in developmenting applications and processes similar to these examples, or others. Here is a visual representation of the scenarios described above:

Development Environments

In contrast to a production environment, a development environment is a combination of hardware and software software developers use to write and test applications they create. 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 an SQL editor to work with the database in conjunction with your Python code. All of this is 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:


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.


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