I: caching the sql queries

Luca Calderano calderano at sgaspa.it
Mon Sep 15 13:27:00 CEST 2003


I MEAN YOU CAN ACCESS ADO TROUGHT COM
AS EXPLAINED HERE:

import win32com.client
import locale
from os import sys


adCmdStoredProc    =   4     # Evaluates CommandText as a stored procedure
name.
adCmdTable         =   2     # Evaluates CommandText as a table name.
adInteger          =   3     # A 4-byte signed integer.
adParamInput       =   0x1   # from enum ParameterDirectionEnum


class DBConnect:
    def __init__(self):
        self.Cnn = win32com.client.Dispatch(r'ADODB.Connection')
        self.ConnStr = r'File Name=C:\Graal.dsn'
        self.Cnn.Open (self.ConnStr)
        locale.setlocale(locale.LC_ALL , "it")

    def GetHeaders(self):
        dati=[]
        Cmd =win32com.client.Dispatch(r'ADODB.Command')
        Cmd.ActiveConnection = self.Cnn
        Cmd.CommandText = 'Intestazioni'
        Cmd.CommandType = adCmdTable
        Cmd.CommandTimeout = 15
        Rst = win32com.client.Dispatch(r'ADODB.Recordset')
        Rst.Open (Cmd)

dati.append(Rst.Fields.Item('DataRiferimento').Value.Format("%d.%m.%Y"))
        Rst.Close()
        self.Cnn.Close()
        return dati

    def GetReturn(self, Ndg=1036):
        dati=[]
        if not str(Ndg).isdigit():
            raise NoDataError('Il codice cliente inserito non e numerico')
        Cmd =win32com.client.Dispatch(r'ADODB.Command')
        Cmd.ActiveConnection = self.Cnn
        Cmd.CommandText = 'Incassi_CercaPerNdg'
        Cmd.CommandType = adCmdStoredProc
        Cmd.CommandTimeout = 15

        Prm = win32com.client.Dispatch(r'ADODB.Parameter')
        Prm = Cmd.CreateParameter('@Ndg',adInteger, adParamInput,8,Ndg)
        Cmd.Parameters.Append (Prm)
        Rst = win32com.client.Dispatch(r'ADODB.Recordset')
        Rst.Open (Cmd)
        while not Rst.EOF:
            for i in range(Rst.Fields.__len__()):
                if Rst.Fields.Item(i).Type == 135:
                    dati.append(Rst.Fields.Item(i).Value.Format("%d/%m/%Y"))
                elif Rst.Fields.Item(i).Type == 6:
                    temp =
float((long(Rst.Fields.Item(i).Value[1])&0xFFFFFFFFL) |
(long(Rst.Fields.Item(i).Value[0])<<32)) /1e4
                    dati.append(locale.format('%.2f',temp, 1))
                else:
                    pass
            Rst.MoveNext()
        Rst.Close
        self.Cnn.Close
        return dati

    def GetData(self,Ndg=1036):
        dati=[]
        if not str(Ndg).isdigit():
            raise NoDataError('Il codice cliente inserito non e numerico')
        Cmd =win32com.client.Dispatch(r'ADODB.Command')
        Cmd.ActiveConnection = self.Cnn
        Cmd.CommandText = 'Anagrafe_CercaPerNdg'
        Cmd.CommandType = adCmdStoredProc
        Cmd.CommandTimeout = 15

        Prm = win32com.client.Dispatch(r'ADODB.Parameter')
        Prm = Cmd.CreateParameter('@Ndg',adInteger, adParamInput,8,Ndg)
        Cmd.Parameters.Append (Prm)
        Rst = win32com.client.Dispatch(r'ADODB.Recordset')
        Rst.Open (Cmd)
        if not Rst.EOF:
            for i in range(Rst.Fields.__len__()):
                if Rst.Fields.Item(i).Value == None:
                    dati.append(None)
                elif Rst.Fields.Item(i).Type == 135:
                    dati.append(Rst.Fields.Item(i).Value.Format("%d/%m/%Y"))
                elif Rst.Fields.Item(i).Type == 6:
                    temp =
float((long(Rst.Fields.Item(i).Value[1])&0xFFFFFFFFL) |
(long(Rst.Fields.Item(i).Value[0])<<32)) /1e4
                    dati.append(locale.format('%.2f',temp, 1))
                elif Rst.Fields.Item(i).Type ==202 or
Rst.Fields.Item(i).Type ==203:
                    a= (Rst.Fields.Item(i).Value)
                    dati.append(a.encode('latin-1','ignore'))
                else:
                    dati.append(Rst.Fields.Item(i).Value)
        else:
            Rst.Close
            self.Cnn.Close
            raise NoDataError('Il codice cliente inserito non e corretto')
        Rst.Close
        self.Cnn.Close
        return dati

class Error(Exception):
    pass

class NoDataError(Error):
    def __init__(self, message):
        self.message = message

    def __str__(self):
        return `self.message`

if __name__ == '__main__':
    prova = DBConnect()
    try:
        a = prova.GetData()
        b = prova.GetHeaders()
        prova = DBConnect()
        c = prova.GetReturn()
        print b ,c
        print '-'*20
        for v  in a:
            if v:
                print v
            else:
                print ''
    except NoDataError,e :
        print e


      S.G.A S.p.A.
Nucleo Sistemi Informativi
     Luca Calderano


-----Messaggio originale-----
Da: python-list-admin at python.org
[mailto:python-list-admin at python.org]Per conto di JZ
Inviato: lunedi 15 settembre 2003 13.14
A: python-list at python.org
Oggetto: Re: caching the sql queries


On Mon, 15 Sep 2003 12:55:15 +0200, "Luca Calderano"
<calderano at sgaspa.it> wrote:

>>Is there any equivalent of caching sql queries for Python like ADOdb
>>for PHP? (For those who did not hear about ADODb and its feature :
>>http://phplens.com/adodb/caching.of.recordsets.html)
>
>The Only way is using Com

What do you mean?

--
JZ
--
http://mail.python.org/mailman/listinfo/python-list






More information about the Python-list mailing list