Database Insert Operation in Python

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

558 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

Short Term vs Medium Term vs Long Term Scheduling

David Meador
Updated on 31-Jan-2020 09:47:42

8K+ Views

Process Scheduling handles the selection of a process for the processor on the basis of a scheduling algorithm and also the removal of a process from the processor. It is an important part of multiprogramming in operating system.Process scheduling involves short-term scheduling, medium-term scheduling and long-term scheduling. Details about these are given as follows −Long-Term SchedulingLong-term scheduling involves selecting the processes from the storage pool in the secondary memory and loading them into the ready queue in the main memory for execution. This is handled by the long-term scheduler or job scheduler.The long-term scheduler controls the degree of multiprogramming. It ... 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

712 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

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

Raise File Download Dialog Box in Python

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

577 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

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

636 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

529 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 Radio Button Data to CGI Program in Python

Mohd Mohtashim
Updated on 31-Jan-2020 08:05:24

373 Views

Radio Buttons are used when only one option is required to be selected.ExampleHere is example HTML code for a form with two radio buttons − Maths Physics The result of this code is the following form −Below is radiobutton.py script to handle input given by web browser for radio button −#!/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('subject'):    subject = form.getvalue('subject') else:    subject = "Not set" print "Content-type:text/html\r\r" print "" print "" print "Radio - Fourth CGI Program" ... Read More

Advertisements