How to create an update query with Open Office Base?

╄→гoц情女王★ 提交于 2019-11-29 18:12:21
Jim K

Base does not typically use update queries (but see below). Instead, the easiest way to do an update command is to go to Tools -> SQL. Enter something similar to the following, then press Execute:

UPDATE "Table1" SET "Value" = 'BBB' WHERE ID = 0

The other way is to run the command with a macro. Here is an example using Basic:

Sub UpdateSQL
    REM Run an SQL command on a table in LibreOffice Base
    Context = CreateUnoService("com.sun.star.sdb.DatabaseContext")
    databaseURLOrRegisteredName = "file:///C:/Users/JimStandard/Desktop/New Database.odb"
    Db = Context.getByName(databaseURLOrRegisteredName )
    Conn = Db.getConnection("","") 'username & password pair - HSQL default blank

    Stmt = Conn.createStatement()
    'strSQL = "INSERT INTO ""Table1"" (ID,""Value"") VALUES (3,'DDD')"
    strSQL = "UPDATE ""Table1"" SET ""Value"" = 'CCC' WHERE ID = 0"
    Stmt.executeUpdate(strSQL)

    Conn.close()
End Sub

Note that the data can also be modified with a form or by editing the table directly.

Under some circumstances it is possible to create an update query. I couldn't get this to work with the default built-in HSQLDB 1.8 engine, but it worked with MYSQL.

  1. In the Queries section, Create Query in SQL View
  2. Click the toolbar button to Run SQL Command directly.
  3. Enter a command like the following:
    update mytable set mycolumn = 'This is some text.' where ID = 59;
  1. Hit F5 to run the query.

It gives an error that The data content could not be loaded, but it still performs the update and changes the data. To get rid of the error, the command needs to return a value. For example, I created this stored procedure in MYSQL:

DELIMITER $$
CREATE PROCEDURE update_val
(
   IN id_in INT,
   IN newval_in VARCHAR(100)
)
BEGIN
    UPDATE test_table SET value = newval_in WHERE id = id_in;
    SELECT id, value FROM test_table WHERE id = id_in;
END
$$
DELIMITER ;

Then this query in LibreOffice Base modifies the data without giving any errors:

CALL update_val(2,'HHH')

See also:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!