Macros/Basic/Base

    From The Document Foundation Wiki

    Return to Index

    Base

    Connect to register database

    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

    Create table with 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()

    Insert data with 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()


    Insert data using prepareStatement

    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()