Macros/Basic/Base/es

From The Document Foundation Wiki
Jump to navigation Jump to search
This page is a translated version of the page Macros/Basic/Base and the translation is 71% complete.
Other languages:
English • ‎español • ‎italiano • ‎فارسی

Return to Index

Base

Conectar con base de datos registrada

Sub Main()	
    con = get_db("Bibliography")
End Sub

Function get_db(db_name As String)
    dbc = createUnoService("com.sun.star.sdb.DatabaseContext")
    If dbc.hasByName(db_name) Then
        db = dbc.getByName(db_name)
        con = db.getConnection("","")
    End If
    get_db = con
End Function

Crear tabla con SQL

con = get_db("test")

sql = "CREATE TABLE ""person"" (""id"" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY NOT NULL, ""name"" VARCHAR(255) NOT NULL, ""last_name"" VARCHAR(255) NOT NULL)"
cursor = con.createStatement()
cursor.execute(sql)
con.getParent().DatabaseDocument.store()
con.close()

Insertar datos con SQL

con = get_db("test")

sql = "INSERT INTO ""person"" (""name"", ""last_name"") VALUES ('Teresa', 'Salgueiro')"
cursor = con.createStatement()
cursor.execute(sql)
con.getParent().DatabaseDocument.store()
con.close()


Insertar datos preparando la sentencia SQL

con = get_db("test")

sql = "INSERT INTO ""person"" (""name"", ""last_name"") VALUES (?, ?)"
cursor = con.prepareStatement(sql)
cursor.setString(1, "Teresa")
cursor.setString(2, "Salgueiro")
cursor.execute()
con.getParent().DatabaseDocument.store()
con.close()