使用 UltraLite,可以使用 ExecuteStatement 方法(ULPreparedStatement 类的一个成员)执行 SQL 数据操作。
UltraLite 使用 ? 字符指示查询参数。对于任何 INSERT、UPDATE 或 DELETE 语句,每个 ? 都是根据其在预准备语句中的序号位置引用的。例如,第一个 ? 引用为参数 1,第二个引用为参数 2。
使用以下代码声明 ULPreparedStatement:
ULPreparedStatement * prepStmt; |
准备要执行的 SQL 语句。
以下代码准备要执行的 INSERT 语句:
prepStmt = conn->PrepareStatement("INSERT INTO MyTable(MyColumn1) VALUES (?)"); |
准备语句时检查错误。
例如,以下代码适用于检查 SQL 语法错误:
if( prepStmt == NULL ) { const ULError * ulerr; ulerr = conn->GetLastError(); // write code to handle the error return; } |
设置替换预准备语句中 ? 字符的值。
以下代码在检查错误时将 ? 字符设置为 "some value"。例如,当参数序号超出预准备语句中的参数个数范围时,将捕获错误。
if( !prepStmt->SetParameterString(1, "some value") ) { const ULError * ulerr; ulerr = conn->GetLastError(); // write code to handle the error return; } |
执行预准备语句,将数据插入数据库。
以下代码可检查执行语句后可能出现的错误。例如,如果唯一索引中发现重复的索引值,将返回错误。
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(); } |
清理预准备语句资源。
以下代码可释放预准备语句对象使用的资源。调用 Close 方法后,不能再访问此对象。
prepStmt->Close(); |
将数据提交到数据库。
以下代码可将数据保存到数据库,并防止数据丢失。如果设备应用程序在应用程序完成提交调用前意外终止,来自第 5 步的数据将丢失。
conn->Commit(); |
UltraLite 使用 ? 字符指示查询参数。对于任何 INSERT、UPDATE 或 DELETE 语句,每个 ? 都是根据其在预准备语句中的序号位置引用的。例如,第一个 ? 引用为参数 1,第二个引用为参数 2。
使用以下代码声明 ULPreparedStatement:
ULPreparedStatement * prepStmt; |
准备要执行的 SQL 语句。
以下代码准备要执行的 DELETE 语句:
prepStmt = conn->PrepareStatement("DELETE FROM MyTable(MyColumn1) VALUES (?)"); |
准备语句时检查错误。
例如,以下代码适用于检查 SQL 语法错误:
if( prepStmt == NULL ) { const ULError * ulerr; ulerr = conn->GetLastError(); // write code to handle the error return; } |
设置替换预准备语句中 ? 字符的值。
以下代码在检查错误时将 ? 字符设置为 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; } |
执行预准备语句,将数据从数据库删除。
以下代码可检查执行语句后可能出现的错误。例如,如果外键对尝试删除的行存在引用,将返回错误。
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(); } |
清理预准备语句资源。
以下代码可释放预准备语句对象使用的资源。调用 Close 方法后,不能再访问此对象。
prepStmt->Close(); |
将数据提交到数据库。
以下代码可将数据保存到数据库,并防止数据丢失。如果设备应用程序在应用程序完成提交调用前意外终止,来自第 5 步的数据将丢失。
conn->Commit(); |
UltraLite 使用 ? 字符指示查询参数。对于任何 INSERT、UPDATE 或 DELETE 语句,每个 ? 都是根据其在预准备语句中的序号位置引用的。例如,第一个 ? 引用为参数 1,第二个引用为参数 2。
使用以下代码声明 ULPreparedStatement:
ULPreparedStatement * prepStmt; |
准备要执行的 SQL 语句。
以下代码准备要执行的 UPDATE 语句:
prepStmt = conn->PrepareStatement("UPDATE MyTable SET MyColumn = ? WHERE MyColumn = ?"); |
准备语句时检查错误。
例如,以下代码适用于检查 SQL 语法错误:
if( prepStmt == NULL ) { const ULError * ulerr; ulerr = conn->GetLastError(); // write code to handle the error return; } |
设置替换预准备语句中 ? 字符的值。
以下代码在检查错误时将 ? 字符设置为整数值。例如,当参数序号超出预准备语句中的参数个数范围时,将捕获错误。
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; } |
执行预准备语句,更新数据库中的数据。
以下代码可检查执行语句后可能出现的错误。例如,如果唯一索引中发现重复的索引值,将返回错误。
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(); } |
清理预准备语句资源。
以下代码可释放预准备语句对象使用的资源。调用 Close 方法后,不能再访问此对象。
prepStmt->Close(); |
将数据提交到数据库。
以下代码可将数据保存到数据库,并防止数据丢失。如果设备应用程序在应用程序完成提交调用前意外终止,来自第 5 步的数据将丢失。
conn->Commit(); |
![]() |
使用DocCommentXchange讨论此页。
|
版权 © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |