programacion de bases de datos en ooobasic

Download Programacion de bases de datos en OOoBasic

If you can't read please download the document

Upload: alexandro-colorado

Post on 10-Jun-2015

10.075 views

Category:

Technology


0 download

DESCRIPTION

Programacion de OpenOffice.org

TRANSCRIPT

  • 1. Programando bases de datos en OpenOffice.org Por Alexandro JZA Colorado

2. De que trata esta presentacin?

  • Comenzar una macro
  • Crear una base de datos
  • Llamar la base de datos
  • Procesar sentencias
  • Anatoma del cdigo Basic
  • Otras ideas

3. Como comenzar una macro?

  • Vaya a Herramientas Macros Organizar Macro Nuevo
  • Basic puede llamar a Base como un mdulo com.sun.star.sdb.DatabaseContext
  • Basic puede interactuar con base:
    • executeQuery()
    • getConnection()
    • createStatement()
    • executeUpdate()
  • Puedes usar Base para conectarte a bases de datos remotas como Oracle, MySQL y PostgreSQL

4. Diagrama de la logica

  • Componente de DB
  • Seleccionar DB
    • Autenticarse
  • Crear la instruccin
  • Ciclar las instrucciones en toda las tabla

getByURL (myDatabase) DatabaseContext Statement Loop 5. Conectarte a una DB

  • Script para conectarte a una base de datos existente y registrada en Base usandogetByName()
  • REM If the database does not exist, then create it.
  • If NOTFileExists ( dbURL ) Then
  • CreateBinaryDB ( dbURL ,bVerbose )
  • End If
  • REM Use the DatabaseContext to get a reference to the database.
  • oBaseContext=CreateUnoService ( "com.sun.star.sdb.DatabaseContext" )
  • oDB=oBaseContext . getByName ( dbURL )
  • oCon=oDB . getConnection ( "" ,"" )
  • oStmt = oCon . createStatement ()
  • sTableName$="BINDATA"
  • REM First, check to see if the table exists!
  • sSql="select count(*) from INFORMATION_SCHEMA.SYSTEM_TABLES "& _
  • "where TABLE_NAME='"&sTableName&"' "& _
  • "AND TABLE_SCHEM='PUBLIC'"
  • nCount=0
  • oResult=oStmt . executeQuery ( sSql )
  • If NOTIsNull ( oResult ) AND NOTIsEmpty ( oResult ) Then
  • oResult .Next()
  • nCount=oResult . getLong ( 1 )
  • End If

6. Insertar comando a una DB

  • Guarda la sentencia a una variable y esta la envia a una funcin llamadaexecuteQuery()
  • REM If the database does not exist, then create it.
  • If NOTFileExists ( dbURL ) Then
  • CreateBinaryDB ( dbURL ,bVerbose )
  • End If
  • REM Use the DatabaseContext to get a reference to the database.
  • oBaseContext=CreateUnoService ( "com.sun.star.sdb.DatabaseContext" )
  • oDB=oBaseContext . getByName ( dbURL )
  • oCon=oDB . getConnection ( "" ,"" )
  • oStmt = oCon . createStatement ()
  • sTableName$="BINDATA"
  • REM First, check to see if the table exists!
  • sSql="select count(*) from INFORMATION_SCHEMA.SYSTEM_TABLES "& _
  • "where TABLE_NAME='"&sTableName&"' "& _
  • "AND TABLE_SCHEM='PUBLIC'"
  • nCount=0
  • oResult=oStmt . executeQuery ( sSql )
  • If NOTIsNull ( oResult ) AND NOTIsEmpty ( oResult ) Then
  • oResult .Next()
  • nCount=oResult . getLong ( 1 )
  • End If

7. Procesar comando a una DB

  • Un bucle el cual va reportando los resultado uno por uno usando un If...Next(), tambin puedes con When.
  • REM If the database does not exist, then create it.
  • If NOTFileExists ( dbURL ) Then
  • CreateBinaryDB ( dbURL ,bVerbose )
  • End If
  • REM Use the DatabaseContext to get a reference to the database.
  • oBaseContext=CreateUnoService ( "com.sun.star.sdb.DatabaseContext" )
  • oDB=oBaseContext . getByName ( dbURL )
  • oCon=oDB . getConnection ( "" ,"" )
  • oStmt = oCon . createStatement ()
  • sTableName$="BINDATA"
  • REM First, check to see if the table exists!
  • sSql="select count(*) from INFORMATION_SCHEMA.SYSTEM_TABLES "& _
  • "where TABLE_NAME='"&sTableName&"' "& _
  • "AND TABLE_SCHEM='PUBLIC'"
  • nCount=0
  • oResult=oStmt . executeQuery ( sSql )
  • If NOTIsNull ( oResult ) AND NOTIsEmpty ( oResult ) Then
  • oResult .Next()
  • nCount=oResult . getLong ( 1 )
  • End If

8. Reportando tus datos Aqu insertamos a la hoja de calculohaciendo unbucle 9. Reportando tus datos

  • Como poner tu informacin en tus documentos.
  • Este ejemplo tenemos la informacin en Ooo en la hoja de clculo en oSheet
  • Result = Stmnt.executeQuery(strSQL)
  • ' we get on the current document
  • firstDoc = ThisComponent.getSheets().getByIndex(y)
  • oDoc = ThisComponent
  • ....
  • While Result.next()
  • oDoc.getSheets().insertNewByName(x, 1 )
  • oSheets( 1 ). getCellbyPosition ( 2 , 2 ).SetString( Result.getString (z))
  • Wend
  • oCon.close()

Esta funcin no es funcional ya que no declaramos el contador engetCellByPosition() pero podemos verResult . getString() 10. Crear DB desde Basic

  • Script para crear una nueva base de datos
  • REM Use "Option Compatible", or you can not use a default argument.
  • Sub CreateBinaryDB (Optional dbURL$ = "" , Optional bVerbose = False )
  • Dim oDBContext 'DatabaseContext service.
  • Dim oDB 'Database data source.
  • REM No URL Specified, get one.
  • If dbURL = "" Then dbURL = ChooseAFile ( OOoBaseFilters (), False )
  • REM Still No URL Specified, exit.
  • If dbURL = "" Then Exit Sub
  • If FileExists ( dbURL ) Then
  • If bVerbose Then Print "The file already exists."
  • Else
  • If bVerbose Then Print "Creating " & dbURL
  • oDBContext = createUnoService ( "com.sun.star.sdb.DatabaseContext" )
  • oDB = oDBContext . createInstance ()
  • oDB . URL = "sdbc:embedded:hsqldb"
  • oDB . DatabaseDocument . storeAsURL ( dbURL , Array ())
  • End If
  • End Sub