Found 26504 Articles for Server Side Programming

Disconnecting Database in Python

Mohd Mohtashim
Updated on 31-Jan-2020 10:02:53

6K+ Views

To disconnect Database connection, use close() method.db.close()If the connection to a database is closed by the user with the close() method, any outstanding transactions are rolled back by the DB. However, instead of depending on any of DB lower level implementation details, your application would be better off calling commit or rollback explicitly.

Commit & RollBack Operation in Python

Mohd Mohtashim
Updated on 31-Jan-2020 10:02:15

663 Views

COMMITCommit is the operation, which gives a green signal to database to finalize the changes, and after this operation, no change can be reverted back.Here is a simple example to call commit method.db.commit()ROLLBACKIf you are not satisfied with one or more of the changes and you want to revert back those changes completely, then use rollback() method.Here is a simple example to call rollback() method.db.rollback()

Performing Database Transactions using Python

Mohd Mohtashim
Updated on 31-Jan-2020 10:01:40

1K+ Views

Transactions are a mechanism that ensures data consistency. Transactions have the following four properties −Atomicity − Either a transaction completes or nothing happens at all.Consistency − A transaction must start in a consistent state and leave the system in a consistent state.Isolation − Intermediate results of a transaction are not visible outside the current transaction.Durability − Once a transaction was committed, the effects are persistent, even after a system failure.The Python DB API 2.0 provides two methods to either commit or rollback a transaction.ExampleYou already know how to implement transactions. Here is again similar example −# Prepare SQL query to DELETE required records sql ... Read More

Database DELETE Operation in Python

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

289 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

Database Update Operation in Python

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

470 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

Database READ Operation in Python

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

812 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

Database INSERT Operation in Python

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

506 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

404 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

686 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

Advertisements