Sling Academy
Home/Python/Data Persistence in Python – Tutorials & Examples

Data Persistence in Python – Tutorials & Examples

This series of tutorials shows you how to interact with databases like SQLite, MongoDB, MySQL, PostgreSQL by using Python modules like sqlite3, pymongo, mysql-connector-python, and psycopg2.

1 Python sqlite3: How to establish/close a connection

2 Python sqlite3: Setting a timeout for a connection

3 Python sqlite3: Using cached statements

4 Python sqlite3.ProgrammingError: parameters are of unsupported type

5 Python sqlite3: Type Conversion (Implicit/Explicit)

6 Python sqlite3 warning: ‘You can only execute one statement at a time’

7 Python sqlite3 error: Numeric value out of range

8 Python sqlite3 error: String or BLOB exceeds size limit

9 Python sqlite3 error: The database path is not found

10 Python sqlite3.OperationError: database is locked

11 Python sqlite3.OperationError: Transaction failed

12 Python sqlite3.IntegrityError: Foreign key check failed

13 Python sqlite3.NotSupportedError: Causes & Solutions

14 Python sqlite3: Understanding transactions and commit/rollback

15 Python sqlite3: Using placeholders in SQL statements

16 Python sqlite3: Insert a new row and get the ID

17 Python sqlite3: How to perform bulk inserts

18 Python sqlite3: Define a table with auto-incrementing primary key

19 Python sqlite3 upsert: Update if exists, else insert

20 Python sqlite3: How to set a connection runtime limit

21 Python sqlite3: execute(), executescript(), and executemany() – Examples

22 Python sqlite3: Understanding create_function() method (with examples)

23 Python sqlite3 create_aggregate() method: Explanation with examples

24 Python sqlite3 create_window_function() – Explanation with examples

25 Python 3.11 – sqlite3 create_collation() method: Explanation with examples

26 Python sqlite3 – iterdump() and backup() methods: Explanation with examples

27 Python sqlite3: Serialize a database into bytes and deserialize it back

28 Python sqlite3: fetchone(), fetchmany(), and fetchall() methods – Examples

29 Python sqlite3: How to get a list of column names from a table

30 Understanding sqlite3.Blob class in Python

31 Python sqlite3: How to register adapters and converters

32 Python sqlite3: How to open a database in read-only mode

33 Python sqlite3: Pagination examples

34 Python sqlite3: How to handle invalid UTF-8 encoding

35 Python ModuleNotFoundError: No module named ‘_sqlite3’

36 Python: Converting an SQLite database to JSON and vice versa

37 Python: Converting an SQLite database to CSV and vice versa

38 Python sqlite3: CRUD examples

39 Python sqlite3: How to remove duplicates from a table

40 Python sqlite3 aggregation: SUM, AVG, COUNT, MIN, and MAX

41 Python sqlite3: Selecting/Counting Distinct Rows

42 Python sqlite3: Choosing the right date time format

43 Python sqlite3: Working with multiple databases

44 PyMongo: How to establish/close a connection

45 PyMongo: How to use a connection pool

46 PyMongo: How to List All Databases

47 PyMongo: How to list all collections in a database

48 PyMongo: How to insert a document and get the ID

49 PyMongo bulk insert: How to insert a list of documents/dicts

50 PyMongo: How to get the latest/earliest document

51 PyMongo: Using find_one() and find() methods

52 PyMongo: How to count documents based on a condition

53 PyMongo: How to sort documents based on a field (ASC, DESC)

54 PyMongo: Sorting documents by multiple fields

55 PyMongo: How to implement pagination

56 PyMongo: How to select documents within a range

57 Indexing in PyMongo: A Practical Guide

58 PyMongo: How to execute raw database commands

59 Python: Using sqlite3 with Type Hints

60 Python: Using PyMongo with Type Hints

61 Python sqlite3: Write a program to benchmark query execution time

62 PyMongo: How to update and delete documents

63 How to translate MongoDB shell syntax to PyMongo code

64 PyMongo: How to use the aggregation pipeline (sum, avg, count, min, max)

65 PyMongo: How to select/count distinct documents

66 PyMongo: Updating specific array elements with array filters

67 PyMongo: Query and update documents based on nested fields

68 PyMongo: How to Define and Use Custom Types

69 PyMongo: How to save datetime with timezone

70 PyMongo: Grouping documents by day/month/year

71 PyMongo: Saving and Querying Location Data (Latitude, Longitude)

