
Veja o exemplo a seguir, acessando uma Base de Dados OpenBASE Local.
O esquema do banco usado neste exemplo é o seguinte:
Banco EXEMPLO 1
nome: PESSOA E
NOMEP(0) U20
IDADE N3
O Script Python é o seguinte:
#!c:\util\python\python.exe
Banco = "C:\\usr\\tsgbd\\tsdic\\Exemplo"
nivel = "a"
seguranca = 1
modo = "2"
Arquivo="PESSOA"
gw_nome=" Digite aqui o nome ... "
gw_idade="***"
import win32com.client
def DisplayHTML(gw_nome,gw_idade):
import os
env=os.environ
script=str(env.get('SCRIPT_NAME'))
print "Content-Type: text/html\r\n\r\n"
print "<HTML><BODY>"
print "<h2>Demonstração OpenBASE - COM - Python</h2>"
print '<Form method="post" action="'+script+'">'
print '<INPUT TYPE=HIDDEN NAME="in_key" VALUE="process">'
print "Informe o Nome: "
print '<input type="text" size=20 name="in_nome" value="'+gw_nome+'">'
print '<input type="submit" name="in_cont" value="Continua">'
print "<br><br><br><b>Resultados: <br>"
print "<ul>"
print "<li>Nome ==> " + gw_nome + "<br>"
print "<li>Idade ==> " + gw_idade
print "<eul>"
print "</b></form></body></html>"
def ProcessForm(form):
# extract the information from the form in easily digestible format
try:
gw_nome = form["in_nome"].value
o = win32com.client.Dispatch("OpenBase.OBcom.1")
r = o.OAbreBancoDeDados(Banco, nivel, seguranca, modo)
r = o.OObtemRegistrosNoArquivo(Arquivo)
r = o.OLeRegistroPorChavePrimaria(Arquivo, gw_nome)
gw_idade = o.OPegaItem(Arquivo,"IDADE")
r = o.OFechaBancoDeDados(0)
except:
# name is required, so output an error if
# not given and exit script
#Display("You need to at least supply a name. Please go back.")
raise SystemExit
DisplayHTML(gw_nome,gw_idade)
###
### Begin actual script
###
### evaluate CGI request
import cgi
form = cgi.FieldStorage()
### "key" is a hidden form element with an
### action command such as "process"
try:
key = form["in_key"].value
except:
key = None
if key == "process":
ProcessForm(form)
else:
DisplayHTML(gw_nome,gw_idade)
