Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 11.0.1 (中文) » UltraLite - M-Business Anywhere 编程 » 了解 UltraLite for M-Business Anywhere 开发 » 使用 SQL 处理数据

 

数据操作:INSERT、UPDATE 和 DELETE

使用 UltraLite 可以执行 SQL 数据操作语言操作和 DDL 操作。这些操作是使用 ExecuteStatement 方法(PreparedStatement 类的一个成员)执行的。

有关 PreparedStatement 类的详细信息,请参见PreparedStatement 类

预准备语句中的参数标记

UltraLite 使用 ? 参数标记处理变量值。对于任何 INSERT、UPDATE 或 DELETE 语句,每个 ? 都是根据其在预准备语句中的序号位置引用的。例如,第一个 ? 引用为 1,第二个引用为 2。

♦  插入一行
  1. 声明 PreparedStatement 对象。

    var PrepStmt;
  2. 给预准备语句对象指派 INSERT 语句。在以下代码中,TableName 和 ColumnName 是表和列的名称。

    PrepStmt = conn.prepareStatement( 
        "INSERT into TableName(ColumnName) values (?)", null );

    空参数指示该语句没有永久名称。

  3. 为该语句指派参数值。

    var NewValue;
    NewValue = "Bob";
    PrepStmt.setStringParameter(1, NewValue);
  4. 执行该语句。

    PrepStmt.executeStatement( null );
♦  更新一行
  1. 声明 PreparedStatement 对象。

    var PrepStmt;
  2. 给预准备语句对象指派 UPDATE 语句。在以下代码中,TableName 和 ColumnName 是表和列的名称。

    PrepStmt = conn.prepareStatement(
      "UPDATE TableName SET ColumnName = ? WHERE ID = ?", null);

    空参数指示该语句没有永久名称。

  3. 使用适合于数据类型的方法为该语句指派参数值。

    var NewValue;
    NewValue = "Bob";
    PrepStmt.setStringParameter(1, NewValue);
    PrepStmt.setIntParameter(2, 6);
  4. 执行该语句

    PrepStmt.executeStatement( );
♦  删除一行
  1. 声明 PreparedStatement 对象。

    var PrepStmt;
  2. 给预准备语句对象指派 DELETE 语句。

    PrepStmt = conn.prepareStatement(
        "DELETE FROM customer WHERE ID = ?", null );

    空参数指示该语句没有永久名称。

  3. 为该语句指派参数值。

    var IDValue;
    IDValue = 6;
    PrepStmt.setIntParameter( 1, IDValue );
  4. 执行该语句。

    PrepStmt.executeStatement( );