Server Side Programming Articles - Page 1904 of 2646

Database INSERT Operation in Python

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

556 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

426 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

711 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

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

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

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

576 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

SaiKrishna Tavva
Updated on 23-Sep-2024 14:19:21

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

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

633 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

528 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

332 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