72 PyMongo: How to insert and update JSON data

73 Solving PyMongo Error: Couldn’t connect to server 127.0.0.1:27017

74 PyMongo: How to query documents with regex (regular expressions)

75 PyMongo: How to simulate ‘LIKE’ and ‘ILIKE’ in SQL

76 PyMongo: how to search documents by ObjectId

77 PyMongo: How to find documents by a list of values

78 PyMongo ValueError – Expecting property name: line 1 column 2 (char 1)

79 PyMongo: How to convert ObjectId to string and vice versa

80 PyMongo CursorNotFound error: Cursor not found/valid at server

81 PyMongo: Why you need to close a connection?

82 PyMongo error – Upsert failed: E11000 duplicate key error collection

83 PyMongo error: Upsert must be an instance of dict, not str

84 PyMongo Upsert Examples: Update if exists, else insert

85 PyMongo error: Collection object is not callable

86 PyMongo: Find documents with null/empty fields

87 PyMongo: Convert query results to a list of dictionaries

88 PyMongo: How to create/drop indexes

89 PyMongo: How to create/drop databases

90 PyMongo: Find documents within a day/week/month/year

91 PyMongo: Find results within a specific area (geospatial queries)

92 PyMongo ValueError: update only works with $ operators

93 PyMongo: How to get the length of a cursor

94 PyMongo: How to connect to a remote MongoDB server

95 PyMongo Auth with username and password (examples)

96 PyMongo: How to save and query ISODate

97 PyMongo: How to exclude _id field from query results

98 Solving Python ImportError: No module named ‘pymongo’

99 PyMongo: How to check if a collection exists

100 PyMongo: Removing specific fields from documents

101 PyMongo: How to append values to an array field

102 PyMongo error: Date time ‘…’ is not JSON serializable

103 PyMongo: How to set a timeout for a connection

104 PyMongo ImportError: Cannot import name BSON

105 PyMongo: How to perform case-insensitive text search

106 PyMongo ObjectId generation_time examples

107 PyMongo error: Exceeded memory limit for $group, but didn’t allow external sort.

108 PyMongo: How to create compound indexes

109 PyMongo bson.errors.InvalidDocument: Cannot encode object

110 PyMongo: Using $and, $or, $not, $nor operators (6 examples)

111 How to check your PyMongo version and upgrade it

112 Fixing BulkWriteError: batch op errors occurred

113 MongoEngine: How to connect to a database

114 MongoEngine: How to use multiple databases

115 MongoEngine: How to define document’s schema

116 MongoEngine: Working with ListField

117 MongoEngine – Working with EnumField

118 MongoEngine – Using ImageField and FileField

119 MongoEngine Indexing: A Practical Guide

120 MongoEngine Document Validation: The Complete Guide

121 MongoEngine: Sorting Documents – Tutorial & Examples

122 MongoEngine: CRUD Operations – Tutorial & Examples

123 MongoEngine: How to Perform Text Search

124 MongoEngine Pagination – Tutorial & Examples

125 MongoEngine: How to ignore a field when validating documents

126 MongoEngine: How to exclude fields from query results

127 MongoEngine: How to insert one or multiple documents

128 MongoEngine: How to update a document by ID

129 MongoEngine: How to delete documents by a condition

130 MongoEngine Upsert: Insert if not exists, update if exists

131 MongoEngine: AND & OR Conditions – Tutorial & Examples

132 MongoEngine: Filter Documents by Multiple Fields

133 MongoEngine: How to update embedded documents

134 MongoEngine: How to convert a document to JSON

135 Using Regular Expressions in MongoEngine

136 MongoEngine ConnectionError: You have not defined a default connection

137 MongoEngine: Fetch the latest/earliest document

138 MongoEngine: How to create/drop a collection

139 MongoEngine: How to close a connection

140 MongoEngine Aggregation: A Practical Guide

141 MongoEngine: How to query distinct values

142 MongoEngine: Set a default value for a field

143 MongoEngine: Required and Optional Fields

144 MongoEngine: Set max length for a text field

145 MongoEngine: Set min/max for a number field

146 MongoEngine: Unique and Non-Unique Fields

147 MongoEngine: How to see the generated query

148 MongoEngine ReferenceField – Tutorial & Examples

149 MongoEngine: How to append item to a ListField

150 MongoEngine DateTimeField – Tutorial & Examples

151 MongoEngine BinaryField – Tutorial & Examples

152 MongoEngine DictField: A Practical Guide (with examples)