☰
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.2 : SQLite Databases

SQLite Databases
Subscribe Contact


Contents

Overview

For the purposes of learning to connect to and interact with a database from within Python, we will use SQLite because installation of SQLite and working with it is fairly easy. However, the concepts we cover here will work with all relational database systems with only minor changes to the code. So, on this page, we'll get SQLite and SQLite Studio installed and we'll also set up a sample database to work with over the next several sections.

What is SQLite?

SQLite is a file-based database tool. In this context, file-based means that databases are stored in a file (with a .db file extension). This fact makes SQLite databases very portable and easy to manage. We also should recognize that SQLite is not a "server," like SQL Server, MySQL, etc. This means that I can use SQLite on nearly any kind of computer and many "smart" devices such as phones, tablets, etc. This makes SQLite very flexible. On the other hand, SQLite does have limitations for large applications where multiple users need to connect to the database simultaneously, large quantities of data, etc. These limitations will not affect what we're studying here and as mentioned above, the concepts of connecting to a database, querying a database, updating data in a database, etc. are conceptually the same regardless of the database product in use.

Installing SQLite & SQLite Studio

Choose your operating system for detailed installation instructions.



Sample Database

Be sure you have SQLite and SQLite Studio installed on your computer before proceeding.

Also, I will use the following configuration on my computer where I have SQLite and SQLite Studio software installed, as well as where I will store additional files as explained below. You can set up your computer any way you wish, you'll need to know the location of these items on your computer.



Next, open SQLite Studio and click the Add a Database button as shown here:

The Add Database dialog will open:

Click the green + button and the File Explorer will open in which you'll navigate to the location where you want the new database file to be created. In my example, I will navigate to my C:\SQLite\Data folder where I want to store all of my database files. After I have navigated to that folder I enter the name of the database in the File name: field of the dialog. In this case we want to name our database grading_system.

When we click the Save button and then Ok, we see that the new database is now listed in SQLite Studio:

At this point though the grading_system database is empty. There is no structure to the database and no data in it, yet. There are several options to load create the database structure and to load data. For our purposes, to make it easier to get started on Python coding, we'll use a script I wrote to populate this database automatically.

The script is a program written in Structured Query Language (SQL) that resets the grading_system database, that is, removes everything from it and rebuilds it to an original state. You can run this script as often as you like to rebuild the database. This is useful as you experiment with the database, you can modify and delete data all you want to try different SQL features. Then you can re-run this script to reset the database back to its original state any time.

I have packaged the script file, along with another file that contains sample queries you can use with the grading_system database, in a ZIP file you can download. Download the ZIP file using the download icon ...

In my example, I will download the ZIP and then move the downloaded file from my Downloads folder to me my C:\SQLite\Scripts folder:

I then extract the ZIP file and now have two .sql files:


Next, inside of SQLite Studio, open the BuildGradingSystemDB.sql file by clicking the Open SQL Editor button:

Then click the Load SQL from File button:

In the File Open dialog navigate to the location where you stored the script files, in my example that is the C:\SQLite\Scripts folder. Select the BuildGradingSystemDB.sql file and then click the Open button:

The script file will open in the editor and look like this:

To run the script, first double-click the grading_system database name to select it. Then, select all of the text (Windows & Linux: Ctrl + A, Macintosh: Cmd + A) in the editor window and then click the Run button:

After the script runs we now see that there are tables in the database (you can see these by clicking the > arrow next to Tables under the database name):

Next, let's try a query to confirm that we also have data in the database. To do this, we'll open the DemoQueries.sql script file. To do this, click the Load SQL from file button again and this time select the DemoQueries.sql file and click the Open button:

The script will open. If you look through the file there are numerous, separate, segments of SQL code, each starting with SELECT. Each of those are individual queries. To run one, highlight a query (which starts with SELECT and ends with a semi-colon ; character), and then click the Run button.

For example, in the following screen capture I highlighted the SELECT * FROM students; query and clicked the Run button:

The results of the query is called our result set. Getting a result set here indicates that our build script worked and we have data in the database.

For now, that completes the set up of SQLite and the sample database. We will continue to use the grading_system database in the sections that follow.







© 2023 John Gordon
Cascade Street Publishing, LLC