Professional Documents
Culture Documents
Delete Operation On Sqllite and Ms Access Database
Delete Operation On Sqllite and Ms Access Database
Delete Operation On Sqllite and Ms Access Database
• In order to delete data in the SQLite database from a Python program, we use the following
steps:
• First, establish a connection with the SQLite database by creating a Connection object using the
connect() function.
• Second, to execute a DELETE statement, you need to create a Cursor object using the cursor()
method of the Connection object.
• Third, execute the DELETE statement using the execute() method of the Cursor object. In case
you want to pass the arguments to the statement, you use a question mark ( ?) for each argument.
• The following create_connection() function establishes a database connection to an SQLite
database specified by a database file name:
CODE
• def del_report():
• conn = sql.Connection(DATABASE)
• cursor = conn.cursor()
•
• with conn:
• cursor.execute('DELETE FROM student_records WHERE
student_id = "T333" ')
• print('deleted')
•
MS-ACCESS DATABASE
• The first thing to do is to import the pyodbc (a python module that makes
accessing the ODBC databases simple. ODBC simply means Open Database
Connectivity.
• If you haven’t installed the pyodbc module, use pip install pyodbc
• The second thing is checking for the available drivers.
• The above code is to check for available drivers, and if you run the above code, you will the
following output:
• As you can see we have our driver now, and the only part we need is: MS-Access Drivers :
['Microsoft Access Driver (*.mdb, *.accdb)'
• Now we need to create connection between MS-Access and python.
• The code to create connection is as follows:
If you run the above code and there are no errors, a connection message is printed
(Connected to database)
The code for connecting to the database specifies the driver name, database name and the path
/ location of the database.
Now that we are connected to the database, it’s time to create the cursor object. This is done by
invoking the cursor() method on the connection object created.
After that, we execute the delete operation as follows:
If there are no errors, the (deletion successful) message is printed.
However you are not limited to this way only there are so many other ways to deal with
delete operation in MS-Access using python.
The above database is the database with all entries