Macros/Basic/Base/es

From The Document Foundation Wiki
Jump to: navigation, search
This page is a translated version of the page Macros/Basic/Base and the translation is 71% complete.

Other languages:
English • ‎español

Return to Index

Base

Conectar con base de datos registrada

 1 Sub Main()    
 2     con = get_db("Bibliography")
 3 End Sub
 4 
 5 Function 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
12 End Function

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

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


Insertar datos preparando la sentencia SQL

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