Macros/Basic/Base

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 100% complete.
Other languages:
English • ‎español • ‎italiano • ‎فارسی

Ritorna all'indice

Base

Connettetevi per registrare il database

 1Sub Main()	
 2    con = get_db("Bibliography")
 3End Sub
 4
 5Function get_db(db_name As String)
 6    dbc = createUnoService("com.sun.star.sdb.DatabaseContext")
 7    If dbc.hasByName(db_name) Then
 8        db = dbc.getByName(db_name)
 9        con = db.getConnection("","")
10    End If
11    get_db = con
12End Function

Create una tabella con SQL

1    con = get_db("test")
2	
3    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)"
4    cursor = con.createStatement()
5    cursor.execute(sql)
6    con.getParent().DatabaseDocument.store()
7    con.close()

Inserite dati con SQL

1    con = get_db("test")
2	
3    sql = "INSERT INTO ""person"" (""name"", ""last_name"") VALUES ('Teresa', 'Salgueiro')"
4    cursor = con.createStatement()
5    cursor.execute(sql)
6    con.getParent().DatabaseDocument.store()
7    con.close()


Inserite dati usando dichiarazioni predeterminate

1    con = get_db("test")
2	
3    sql = "INSERT INTO ""person"" (""name"", ""last_name"") VALUES (?, ?)"
4    cursor = con.prepareStatement(sql)
5    cursor.setString(1, "Teresa")
6    cursor.setString(2, "Salgueiro")
7    cursor.execute()
8    con.getParent().DatabaseDocument.store()
9    con.close()