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

SQL Anywhere 12.0.1 » UltraLite - C 及 C++ 编程 » 应用程序开发 » UltraLite C++ 应用程序开发 » 使用 SQL 语句创建和修改数据

 

使用 INSERT、UPDATE 和 DELETE 修改数据

使用 UltraLite,可以使用 ExecuteStatement 方法(ULPreparedStatement 类的一个成员)执行 SQL 数据操作。

 ♦ 插入行
注意

UltraLite 使用 ? 字符指示查询参数。对于任何 INSERT、UPDATE 或 DELETE 语句,每个 ? 都是根据其在预准备语句中的序号位置引用的。例如,第一个 ? 引用为参数 1,第二个引用为参数 2。

  1. 使用以下代码声明 ULPreparedStatement:

    ULPreparedStatement * prepStmt;
  2. 准备要执行的 SQL 语句。

    以下代码准备要执行的 INSERT 语句:

    prepStmt = conn->PrepareStatement("INSERT INTO MyTable(MyColumn1) VALUES (?)");
  3. 准备语句时检查错误。

    例如,以下代码适用于检查 SQL 语法错误:

    if( prepStmt == NULL ) {
        const ULError * ulerr;
        ulerr = conn->GetLastError();
        // write code to handle the error
        return;
    }
  4. 设置替换预准备语句中 ? 字符的值。

    以下代码在检查错误时将 ? 字符设置为 "some value"。例如,当参数序号超出预准备语句中的参数个数范围时,将捕获错误。

    if( !prepStmt->SetParameterString(1, "some value") ) {
        const ULError * ulerr;
        ulerr = conn->GetLastError();
        // write code to handle the error
        return;
    }
  5. 执行预准备语句,将数据插入数据库。

    以下代码可检查执行语句后可能出现的错误。例如,如果唯一索引中发现重复的索引值,将返回错误。



    bool success;
    success = prepStmt->ExecuteStatement();
    if( !success ) {
        const ULError * ulerr;
        ulerr = conn->GetLastError();
        // write code to handle the error
    } else {
        // Use the following line if you are interested in the number of rows inserted ...
        ul_u_long rowsInserted = prepStmt->GetRowsAffectedCount();
    }
  6. 清理预准备语句资源。

    以下代码可释放预准备语句对象使用的资源。调用 Close 方法后,不能再访问此对象。

    prepStmt->Close();
  7. 将数据提交到数据库。

    以下代码可将数据保存到数据库,并防止数据丢失。如果设备应用程序在应用程序完成提交调用前意外终止,来自第 5 步的数据将丢失。

    conn->Commit();
 ♦ 删除行
注意

UltraLite 使用 ? 字符指示查询参数。对于任何 INSERT、UPDATE 或 DELETE 语句,每个 ? 都是根据其在预准备语句中的序号位置引用的。例如,第一个 ? 引用为参数 1,第二个引用为参数 2。

  1. 使用以下代码声明 ULPreparedStatement:

    ULPreparedStatement * prepStmt;
  2. 准备要执行的 SQL 语句。

    以下代码准备要执行的 DELETE 语句:

    prepStmt = conn->PrepareStatement("DELETE FROM MyTable(MyColumn1) VALUES (?)");
  3. 准备语句时检查错误。

    例如,以下代码适用于检查 SQL 语法错误:

    if( prepStmt == NULL ) {
        const ULError * ulerr;
        ulerr = conn->GetLastError();
        // write code to handle the error
        return;
    }
  4. 设置替换预准备语句中 ? 字符的值。

    以下代码在检查错误时将 ? 字符设置为 7。例如,当参数序号超出预准备语句中的参数个数范围时,将捕获错误。

    ul_s_long value_to_delete = 7;
    if( !prepStmt->SetParameterInt(1, value_to_delete) ) {
        const ULError * ulerr;
        ulerr = conn->GetLastError();
        // write code to handle the error.
        return;
    }
  5. 执行预准备语句,将数据从数据库删除。

    以下代码可检查执行语句后可能出现的错误。例如,如果外键对尝试删除的行存在引用,将返回错误。



    bool success;
    success = prepStmt->ExecuteStatement();
    if( !success ) {
        const ULError * ulerr;
        ulerr = conn->GetLastError();
        // write code to handle the error
    } else {
        // Use the following line if you are interested in the number of rows deleted ...
        ul_u_long rowsDeleted = prepStmt->GetRowsAffectedCount();
    }
  6. 清理预准备语句资源。

    以下代码可释放预准备语句对象使用的资源。调用 Close 方法后,不能再访问此对象。

    prepStmt->Close();
  7. 将数据提交到数据库。

    以下代码可将数据保存到数据库,并防止数据丢失。如果设备应用程序在应用程序完成提交调用前意外终止,来自第 5 步的数据将丢失。

    conn->Commit();
 ♦ 更新行
注意

UltraLite 使用 ? 字符指示查询参数。对于任何 INSERT、UPDATE 或 DELETE 语句,每个 ? 都是根据其在预准备语句中的序号位置引用的。例如,第一个 ? 引用为参数 1,第二个引用为参数 2。

  1. 使用以下代码声明 ULPreparedStatement:

    ULPreparedStatement * prepStmt;
  2. 准备要执行的 SQL 语句。

    以下代码准备要执行的 UPDATE 语句:

    prepStmt = conn->PrepareStatement("UPDATE MyTable SET MyColumn = ? WHERE MyColumn = ?");
  3. 准备语句时检查错误。

    例如,以下代码适用于检查 SQL 语法错误:

    if( prepStmt == NULL ) {
        const ULError * ulerr;
        ulerr = conn->GetLastError();
        // write code to handle the error
        return;
    }
  4. 设置替换预准备语句中 ? 字符的值。

    以下代码在检查错误时将 ? 字符设置为整数值。例如,当参数序号超出预准备语句中的参数个数范围时,将捕获错误。



    bool success;
    success = prepStmt->SetParameterInt( 1, 25 );
    if( success ) {
        success = prepStmt->SetParameterInt( 2, -1 );
    }
    if( !success ) {
        const ULError * ulerr;
        ulerr = conn->GetLastError();
        // write code to handle the error
        return;
    }
  5. 执行预准备语句,更新数据库中的数据。

    以下代码可检查执行语句后可能出现的错误。例如,如果唯一索引中发现重复的索引值,将返回错误。

    success = prepStmt->ExecuteStatement();
    if( !success ) {
        const ULError * ulerr;
        ulerr = conn->GetLastError();
        // write code to handle the error
    } else {
        // if you are interested in the number of rows updated ...
        ul_u_long rowsUpdated = prepStmt->GetRowsAffectedCount();
    }
  6. 清理预准备语句资源。

    以下代码可释放预准备语句对象使用的资源。调用 Close 方法后,不能再访问此对象。

    prepStmt->Close();
  7. 将数据提交到数据库。

    以下代码可将数据保存到数据库,并防止数据丢失。如果设备应用程序在应用程序完成提交调用前意外终止,来自第 5 步的数据将丢失。

    conn->Commit();
 另请参见