pymysql module fetch the result(sof the sql using the cursor object ( fetchallfetchmany or fetchone close the database connection these steps are essentially boiler platecode that is you will use them whenever you access database via pymysql (or indeed any db-api compliant modulewe will take each of these steps in turn importing the module as the pymysql module is not one of the built-in modules provided by default with python you will need to import the module into your codefor example using import pymsql be careful with the case used here as the module name is pymysql in the code (if you try to import pymysql python will not find it!connect to the database each database module will have their own specifics for connecting to the database serverthese usually involve specifying the machine that the database is running on (as databases can be quiet resource intensivethey are often run on separate physical computer)the user to use for the connection and any security information required such as password and the database instance to connect to in most cases database is looked after by database management system ( dbmsthat can manage multiple database instances and it is therefore necessary to specify which database instance you are interested in for mysqlthe mysql database server is dbms that can indeed look after multiple database instances the pymysql connect function thus requires the following information when connecting to the database isthe name of the machine hosting the mysql database server dbserver mydomain com if you want to connect to the same machine as your python program is running onthen you can use localhost this is special name reserved for the local machine and avoids you needing to worry about the name of your local computer the user name to use for the connection most databases limit access to their databases to named users these are not necessary users such as humans that log into system but rather entities that are allowed to connect to the database and perform certain operations for exampleone user may only be able to read data in the database where as another user is allowed to insert new data into the