Loading lesson path
Python
Connect Python to SQL and document databases for real-world application work.
Python MySQL
Python MongoDB
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…
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.…
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…
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…
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",…
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…
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…
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…
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…
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…
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…
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…
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…
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…
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…
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…
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…
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…
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…
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…
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…