What is a CRUD operation?
CRUD is an acronym that stands for Create, Read, Update, and Delete. It is a set of four basic operations that are typically used for managing data in a persistent storage system such as a database. The CRUD operations can be summarized as follows:
Create: This operation involves creating new data and storing it in a persistent storage system. It typically involves adding a new record or row to a database table, with the data provided by the user.
Read: This operation involves retrieving existing data from a persistent storage system. It typically involves querying a database table to retrieve one or more records or rows, based on some criteria such as a primary key or a search term.
Update: This operation involves modifying existing data in a persistent storage system. It typically involves updating one or more fields or columns of an existing record or row in a database table.
Delete: This operation involves removing existing data from a persistent storage system. It typically involves deleting one or more records or rows from a database table, based on some criteria such as a primary key or a search term.
Implementing CRUD using Python Dictionary
In this example, the dictionary data
Dict is used to store key-value pairs. The createDict()
function takes a key
and value
argument, and adds the key-value pair to the dictionary. The readDict()
function takes a key
argument and returns the corresponding value from the dictionary. The updateDict()
function takes a key
and value
argument, and updates the value for the corresponding key in the dictionary. The deleteDict()
function takes a key
argument and removes the corresponding key-value pair from the dictionary.
# Initialize an empty dictionary
dataDict = {}
# Create operation
def createDict(key, value):
dataDict[key] = value
print(f"Added key: {key} with value: {value}")
# Read operation
def readDict(key):
if key in data:
return dataDict[key]
else:
print(f"Key: {key} not found")
# Update operation
def updateDict(key, value):
if key in data:
dataDict[key] = value
print(f"Updated key: {key} with value: {value}")
else:
print(f"Key: {key} not found")
# Delete operation
def deleteDict(key):
if key in data:
del dataDict[key]
print(f"Deleted key: {key}")
else:
print(f"Key: {key} not found")
# Example usage
createDict("name", "John")
print(read("name"))
updateDict("name", "Jane")
print(read("name"))
deleteDict("name")
In case you want to explore a bit more interesting code where nested dictionary CRUD operation is used, have a look at below code:
# Consider the below CRUD queries
crudQueries = [
["INS_OR_UPD", "KEY1", "KEY2", "5"],
["INS_OR_UPD", "KEY1", "KEY2", "6"],
["INS_OR_UPD", "KEY3", "KEY2", "7"],
["SELECT", "KEY1", "KEY2"],
["SELECT", "KEY1", "KEY3"],
["DELETE", "KEY1", "KEY2"],
["DELETE", "KEY1", "KEY2"]
];
# Create a nested dictionary and Implement CRUD operation in Python
dataDict = {} # initialize a dictionary
outputList = [] # initialize a list
def myFunction():
for query in crudQueries:
if(query[0]=='INS_OR_UPD'):
tempDict = {query[2]:query[3]} # nested dictionary
dataDict[query[1]] = tempDict
outputList.append(query[3])
if(query[0]=='SELECT'):
outputList.append(query[2])
if(query[0]=='DELETE'):
value = dataDict.pop(query[1], None) # if key does not exist then also no error
outputList.append(value)
return outputList,dataDict
# Call the function
outputList,dataDict = myFunction()
print(outputList,dataDict)
Conclusion:
CRUD operations are fundamental to most applications that involve data management and are a crucial part of software development. They allow users to create, read, update, and delete data, providing a way to manage data in a consistent and efficient manner.