Here is a little sample of ADOdb for python and the use of a technique similar 
to localisation for languages only here it is for SQL dialects:

adodbtest.py
#!/usr/bin/python

# demo of adodb using MySQL and Postgresql
WARNING =  "WARNING: USE THIS SCRIPT AT YOUR OWN RISK\n"
WARNING += "         THIS SCRIPT IS NOT TESTED"

# 1. install MySQL and Postgresql
# 2. install database specific drivers: psycopg and MySQL-python
# 3. install python-adodb from http://adodb.sourceforge.net
# 4. cd to directory containing adodbtest.py (this file)
# 5. edit adodbtest.py (this file) and set defines for ONLY ONE of the 
databases at a time
# 6. python adodbtest.py  # invoke 

import sys
from cgi import escape

import adodb

# DEFINES:  uncomment appropriate database section
# parameters if using Postgresql
#dbdriver   = 'postgres'
#dbserver   = 'localhost'
#dbuser     = 'postgres'
#dbuserpw   = ''
#dbdatabase = 'postgres'
#dbcreate   =  True; # True allows db creation

# parameters if using MySQL
dbdriver   = 'mysql'
dbserver   = 'localhost'
dbuser     = 'root'
dbuserpw   = ''
dbdatabase = 'adodbtest'
dbcreate   =  True; # True allows db creation
# END DEFINES


# Now we do 'localisation' for the SQL dialect, similar to localisation for 
languages
# and I am sure programmers could devise a much more elegant dictionary 
approach.
#
# Dynamically import the appropriate SQL language file into the current 
namespace.
imp = "from " + dbdriver + "_sql import *"
exec imp

print WARNING
ans = raw_input('Do you wish to continue? (y|n) ')
if (ans != "y"): sys.exit(1)

# try to connect to dbdatabase
conn   = adodb.NewADOConnection(dbdriver)
connected = False
try:    connected = 
conn.Connect(dbserver,dbuser,dbuserpw,dbdatabase)    # db 
specified
except: pass
print "connected: " + str(connected)
sql = sqlDisplayCurrentDatabase
cursor = False
try:
    cursor = conn.GetOne(sql)
    print cursor
except: pass
# sys.exit(1)

#  if not connected and dbcreate:
if (cursor != dbdatabase) and dbcreate:
    conn   = adodb.NewADOConnection(dbdriver)
    connected = 
conn.Connect(dbserver,dbuser,dbuserpw,"")  # no db specified
    print "connected: " + str(connected)
    if connected:  # if connected with no db specified
        sql  = sqlCreateDatabase % dbdatabase
        # CREATE DATABASE
        cursor = conn.Execute(sql)
        if cursor:
             print "Database: " + 
dbdatabase + " created."
             # CONNECT TO NEW DATABASE
             conn   = 
adodb.NewADOConnection(dbdriver)
             connected = 
conn.Connect(dbserver,dbuser,dbuserpw,dbdatabase)    # db 
specified
             print connected
        else:
             print "Failed to create 
database: " + dbdatabase
             print "The database server 
returned an error: " + conn.ErrorMsg()
else:
    print "Database error: " + conn.ErrorMsg()

if connected:
    print "dbdriver: " + dbdriver
    print "database: " + dbdatabase
    print "dbuser:   " + dbuser
    print "Connected successfully."
    # CREATE TABLE adodbtesttbl
    sql = sqlCreateTable_adodbtesttbl
    cursor = None
    try:    cursor = conn.Execute(sql)
    except: pass
    if cursor:
        print "Table: adodbtesttbl created."
        # INSERT INTO adodbtesttbl
        sql = sqlLoadTable_adodbtesttbl
        cursor = conn.Execute(sql)
    print ""
    print "SELECT ALL ROWS FROM adodbtesttbl:"
    sql = sqlSelectStar_adodbtesttbl
    cursor = conn.Execute(sql)
    while not cursor.EOF:
        print cursor.fields
        cursor.MoveNext()
    cursor.Close()
    print ""
    print "SELECT FIRST FIVE ROWS FROM adodbtesttbl:"
    cursor = conn.SelectLimit(sql, 5)
    while not cursor.EOF:
        print cursor.fields
        cursor.MoveNext()
    cursor.Close()
    conn.Close()
else:
    print "Database error: " + conn.ErrorMsg()


And the 'language' files:

mysql_sql.py
#!/usr/bin/python
sqlDisplayCurrentDatabase      = 'SELECT database()'
sqlCreateDatabase = "CREATE DATABASE %s"
sqlCreateTable_adodbtesttbl    = 'CREATE TABLE adodbtesttbl ( id 
INT, location CHAR(50), landmark CHAR(30))'
sqlLoadTable_adodbtesttbl      = "INSERT INTO adodbtesttbl 
(id, location, landmark)\
                      
          VALUES \
                      
          (1,'Egypt','Sphinx'),\
                      
          (2,'Italy','Leaning Tower of Pisa'),\
                      
          (3,'Greece','Parthenon'),\
                      
          (4,'France','Eiffel Tower'),\
                      
          (5,'Japan','Mount Fuji'),\
                      
          (6,'Australia','Ayers Rock/Uluru'),\
                      
          (7,'USA','Grand Canyon'),\
                      
          (8,'Canada','Niagara Falls'),\
                      
          (9,'England','Big Ben'),\
                      
          (10,'Russia','Red Square')\
                      
          "
sqlSelectStar_adodbtesttbl      = 'SELECT * FROM adodbtesttbl'


postgres_sql.py
#!/usr/bin/python
sqlDisplayCurrentDatabase      = 'SELECT 
current_database()'
sqlCreateDatabase = "CREATE DATABASE %s"
sqlCreateTable_adodbtesttbl    = 'CREATE TABLE adodbtesttbl ( id 
integer, location CHAR(50), landmark CHAR(30))'
sqlLoadTable_adodbtesttbl      = "INSERT INTO adodbtesttbl 
(id, location, landmark)\
                      
          VALUES \
                      
          (1,'Egypt','Sphinx'),\
                      
          (2,'Italy','Leaning Tower of Pisa'),\
                      
          (3,'Greece','Parthenon'),\
                      
          (4,'France','Eiffel Tower'),\
                      
          (5,'Japan','Mount Fuji'),\
                      
          (6,'Australia','Ayers Rock/Uluru'),\
                      
          (7,'USA','Grand Canyon'),\
                      
          (8,'Canada','Niagara Falls'),\
                      
          (9,'England','Big Ben'),\
                      
          (10,'Russia','Red Square')\
                      
          "
sqlSelectStar_adodbtesttbl      = 'SELECT * FROM adodbtesttbl'


Please keep in mind that I'm not a python programmer so I'm sure this quick 
hack could be greatly improved upon.  Enjoy.

Regards,
Gerry





_______________________________________________
Tinyerp-users mailing list
http://tiny.be/mailman/listinfo/tinyerp-users

Reply via email to