Found 10784 Articles for Python

Database INSERT Operation in Python

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() # 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

Creating Database Table in Python

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

308 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

Database Connection in Python

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

547 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

How do I Install MySQLdb in Python?

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       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

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

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

338 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

File Upload Example in Python

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 intentionally to save people uploading file on our server, but you can try above code with your server.Here is the script save_file.py to handle file upload −#!/usr/bin/python import cgi, os import cgitb; cgitb.enable() form = cgi.FieldStorage() # Get filename here. fileitem = form['filename'] # Test if the file was uploaded ... Read More

Setting up Cookies in Python

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 = 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

Using Cookies in CGI in Python

Mohd Mohtashim
Updated on 31-Jan-2020 08:07:20

393 Views

HTTP protocol is a stateless protocol. For a commercial website, it is required to maintain session information among different pages. For example, one user registration ends after completing many pages. How to maintain user's session information across all the web pages?In many situations, using cookies is the most efficient method of remembering and tracking preferences, purchases, commissions, and other information required for better visitor experience or site statistics.How It Works?Your server sends some data to the visitor's browser in the form of a cookie. The browser may accept the cookie. If it does, it is stored as a plain text ... Read More

Passing Drop Down Box Data to CGI Program in Python

Mohd Mohtashim
Updated on 31-Jan-2020 08:06:27

368 Views

Drop Down Box is used when we have many options available but only one or two will be selected.ExampleHere is example HTML code for a form with one drop down box − Maths Physics OutputThe result of this code is the following form −Below is dropdown.py script to handle input given by web browser.#!/usr/bin/python # Import modules for CGI handling import cgi, cgitb # Create instance of FieldStorage form = cgi.FieldStorage() # Get data from fields if form.getvalue('dropdown'):    subject = form.getvalue('dropdown') else:    subject = "Not entered" print "Content-type:text/html\r\r" print "" print "" print "Dropdown Box ... Read More

Passing Text Area Data to CGI Program in Python

Mohd Mohtashim
Updated on 31-Jan-2020 08:04:50

221 Views

TEXTAREA element is used when multiline text has to be passed to the CGI Program.ExampleHere is example HTML code for a form with a TEXTAREA box − Type your text here... The result of this code is the following form −Below is textarea.cgi script to handle input given by web browser −#!/usr/bin/python # Import modules for CGI handling import cgi, cgitb # Create instance of FieldStorage form = cgi.FieldStorage() # Get data from fields if form.getvalue('textcontent'):    text_content = form.getvalue('textcontent') else:    text_content = "Not entered" print "Content-type:text/html\r\r" print "" print ""; print "Text Area - Fifth ... Read More

Advertisements