Python Dbm ModuleWe all work with databases in our daily life, knowingly or unknowingly. But it remains the fact that database is an integral part of the internet and every service provided on the internet. Databases are systems or software where data from the internet or service provided is stored. Database stores data in the forms of rows & columns, usually a collection of many tables, allowing us to store useful information for a very long time. Databases are the key component while working on any software as they provide us the options to store the data we have in an organized manner. According to the official definitions of a database in computer science, a database is an organized, well-maintained, and well-structured collection of data stored which can be accessed electronically. We cannot even imagine that the internet can be possible without the existence of a database. It is possible that many of us haven’t worked on the database yet knowingly, but unknowingly, there would be many instances when we would have worked on databases. Databases are of different kinds, with each type of database being designed with some specific set of features that are embedded to perform some specific set of tasks. Therefore, different types of databases are used in different fields & organizations according to their features. Talking about computer science, a database is the necessity of every project we do or work on. Databases are used by almost every developer and programmer where they used to store their data in arranged format. We may have to work on different types of databases while working on a single project as per the project’s requirement. We can use multiple methods to work or access databases while working on the current development project. One of the most popular ways is to use a package or module in the program to directly access the database present in the system through the program or code on which we are currently working. But accessing the database through this method requires a lot of space in the system and slows down the system’s running speed. Furthermore, not all programming languages provide libraries or functions that we can use to access the database through a program. Therefore, it was necessary to build packages that can be an alternative to the database/s we require while working on a code. In today’s time, many programming languages offer us packages & libraries which act like database managers and work as a database to store the data of our programs. While working with such packages or modules, we don’t have to provide links for connecting to any external database dependencies, which is the biggest advantage of such packages. Therefore, such packages of any programming languages that act as a database manager and provide the database environment in the program so that we can store all the data there are becoming very much popular among developers. Most of the developers in today’s time prefer to work with these packages instead of working with the packages that provide a connection with the actual database. While working with database manager packages, we don’t even need to have any actual database software installed in our system. We can directly work with these packages and store the data in these database managers by creating a database in the system and working on it. Such type of packages is offered by many popular programming languages, including Python. Talking specially about Python, it also has a built-in database manager package that we can use in our Python programs to work with databases. The dbm module is the Python package that acts as a database manager, and we can use this to work on the data provided inside a Python program. Therefore, we will learn more about this dbm module and its functions in this tutorial. Introduction to Dbm Module of PythonThe dbm module, a Python package, is a database library of Python that acts as a database manager (DBM) in the program when we work with this module. The dbm module of Python provides us the simple dictionary interface (Which we usually use in Python programs while working with data values) in the DBM form, and this module is generally used in the UNIX operating system. Even though this module is very common on UNIX operating systems, we can also use this module on other operating systems such as Windows and others. Like the basic functionality of any database, the dbm module also stores data in the key-value pair format, which we commonly use under the dictionary data types in Python. This key-value dictionary format of the dbm module makes it even easier to retrieve the data from the database. The data stored in the dbm module is stored by using a single primary (“key) in fixed-size blocks, which makes it even easier to work with this database manager package. Sub-modules of the Dbm Package:The Dbm Package has many sub-modules built in it that are used to perform different functions in a database, and we can use all these sub-modules by simply importing the dbm module in the program. We will here provide a brief description of these three sub-modules of the dbm package to understand them. Following are the three types of sub-modules present in the dbm package for handling the databases: (1) dbm.dumb: This is one of the three sub-modules of the dbm package, and we can use this in the portable DBM implementation operations. (2) dbm.ndbm: This is one of the three sub-modules of the dbm package, and we use this sub-module in the operations that require an interface based on ndbm. This sub-module is used to provide the ndbm based interface while working on the databases in a Python program. (3) dbm.gnu: The dbm.gnu is also one of the dbm package sub-modules, and it is used for the operations that involve GNU’s reinterpretation of dbm. This is a brief description of the sub-modules of the dbm package, and we can use these sub-modules in a Python program according to the operations we want to perform on the databases using a Python program. Dbm Module of Python: AdvantagesThe dbm module of Python is a popular choice over the common database packages that are used to link the database installed in our system to the Python program we are working on. The dbm module offers multiple advantages over the normal Python database packages commonly used in Python projects. Before we start working with the dbm package, we will first learn about its advantages. Following are the advantages offered by the dbm package when we use it in our programs: (1) Better performance:When we use the dbm package with our Python programs instead of other database packages, we can clearly observe the performance difference. It is much easier to execute the programs and perform operations on the databases using the dbm module as this is a database manager package itself. If we use other database packages, we have to install database software in our system to work with these packages. But this is not in the case of the dbm package, thus making the operations performing using this module faster and smoother. The performance improves while working with the dbm package as this package offers a database environment, and we can carry out database operations without actually installing database software in our system. The speed of execution of programs and performing database operations with the dbm module is faster than the other database packages. (2) Easy to retrieve data:The data stored in the database created using the dbm module is stored in the key-value pair format, making it much easier to retrieve the data from the database. Thus, it also makes it easier to access the information and data present in the databases when we are working on other databases. This saves some of the time we spent on retrieving data and makes the execution of the database operations faster. (3) Operability on multiple operating systems:It is a known fact that the dbm module is the most popular choice on the UNIX operating system, but that doesn’t mean this module is not supported on other operating systems. The dbm module can be used on different types of modules like Windows, UNIX, LINUX, and many others. Therefore, if we even think to switch to other operating systems in our system, we can still access and work on databases using this module. The operability feature of this module saves our time which we usually spend on looking for different database modules for different operating systems. Apart from the main advantages of the dbm module as listed above, this module has several other benefits too, and the following is the list of other advantages of using this module: (4) Provide a simple interface format like dictionary data types which makes it easier to work this module, (5) It is a database management module, so we don’t need to install any dependencies to work with this package, These are advantages of the dbm module, and that’s why the dbm module is a popular choice over the common Python database packages to carry out database operations using a Python program. Dbm Module of Python: InstallationThe dbm module of Python is one of the in-built modules of Python, which means that we can directly import this module in our example programs of implementation part of this tutorial. This means that we can directly start working with the dbm module without spending any extra time on performing the installation process of this module. To verify whether the dbm module is in-built present in our system or not, we can use the following pip command installation line in the command prompt shell of our system: When we try to install the dbm module using the installation line given above, it will show ‘no matching distributions are found’, which means that this module is already present in our system and an in-built Python module. We can directly start working with the dbm module & use functions of this module in the example programs by importing the dbm module using the following code line in the program: When we use this line of code in our example programs, we will be able to use this data manager module and use its functions to create a database through the program. The database operations will be performed using this module when the program is executed. Now, we can proceed with the implementation part of this module, where we will learn how to use the functions of this module to carry out database-related operations. Dbm Module of Python: FunctionsBefore we proceed with the implementation part of the dbm module, we first go through all the major functions. This will make it easy for us to use these functions in the example programs of the implementation part and understand their working in the program. Thus, we will know which functions we should be used for carrying out a particular database operation and which function is carrying out which database operation. To have proper knowledge about the functions of this module, we can also get an idea of what kind of functions are used to get a particular result from the database. Now, we will learn all the major functions of the dbm module so that it will later become easy for us to use them and understand their work. Following is the description of the major functions of the dbm module: (1) dbm.open():The open() function of the dbm module is used to carry out popularly the following two tasks: (i) Opening a database file present in the system (ii) Creating a new database in the program through using this open() function The open() function that’s why became the most popular function among all the functions of the dbm module. Apart from the two tasks mentioned above, the open() function also acts as a starting point in the program where all the database-related operations can be performed. When we use this open() function in the program with writing a database name in the program, it will open the database if present in the system; otherwise, it will create a new database with the same name as we give in the argument. Syntax for the open() function: We can use the dbm.open() function in the example program by providing the name of the database inside the function as an argument so that function will either open or create a database with the same name in the program. Following is the syntax for using the open() function of the dbm module in a Python program: Parameters: As we can see, the above given open() functions take multiple arguments in which some are mandatory arguments, and others are optional. Following is the description of parameters that can be given in this open() function: (i) fileName: This argument is the mandatory argument of the function, which is used to open and create a database in the function. We have to provide the name of the file which we want to open in the program. If the file with the same name (as we have provided in the argument) is present in the system’s directory, the function will open it in the program. Otherwise, the function will create a database file with the given name in the system’s directory and then open the file in the program. (ii) flag: This argument is not a mandatory argument for the program, but it is always recommended that we use it to be more specific about the database operations we want to perform. We can use the flag argument to specify the purpose related to the database we are opening in the program. We can use this argument to become more specific about what kind of operations we will perform on the database we are opening. The flag takes ‘r’, ‘w’, ‘c’, or ‘n’ as an argument, but it takes only one of these arguments at a time. Following is the description of the arguments that can be given inside the flag parameter of the open() function:
This is the description of the arguments given inside the ‘flag’ parameter of the open() function. (iii) mode: The mode argument is used when we use the dbm module in the UNIX operating system, and this argument is set to the octal form with a default value of 0o666. This is an optional argument for the open() function, and we can skip this argument while using the dbm module on windows. These all are the parameters taken by the open() function of the dbm module. (2) dbm.whichdb():We can use the whichdb() function of the dbm module while opening a database file in the program, and this function will attempt to guess that the file should be opened with which databases modules from the available modules of the dbm package (dbm.gnu, dbm.ndbm, or dbm.dumb). We have to provide the file name of the database file we are going to work in that program, and this function will guess the database modules that will be used to open this file. Syntax for the whichdb() function:We can use the dbm.whichdb() function in the example program by providing the name of the database file inside the function as an argument so that function will guess which database module should be used to open the given file. Following is the syntax for using the whichdb() function of the dbm module in a Python program: Parameters: As we can see, the above-given whichdb() functions take only one argument, which is the name of the file we are opening in the program. Return type: Following are some common return types of this whichdb() function: (i) Module name: The required module name will be returned for the file, which should be used to open the file whose name has been given as an argument inside the open() and which() function. (ii) Note: If the database file whose name is given inside the argument of the whichdb() function is not present in the system’s directory, it will return none in the output. (iii) An empty string: This is the return in the special case when the given file is present in the system, but whose required module name cannot be guessed by the function. This is the complete description of two major functions of the dbm module, which we will be using in the example program of this tutorial. Apart from these two functions, there are many other functions present in the dbm module used to perform multiple database operations on the database file we are opening in the program. Following is the brief overview of all other important functions or methods present in the dbm module: (1) Close(): This function doesn’t take any argument from the user nor make any changes in the database file opened in the program. This function is used only to close the database file we have opened in the program. (2) update(): We can use the update() function as a database method to update the existing key-value, just like we perform the same operation in the dictionary object. (3) setdefault(): We can use this function to define a default primary key which we have provided as an argument inside this function. (4) get(key): This function takes keys as the argument inside and returns the corresponding value associated with the key in the database file we have opened into the program. (5) value(): This method is used by us to iterate through all the values present in the database file opened. (6) items(): We can use the items() function as a method to iterate over all the items present in the database file we have opened. (7) keys: This method is used by us to iterate through all the keys present in the database file opened. (8) sync(): We can use the sync() function on the opened database file in the program to synchronize data files and the on-disk directory. (9) pop(key): This function is used by us to delete the key we have provided as an argument inside this function, and the value associated with the key given will also be deleted from the database file. These are the description of all the other important functions given in the dbm module, which we can use in a Python program to carry out multiple database operations on a database file. Dbm Module of Python: ImplementationWe have learned all the major functions of the dbm module and their working inside a Python program. Till now, we have learned which dbm module should be used to carry out a particular operation on the database file. In this part, we will learn how these functions work inside a Python program and understand the implementation of the dbm module. We will use the functions that we have learned until now in a database file that we will create inside the program and perform multiple database operations to understand the implementation of the dbm module. Look at the following example program where we have created a database file and performed multiple database operations using the dbm module functions: Output: The name of the developer we have defined inside the database file: b'Python Developer from JavaTpoint' The phone number of the developer we have defined inside the database file: b'98765432' The short name of the developer we have defined inside the database file: b'Python JTP' The first date for learning of the developer we have defined inside the database file: b'26/04/2013' Following data is present in the database file which we have got through the value iteration method: b'Python Developer from JavaTpoint' b'98765432' b'Python JTP' b'26/04/2013' Following data is present in the database file which we have got through the key iteration method: b'Name' b'Mobile' b'Short name' b'Date of first learning' Following data is present in the database file which we have got through the item iteration method: b'Name' b'Python Developer from JavaTpoint' b'Short name' b'Python JTP' b'Date of first learning' b'26/04/2013' As we can see, we have created a database file in the program and performed multiple operations on the file after adding data inside the file. That’s how we can use the dbm module to carry out multiple database operations on a database file using a Python program. |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/courses/263229.html