
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10476 Articles for Python

287 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 using cursor() method cursor = db.cursor() # Prepare SQL query to DELETE required records sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20) try: # Execute the SQL command cursor.execute(sql) # Commit your changes in the database db.commit() except: # Rollback in case ... Read More

469 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", "TESTDB" ) # prepare a cursor object using cursor() method cursor = db.cursor() # Prepare SQL query to UPDATE required records sql = "UPDATE EMPLOYEE SET AGE = AGE + 1 WHERE SEX = '%c'" % ('M') try: # Execute the SQL command cursor.execute(sql) # Commit your ... Read More

809 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() − It fetches the next row of a query result set. A result set is an object that is returned when a cursor object is used to query a table.fetchall() − It fetches all the rows in a result set. If some rows have already been extracted from the result ... Read More

505 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() # Prepare SQL query to INSERT a record into the database. sql = """INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES ('Mac', 'Mohan', 20, 'M', 2000)""" try: # Execute the SQL command cursor.execute(sql) # Commit your changes in the database db.commit() except: # ... Read More

403 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 cursor = db.cursor() # Drop table if it already exist using execute() method. cursor.execute("DROP TABLE IF EXISTS EMPLOYEE") # Create table as per requirement sql = """CREATE TABLE EMPLOYEE ( FIRST_NAME CHAR(20) NOT NULL, LAST_NAME CHAR(20), AGE INT, SEX CHAR(1), INCOME FLOAT )""" cursor.execute(sql) # ... Read More

683 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 machine.You have gone through MySQL tutorial to understand MySQL Basics.ExampleFollowing is the example of connecting with MySQL database "TESTDB"#!/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() # execute SQL query using execute() method. ... Read More

3K+ 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 import MySQLdb ImportError: No module named MySQLdbTo install MySQLdb module, use the following command −For Ubuntu, use the following command - $ sudo apt-get install python-pip python-dev libmysqlclient-dev For Fedora, use the following command - $ sudo dnf install python python-devel mysql-devel redhat-rpm-config gcc For Python ... Read More

538 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 be different from the header mentioned in previous section.For example, if you want make a FileName file downloadable from a given link, then its syntax is as follows −#!/usr/bin/python # HTTP Header print "Content-Type:application/octet-stream; name = \"FileName\"\r"; print "Content-Disposition: attachment; filename = \"FileName\"\r"; # Actual File Content will go here. ... Read More

10K+ Views
There are two common ways to upload a file using Python. One is through a cloud storage service using a web server, and CGI environment, (also known as an automated file upload system). In this tutorial, we will focus on file uploading using the CGI (Common Gateway Interface) environment. The process involves generating an HTML form for file uploading and a Python script to manage file saving and uploading to the server. The steps involved in uploading files using Python are as follows - Creating HTML Form ... Read More

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 = Tuesday, 31-Dec-2007 23:12:40 GMT";\r" print "Set-Cookie:Domain = www.tutorialspoint.com;\r" print "Set-Cookie:Path = /perl;" print "Content-type:text/html\r\r" ...........Rest of the HTML Content....From this example, you must have understood how to set cookies. We use Set-Cookie HTTP header to set cookies.It is optional to set cookies attributes like Expires, Domain, and Path. It is ... Read More