#!/usr/bin/python # TITLE: dbtrainer3 # WRITTEN BY: Bradley Stec, July 1999 # RuleSpace, Inc. # SUPPORTS: WebOps Database Interface Trainer Script # PURPOSE: This script is a code and output example of how to use the # MySQLdb API to communicate to a database and perform basic # SQL command language requests. # FUNCTION: Both by executing the script and by reading the source code, # a programmer should be able to get a grasp on how the interface # works and may borrow code for their own script projects. # REQUIREMENTS: This script requires the access to the webops module, and that a # database named "test" be installed in the webops specified # database server. # # CHANGES: 7/99 Initial Version import sys import webops import traceback sys.stderr = sys.stdout # Set Up Page Layout print "content-type: text/html" print print "\n" print "\n" print "RuleSpace, Inc. WebOps Database Interface Trainer -- Part 3

" print "The purpose of this page is to present sample code for interfacing with the databases within the Web Operations server farm. The display below illustrates real database queries as well as how the information is handled and presented.

" print "By viewing the CGI script which presents the information below you'll be able to see how the database interface and API works within the WebOps scripting environment.

" print "


" # Loading The API Support # The API MySQLdb is a thin wrapper around a C program which is also in the site-packages directory called _mysqlmodule.so # The API follows the Python Database API Spec version 2.0 which basically sets standards for what kind of methods will be # used to access databases within the Python environment. The _mysqlmodule.so and MySQLdb.py programs allow a programmer # to pass SQL commands/queries through to a database. The database server may be remote or local. The MySQL database server # software doesn't provide an interactive cursor, so the API programs create a "cursor" in local memory. Read the Python # Database API Spec version 2.0 for details. # This command loads the support files import MySQLdb # PREPARE TO CONNECT TO DATABASE # This command forms a connection through the support files to the target database server and database name. try: WebOpsDB = MySQLdb.Connection(host=webops.dbserver,user=webops.dbuser,passwd=webops.dbpass,db='test',port=3306,unix_socket="") except: print "\n\n
"
        traceback.print_exc()
        sys.exit()

# A LITTLE BONUS INFORMATION FROM THE API
# The API can provide some information about the connection and server.  Note that these functions are
# making use of the "db" object which is a sub-class of the WebOpsDB object.  The "db" object bypasses the "wrapper"
# level of the MySQLdb interface and speaks directly to the C coded _mysqlmodule.so, therefore it is not recommended
# that you use this method frequently in timing-sensitive script applications.
print "CONNECTED TO
" print "    server ", WebOpsDB.db.get_host_info(), "
" print "    running mySQL server v", WebOpsDB.db.get_server_info(), "
" print "    current status: ",WebOpsDB.db.stat(), "

" print "


" # CREATE YOUR PERSONAL CONNECTION TO THE "RESULTS" WITHIN THE CONNECTION OBJECT # We haven't actually queried anything yet, so don't get confused, we're just preparing for it. try: cursor = WebOpsDB.cursor() except: print "\n\n
"
        traceback.print_exc()
        sys.exit()

# EXAMPLE: DELETING A RECORD
# This example deletes record.
try:
	cursor.execute("DELETE FROM TEAM WHERE FIRST_NAME='Frederick' AND LAST_NAME='Clueless';")
	cursor.execute("DELETE FROM TEAM WHERE FIRST_NAME='Ruprecht';")
except:
        print "\n\n
"
        traceback.print_exc()
        sys.exit()

# REQUERY TO SEE RESULTS
try:
	cursor.execute("SELECT * FROM TEAM")
	resultSet = cursor.fetchall()
except:
        print "\n\n
"
        traceback.print_exc()
        sys.exit()

print "EXAMPLES OF USING THE DELETE SQL COMMAND.

" print "DELETING FRED AND HIS FRIEND RUPRECHT
" print "" print "" for cursorFieldname in cursor.description: print "" print "" for cursorRecord in resultSet: print "" for cursorField in cursorRecord: print "" print "" print "
" +cursorFieldname[0]+ "
" print cursorField print "
" print "" print " 


" # CLOSING THE DATABASE OBJECT try: WebOpsDB.close() except: print "\n\n

"
        traceback.print_exc()
        sys.exit()

# CONTINUED IN DBTRAINER4
# To learn more about converting result sets into Python dictionary data types, see dbtrainer4.

print "\n"
print "\n"