execute()

Read(317) Label: database, execute sql,

Here’s how to use execute() function.

db.execute()

Description:

Execute a SQL statement through a specified database connection.

Syntax:

db.execute(sql {,args …} )

Execute the SQL statement sql in database db. args,… is the parameter of sql, which can also be a sequence consisting of parameter values

db.execute(A, sql {, args,…} )

Execute the SQL statement sql in database db over sequence A. args,… is the parameter of sql. Executions of SQL statements will be committed in BATCHSQL mode, which reduces the frequency of database access. Here args shouldn’t be a a sequence type parameter.

db.execute(cs, sql {, args,…} )

Execute the SQL statement sql in database db over cursor cs. args,… is the parameter of sql. Executions of SQL statements will be committed in BATCHSQL mode, which reduces the frequency of database access. Here args shouldn’t be a a sequence type parameter.

Note:

The function executes a SQL statement sql through the specified database connection. It is generally used to execute the SQL statements that change the records in a database (such as update and insert).

Parameters:

db

Database connection object

sql

A SQL statement in the form of select * from table, for example.

args

An argument value passed into the specified SQL command sql. It can be a constant or an expression. The comma is used to separate multiple arguments.

A

A sequence. the SQL statement is executed on each member of A. Generally args is computed against each member of A and the value is passed to the SQL statement for execution.

cs

A cursor

Options:

@k

After the execution is completed, the transaction won't be committed. by default the transaction will be committed.

@s

No preparation will be done for transaction processing.

Example:

 

A

 

1

=demo.execute("insert into DEPARTMENT (DEPT, MANAGER)values(?,?)","TecSupport",5)

Insert a record into the table

2

=demo.execute("delete from DEPARTMENT where DEPT='TecSupport'")

Delete the records whose DEPT values are ''TecSupport''

3

=demo.execute("update DEPARTMENT set MANAGER = ? where DEPT='Sales'","7")

Modify the MANAGER value of the record whose DEPT value is 'Sales' into 7

4

=[["'TecSupport ",5],[" AppSupport",9]].new(~(1):Dept,~(2): Manager)

5

=demo.execute(A4,"insert into DEPARTMENT (DEPT, MANAGER) values (?,?)", #1,#2)

#1 and #2 stand for the first and second columns respectively in A4

6

=demo.query("select * from DEPARTMENT")

7

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

Below is data_dept.txt:

8

=demo.execute(A7,"insert into DEPARTMENT (DEPT, MANAGER)values (?,?)", #1,#2)

A7 is a cursor

9

=demo.execute("insert into DEPARTMENT(DEPT, MANAGER) values(?)",["TecSupport",9])

a args is a sequence of parameter values. It is automatically converted into  =demo.execute("insert into DEPARTMENT(DEPT, MANAGER) values(?,?)","TecSupport",9) before execution. Same result as A1’s.

Related functions:

db.query()

db.proc()