db.update()

Read(1435) Label: update, database table,

Description:

Update database tables.

Syntax:

db.update(A:A',tbl,Fi:xi,…;P,…)

Update a database table based on a sequence/record sequence

db.update(cs,tbl,Fi:xi,…;P,…)

Update a database table based on a cursor

Note:

The function updates Fi field of the tbl table with the value of computing expression xi over the namesake field in sequence A. If both Fi and xi are omitted, use the namesake field of A and its values.

 

P, is the key of table tbl. First compare the database table tbl with A', the original sequence, according to the primary key if the latter parameter exists and generate a delete command to delete database records that exist in A' but don’t exist in A. When a primary key value exists in A but doesn’t exist in A', insert the corresponding record in A into the database table. If the primary key value exists in both A and A', compare the database record and the records in A and A' to see if there’s any change between the value of computing expression xi over Fi field in sequence A and the original Fi field value; and update the database table with the new value, if there is any.

 

When parameter A' is absent, the function supports only the insert and update operations; no delete operation exists.

Parameter:

db

Database connection

A

A sequence /record sequence

cs

A cursor

A'

The original sequence/record sequence, whose data is considered consistent with the database table.

tbl

The name of a database table

Fi

A field in tbl

xi

An expression which will be computed based on A and its value will be the new value of Fi; it is regarded as the same as Fi when omitted

P

The key of tbl. If omitted, it will be retrieved from tbl; if the retrieval fails, then use A’s

Option:

@u

Compare primary keys between the database table and A' to generate an UPDATE statement; skip the comparison and directly update the database table with A if A' is absent

@i

Compare primary keys between the database table with A'generate INSERT statement; skip the comparison and directly insert records into the database table with A if A' is absent

@a

Clear the target table before the update is executed; empty the database table before executing db.update(A:A',tbl,Fi:xi,…;P,…)

@k

After the execution is completed, the transaction won't be committed. If this option is omitted, the transaction will be committed

@1

The first field is an auto-increment field with no corresponding expression

@d(A:A',tbl,Fi:xi,…;P,…)

Only perform delete operation and A' must be in place

Return value:

None

Example:

Transaction commits automatically after the update is completed

 

A

 

1

=demo.query("select * from EMPLOYEE").keys(EID)

 

2

=file("D:/employee1.txt").import@t()

3

>demo.update(A2:A1,EMPLOYEE,EID,NAME,SURNAME,GENDER,STATE,BIRTHDAY,HIREDATE,DEPT,SALARY:SALARY+10;EID)

EID is the key, which must be included in the update fields. A1 and A2 have the same field names. Compare the primary keys of the database table and those of A1 and A2 to delete records that are held by A1 but not held by A2; and to insert records that exist in A2 but don’t exist in A1. Finally compare the primary key values to update field values of A2’s database table by performing SALARY+10 on SALARY field of records whose field values have changed.

4

>demo.update@u(A2:A1,EMPLOYEE,EID,NAME,SURNAME,GENDER,STATE,BIRTHDAY,HIREDATE,DEPT,SALARY:SALARY+10;EID)

With @u option, update only the field values of the records in A2 by performing SALARY+10 on SALARY field of records whose field values have changed.

5

>demo.update@u(A2,EMPLOYEE,EID,NAME,SURNAME,GENDER,STATE,BIRTHDAY,HIREDATE,DEPT,SALARY:SALARY+10;EID)

Skip the comparison of primary key values and directly insert A2’s computed records to the database table if A' is absent.

6

>demo.update@i(A2:A1,EMPLOYEE,EID,NAME,SURNAME,GENDER,STATE,BIRTHDAY,HIREDATE,DEPT,SALARY:SALARY+10;EID)

With @i option, only insert field values into records and perform SALARY+10 on SALARY field of all records.

7

>demo.update@i(A2,EMPLOYEE,EID,NAME,SURNAME,GENDER,STATE,BIRTHDAY,HIREDATE,DEPT,SALARY:SALARY+10;EID)

Skip the comparison of primary key values and directly insert records into the database table with A if A' is absent.

8

>demo.update@a(A2:A1,EMPLOYEE,EID,NAME,SURNAME,GENDER,STATE,BIRTHDAY,HIREDATE,DEPT,SALARY:SALARY+10;EID)

First empty the database table EMPLOYEE before executing db.update(A:A',tbl,Fi:xi,…;P,…).

Perform operation on records of the resulting database table whose EID is 512.

9

=file("D:/data_user.txt").import@t()

10

=mysql.query("select * from user_test").keys(USERID)

USERID, which is the key of user test table in MySQL database, is a auto-incrementing field.

11

>mysql.update@1(A9:A10,user_test,USERID:#,USERNAME;USERID)

Ignore value of the auto-incrementing field expression.

12

>demo.update@k(A2:A1,EMPLOYEE,EID,NAME,SURNAME,GENDER,STATE,BIRTHDAY,HIREDATE,DEPT,SALARY:SALARY+10;EID)

Do not commit a transaction after the execution.

13

>demo.update@d(A2:A1,EMPLOYEE,EID,NAME,SURNAME,GENDER,STATE,BIRTHDAY,HIREDATE,DEPT,SALARY:SALARY+10;EID)

Compare key values to delete records that exist in A1 but don’t exist in A2; perform operation on records of the updated database table whose EID is 1, 2 and 3.

14

=file("D://data.txt").cursor@t()

 

15

>demo.update(A14,EMPLOYEE,EID,NAME,SURNAME,GENDER,STATE,BIRTHDAY,HIREDATE,DEPT,SALARY:SALARY+10;EID)

A14 is a cursor.