Changes

Jump to navigation Jump to search
1,196 bytes added ,  14:09, 6 April 2015
no edit summary
Line 75: Line 75:  
str.rstrip([chars])
 
str.rstrip([chars])
 
str.strip([chars])
 
str.strip([chars])
str.replace(old, new[, count])
+
str.replace(old, new[, count])see also threading module:
 +
 
 +
import threading
 +
 
 +
 
 
str.split([char])
 
str.split([char])
 
</source>
 
</source>
Line 390: Line 394:  
'''Hiding attributes'''
 
'''Hiding attributes'''
 
attributes that start with a __ wont be visible to others<br />
 
attributes that start with a __ wont be visible to others<br />
 +
== os.system() ==
 +
To execute a Linux command from Python:
 +
<source lang="python">
 +
os.system(command) → returns exit status
 +
</source>
 +
== Database Access ==
 +
=== MySQLdb ===
 +
<source lang="python">
 +
#!/usr/bin/python
 +
 +
import MySQLdb
 +
 +
# Open database connection
 +
db = MySQLdb.connect("DB_HOST","DB_USER","DB_PASSWORD","DB_NAME" )
 +
 +
# prepare a cursor object using cursor() method
 +
cursor = db.cursor()
   −
== MySQLdb ==
+
# Drop table if it already exist using execute() method.
== os.system() ==
+
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)
 +
 
 +
# Prepare SQL query to INSERT a record into the database.
 +
sql = "INSERT INTO EMPLOYEE(FIRST_NAME, \
 +
      LAST_NAME, AGE, SEX, INCOME) \
 +
      VALUES ('%s', '%s', '%d', '%c', '%d' )" % \
 +
      ('Mac', 'Mohan', 20, 'M', 2000)
 +
try:
 +
  # Execute the SQL command
 +
  cursor.execute(sql)
 +
  # Commit your changes in the database
 +
  db.commit()
 +
except:
 +
  # Rollback in case there is any error
 +
  db.rollback()
 +
 
 +
# disconnect from server
 +
db.close()
 +
</source>
 
== ssh tunneling ==
 
== ssh tunneling ==
 
<source lang="python">
 
<source lang="python">

Navigation menu