Makros/Basis/Base

    From The Document Foundation Wiki
    This page is a translated version of the page Macros/Basic/Base and the translation is 100% complete.
    Other languages:

    Zurück zum Inhaltsverzeichnis

    Base

    Anmelden bei der Datenbank

    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

    Tabelle mit SQL erstellen

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

    Daten mit SQL einfügen

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


    Daten mit einem prepareStatement einfügen

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