Mohd Mohtashim has Published 251 Articles

Database DELETE Operation in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 31-Jan-2020 10:00:42

206 Views

DELETE operation is required when you want to delete some records from your database. Following is the procedure to delete all the records from EMPLOYEE where AGE is more than 20 −Example#!/usr/bin/python import MySQLdb # Open database connection db = MySQLdb.connect("localhost", "testuser", "test123", "TESTDB" ) # prepare a cursor object ... Read More

Database Update Operation in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 31-Jan-2020 09:59:37

376 Views

UPDATE Operation on any database means to update one or more records, which are already available in the database.The following procedure updates all the records having SEX as 'M'. Here, we increase AGE of all the males by one year.Example#!/usr/bin/python import MySQLdb # Open database connection db = MySQLdb.connect("localhost", "testuser", "test123", ... Read More

Database READ Operation in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 31-Jan-2020 09:58:22

587 Views

READ Operation on any database means to fetch some useful information from the database.Once our database connection is established, you are ready to make a query into this database. You can use either fetchone() method to fetch single record or fetchall() method to fetech multiple values from a database table.fetchone() ... Read More

Database INSERT Operation in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 31-Jan-2020 09:51:34

401 Views

It is required when you want to create your records into a database table.ExampleThe following example, executes SQL INSERT statement to create a record into EMPLOYEE table −#!/usr/bin/python import MySQLdb # Open database connection db = MySQLdb.connect("localhost", "testuser", "test123", "TESTDB" ) # prepare a cursor object using cursor() method cursor = db.cursor() ... Read More

Creating Database Table in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 31-Jan-2020 09:47:12

306 Views

Once a database connection is established, we are ready to create tables or records into the database tables using execute method of the created cursor.ExampleLet us create Database table EMPLOYEE −#!/usr/bin/python import MySQLdb # Open database connection db = MySQLdb.connect("localhost", "testuser", "test123", "TESTDB" ) # prepare a cursor object using cursor() method ... Read More

Database Connection in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 31-Jan-2020 09:45:11

544 Views

Before connecting to a MySQL database, make sure of the followings −You have created a database TESTDB.You have created a table EMPLOYEE in TESTDB.This table has fields FIRST_NAME, LAST_NAME, AGE, SEX and INCOME.User ID "testuser" and password "test123" are set to access TESTDB.Python module MySQLdb is installed properly on your ... Read More

How do I Install MySQLdb in Python?

Mohd Mohtashim

Mohd Mohtashim

Updated on 31-Jan-2020 09:43:48

2K+ Views

Before proceeding, you make sure you have MySQLdb installed on your machine. Just type the following in your Python script and execute it −#!/usr/bin/python import MySQLdbIf it produces the following result, then it means MySQLdb module is not installed −Traceback (most recent call last):    File "test.py", line 3, in ... Read More

How To Raise a "File Download" Dialog Box in Python?

Mohd Mohtashim

Mohd Mohtashim

Updated on 31-Jan-2020 09:43:05

330 Views

Sometimes, it is desired that you want to give option where a user can click a link and it will pop up a "File Download" dialogue box to the user instead of displaying actual content. This is very easy and can be achieved through HTTP header. This HTTP header is ... Read More

File Upload Example in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 31-Jan-2020 08:11:09

8K+ Views

To upload a file, the HTML form must have the enctype attribute set to multipart/form-data. The input tag with the file type creates a "Browse" button.        File:     OutputThe result of this code is the following form −Above example has been disabled ... Read More

Setting up Cookies in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 31-Jan-2020 08:08:10

2K+ Views

It is very easy to send cookies to browser. These cookies are sent along with HTTP Header before to Content-type field. Assuming you want to set UserID and Password as cookies. Setting the cookies is done as follows −Example#!/usr/bin/python print "Set-Cookie:UserID = XYZ;\r" print "Set-Cookie:Password = XYZ123;\r" print "Set-Cookie:Expires = ... Read More

Advertisements