当前位置:网站首页>Orientdb batch execute SQL

Orientdb batch execute SQL

2022-06-22 07:51:00 Melody Limi

Preface :

About orientDB There are still too few Chinese resources , It took several twists and turns to find out how to execute in batches orientDB SQL Script , The following is my batch execution SQL The way to , It can be used as a reference for beginners .


Come to the point :

We are at the beginning of learning orientDB when , Often only know in web Page run script , Only suitable for the development phase :


If you enter the testing or production stage , Need to update or initialize script , Such an interface only supports execution one by one , Not competent for every update script , And it is not suitable to judge the version execution .
So we need to look for the multiline command pattern :

[email protected]:~$ /opt/orientdb/bin/console.sh

OrientDB console v.3.2.2 (build 6df1d1942b88896dabea123fe1b8b0c6975e025a, branch develop) https://www.orientdb.com
Type 'help' to display all the supported commands.
orientdb> connect remote:localhost/demodb root root

Connecting to database [remote:localhost/demodb] with user 'root'...OK
orientdb {db=demodb}> script sql
[Started multi-line command. Type just 'end' to finish and execute]

The order is as follows :

1.linux open console.sh,Windows open console.bat

[email protected]:~$ /opt/orientdb/bin/console.sh

 2. Connect to database

demodb It's the database name ,root root Is the account password

orientdb> connect remote:localhost/demodb root root

 3. Enter multiline command mode ( With end Key words end )

orientdb {db=demodb}> script sql

Basic usage in multiline mode :

1. Modifying the table structure cannot use begin

--begin The transaction is started , Statements that modify the table structure cannot be used 
script sql
create class Version extends V;
end

2. The job change table data can be begin--commit perhaps begin--rollback

begin;
    Create vertex Version set VersionNo='2021-12-31 18:00:00';
commit;
end

3.if usage , Be careful (select result )

  • Error example :

script sql
begin;
let VersionNo = SELECT VersionNo FROM Version;
if($VersionNo = '2021-12-31 18:00:00') {
    update Version set VersionNo='2022-02-12 17:56:00';
}
commit;
end

The results have not changed (VersionNo still 2021-12-31 18:00:00)

orientdb {db=demodb}> script sql
[Started multi-line command. Type just 'end' to finish and execute]
orientdb {db=demodb}> begin;
orientdb {db=demodb}> let VersionNo = SELECT VersionNo FROM Version;
orientdb {db=demodb}> if($VersionNo = '2021-12-31 18:00:00') {
orientdb {db=demodb}>     update Version set VersionNo='2022-02-12 17:56:00';
orientdb {db=demodb}> }
orientdb {db=demodb}> commit;
orientdb {db=demodb}> end

+----+---------+
|#   |operation|
+----+---------+
|0   |commit   |
+----+---------+
Server side script executed in 0.003000 sec(s). Returned 1 records
orientdb {db=demodb}> select from Version

+----+------+-------+-------------------+
|#   |@RID  |@CLASS |VersionNo          |
+----+------+-------+-------------------+
|0   |#445:0|Version|2021-12-31 18:00:00|
+----+------+-------+-------------------+

1 item(s) found. Query executed in 0.001 sec(s).
  • Write it correctly ( The value should be $VersionNo[0]):

--select The result is an array , You need to take the first value to judge 
script sql
begin;
let VersionNo = SELECT VersionNo FROM Version;
if($VersionNo[0] = '2021-12-31 18:00:00') {
    update Version set VersionNo='2022-02-12 17:56:00';
}
commit;
end

Correct result (VersionNo Has been changed to 2022-02-12 17:56:00):

orientdb {db=demodb}> script sql
[Started multi-line command. Type just 'end' to finish and execute]
orientdb {db=demodb}> begin;
orientdb {db=demodb}> let VersionNo = SELECT VersionNo FROM Version;
orientdb {db=demodb}> if($VersionNo[0] = '2021-12-31 18:00:00') {
orientdb {db=demodb}>     update Version set VersionNo='2022-02-12 17:56:00';
orientdb {db=demodb}> }
orientdb {db=demodb}> commit;
orientdb {db=demodb}> end

+----+---------+
|#   |operation|
+----+---------+
|0   |commit   |
+----+---------+
Server side script executed in 0.008000 sec(s). Returned 1 records
orientdb {db=demodb}> select from Version

+----+------+-------+-------------------+
|#   |@RID  |@CLASS |VersionNo          |
+----+------+-------+-------------------+
|0   |#445:0|Version|2022-02-12 17:56:00|
+----+------+-------+-------------------+

1 item(s) found. Query executed in 0.001 sec(s).
orientdb {db=demodb}>

原网站

版权声明
本文为[Melody Limi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220532175697.html