bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/Python

Python

Databases in Python

Connect Python to SQL and document databases for real-world application work.

Lesson 1

Python MySQL

Python MySQL

2 min
Read lesson →
Lesson 2

Python MongoDB

Python MongoDB

2 min
Read lesson →
Lesson 3

Python MySQL Create Database

Creating a Database To create a database in MySQL, use the "CREATE DATABASE" statement: Example create a database named "mydatabase": import mysql.connector mydb = mysql.connector.connect( host="loca…

2 min
Read lesson →
Lesson 4visual

Python MongoDB Create Database

Creating a Database To create a database in MongoDB, start by creating a MongoClient object, then specify a connection URL with the correct ip address and the name of the database you want to create.…

2 min
Read lesson →
Lesson 5visual

Python MySQL Create Table

Creating a Table To create a table in MySQL, use the "CREATE TABLE" statement. Make sure you define the name of the database when you create the connection Example Create a table named "customers": i…

2 min
Read lesson →
Lesson 6

Python MongoDB Create Collection

A collection in MongoDB is the same as a table in SQL databases. Creating a Collection To create a collection in MongoDB, use database object and specify the name of the collection you want to create…

2 min
Read lesson →
Lesson 7visual

Python MySQL Insert Into Table

Insert Into Table To fill a table in MySQL, use the "INSERT INTO" statement. Example Insert a record in the "customers" table: import mysql.connector mydb = mysql.connector.connect( host="localhost",…

2 min
Read lesson →
Lesson 8visual

Python MongoDB Insert Document

A document in MongoDB is the same as a record in SQL databases. Insert Into Collection To insert a record, or document as it is called in MongoDB, into a collection, we use the insert_one() method. T…

3 min
Read lesson →
Lesson 9visual

Python MySQL Select From

Select From a Table To select from a table in MySQL, use the "SELECT" statement: Example Select all records from the "customers" table, and display the result: import mysql.connector mydb = mysql.con…

2 min
Read lesson →
Lesson 10visual

Python MongoDB Find

In MongoDB we use the find() and find_one() methods to find data in a collection. Just like the SELECT statement is used to find data in a table in a MySQL database. Find One To select data from a co…

2 min
Read lesson →
Lesson 11visual

Python MySQL Where

Select With a Filter When selecting records from a table, you can filter the selection by using the "WHERE" statement: Example Select record(s) where the address is "Park Lane 38": result: import mys…

2 min
Read lesson →
Lesson 12visual

Python MongoDB Query

Filter the Result When finding documents in a collection, you can filter the result by using a query object. The first argument of the find() method is a query object, and is used to limit the search…

2 min
Read lesson →
Lesson 13

Python MySQL Order By

Sort the Result Use the ORDER BY statement to sort the result in ascending or descending order. The ORDER BY keyword sorts the result ascending by default. To sort the result in descending order, use…

2 min
Read lesson →
Lesson 14

Python MongoDB Sort

Sort the Result Use the sort() method to sort the result in ascending or descending order. The sort() method takes one parameter for "fieldname" and one parameter for "direction" (ascending is the de…

2 min
Read lesson →
Lesson 15visual

Python MySQL Delete From By

Delete Record You can delete records from an existing table by using the "DELETE FROM" statement: Example Delete any record where the address is "Mountain 21": import mysql.connector mydb = mysql.con…

2 min
Read lesson →
Lesson 16visual

Python MongoDB Delete Document

Delete Document To delete one document, we use the delete_one() method. The first parameter of the delete_one() method is a query object defining which document to delete. Note: If the query finds mo…

2 min
Read lesson →
Lesson 17

Python MySQL Drop Table

Delete a Table You can delete an existing table by using the "DROP TABLE" statement: Example Delete the table "customers": import mysql.connector mydb = mysql.connector.connect( host="localhost", use…

2 min
Read lesson →
Lesson 18

Python MongoDB Drop Collection

Delete Collection You can delete a table, or collection as it is called in MongoDB, by using the drop() method. Example Delete the "customers" collection: import pymongo myclient = pymongo.MongoClien…

2 min
Read lesson →
Lesson 19visual

Python MySQL Update Table

Update Table You can update existing records in a table by using the "UPDATE" statement: Example Overwrite the address column from "Valley 345" to "Canyon 123": import mysql.connector mydb = mysql.co…

2 min
Read lesson →
Lesson 20visual

Python MongoDB Update

Update Collection You can update a record, or document as it is called in MongoDB, by using the update_one() method. The first parameter of the update_one() method is a query object defining which do…

2 min
Read lesson →
Lesson 21

Python MySQL Limit

Limit the Result You can limit the number of records returned from the query, by using the "LIMIT" statement: Example Select the 5 first records in the "customers" table: import mysql.connector mydb…

2 min
Read lesson →
Lesson 22visual

Python MongoDB Limit

Limit the Result To limit the result in MongoDB, we use the limit() method. The limit() method takes one parameter, a number defining how many documents to return. Consider you have a "customers" col…

2 min
Read lesson →
Lesson 23

Python MySQL Join

Join Two or More Tables You can combine rows from two or more tables, based on a related column between them, by using a JOIN statement. Consider you have a "users" table and a "products" table: user…

2 min
Read lesson →