执行SQL语句并处理结果
执行查询
绑定参数
SQLAPI++在执行sql语句前需要进行参数绑定。
SACommand cmd;
cmd.setCommandText(
_TSA("select * from testtable(id,firname, scdname,age) where age > :1"));
上述语句声明了一个sql文本,将文本设置为一个模板,where语句里带有一个参数。
输入参数
使用<<
进行参数的输入:
cmd << 15L;
执行查询
通过调用Execute执行查询语句:
cmd.Execute(); //执行sql语句
还可以通过调用RowsAffected函数查看执行语句后被影响的行数:
int rows=cmd.RowsAffected();
cout<<rows;
获取结果
通过调用FetchNext()即可获取结果集。
While(cmd.FetchNext()){
cout <<”id:”<<cmd.Field(_TSA(“id”)).asLong()<<” “<<
“firname:”<<cmd.Field(_TSA(“firname”)).asString().GetMultiByteChars()<<” “<<“scdname:”<<cmd.Field(_TSA(“scdname”)).asString().GetMultiByteChars()<<” “<<”age:”<<cmd.Field(_TSA(“age”)).asLong()<<endl;
}
Int型需要调用asLong,char型需要调用asString.GetMultiByteChars()。
执行更新
要更改数据(执行一个insert,update或者delete),可以直接使用Execute执行sql语句,也可以在sql语句中绑定参数,在自己输入,下面分别演示两种用法。
插入
//绑定参数
SACommand insert(&con,_TSA(“insert into testtable(id,firname,scdname,age) values(:1,:2:,3,:4);”));
insert <<3L<<_TSA(“testfir”)<<_TSA(“testscd”)<<20L;
insert.Execute();
//直接执行
SACommand insert(&con,_TSA(“insert into testtable(id,firname,scdname,age) values(2,‘xxx’,’xxx’,18);”));
insert.Execute();
更新
//绑定参数
SACcommand update(
&con,
_TSA(“update testtable set firname =:1 where firname=:2;”)
);
update<<_TSA(“joe”)<<_TSA(“mike”);
update.Execute();
//直接执行
SACcommand update(
&con,
_TSA(“update testtable set firname =’joe’ where firname=’mike’;”)
);
update.Execute();
删除
//绑定参数
SACcommand del(
&con,
_TSA(“delete from testtable where firname=:1;”)
);
del<<_TSA(“joe”);
del.Execute();
//直接执行
SACcommand del(
&con,
_TSA(“delete from testtable where firname=’joe’;”)
);
del.Execute();
创建或修改数据库对象
要创建、更改或者删除一个类似表或者视图这样的数据库对象, 直接调用Execute方法执行创建数据库对象的sql语句即可。
SACommand creatdb(
&con,
_TSA(“create table testtb(id int, firname char(10),scdname char(10),age int);”));
Creatdb.Execute()
执行存储过程
存储过程是一种在数据库中存储复杂程序,以便外部程序调用的一种数据库对象。存储过程是为了完成特定功能的SQL语句集,经编译创建并保存在数据库中,用户可通过指定存储过程的名字并给定参数(需要时)来调用执行。
使用SQLAPI++调用储存过程主要有3步:
1、初始化存储过程命令。
SACommand cmd(&con, "TestProc");
2、绑定输入参数。
//普通赋值
cmd.Param(_TSA("n1")).setAsLong() = 5L;
cmd.Param(_TSA("n2")).setAsLong() = 10L;
//使用流赋值
cmd << SAPos(_TSA("n1")) << 5L << SAPos(_TSA("n2")) << 10L;
3、执行储存过程。
cmd.Execute();
处理数据类型
二进制类型
二进制类型有blob,row,bytea等,下面代码演示这三种类型的基本用法。
创建表:
SACommand cmd(&con,_TSA(“CREATE TABLE blob_type_t1 ( BT_COL1 INTEGER, BT_COL2 BLOB, BT_COL3 RAW, BT_COL4 BYTEA ) ;”)); cmd.Execute();
插入数据:
SACommand cmd(&con,_TSA(“INSERT INTO blob_type_t1 VALUES(10,empty_blob(), HEXTORAW('DEADBEEF'),E'\\xDEADBEEF');”)); cmd.Execute();
读取数据:
SACommand cmd(&con,_TSA(“SELECT * FROM blob_type_t1; ”)); cmd.Execute(); while(cmd.FetchNext()){ … }
字符类型
Vastbase G100中TEXT类型与VARCHAR类型都是可变长的字符类型,区别在于VARCHAR类型通过VARCHAR(n)中的n来限制最大长度,而TEXT类型没有。
TEXT类型与VARCHAR类型几乎没有性能差别,TEXT类型最多可存储1G数据 。
使用TEXT类型存储数据时,可用以下方式来进行读写。
1、创建表。
cmd.setCommandText("create table images (id int, msg text)"); cmd.Execute();
2、插入数据。
cmd.setCommandText("insert into images values(1, ?)" ("the text data")); cmd.Execute();
3、读取数据。
cmd.setCommandText("select msg from images where id = 1"); cmd.Execute(); while(cmd.FetchNext()){ ……; }
使用varchar类型存储数据时。可以使用以下方法来读写。
1、创建表。
cmd.setCommandText("create table images (id int, msg varchar(20))"); cmd.Execute();
2、插入数据。
cmd.setCommandText("insert into images values(1, ?)" ("the text data")); cmd.Execute();
3、读取数据。
cmd.setCommandText("select msg from images where id = 1"); cmd.Execute(); while(cmd.FetchNext()){ ……; }
时间/日期类型
要使用日期类型,主要有一下两步:
步骤1 将日期/时间值绑定到输入变量(SQL 语句)或输入参数(存储过程/函数)中。
步骤2 读取日期/时间值(从结果集中的字段或从存储过程的输出参数)。
1、创建一个需要日期时间绑定的sql命令:
SACommand cmd(
&con,
_TSA("Update TEST set FDATETIME = :1"));
需要一个 SADate 时间类的实例来执行绑定。有几种方法可以创建它。首先是显式构造它:
SADateTime dtValue(
2022 /*year*/, 10 /*October*/, 6 /*day of month*/,
19 /*hours*/, 0 /*minutes*/, 0 /*seconds*/);
2、可以从现有的C结构tm转换为SADate时间:
extern struct tm some_tm_Value;
SADateTime dtValue = some_tm_Value;
3、绑定日期/时间值并执行:
cmd << dtValue;
cmd.Execute();
假设要从名为FDATETIMETEST的表中检索命名的日期/时间字段。
(1)创建并执行一个返回日期时间类型数据的示例 SQL 命令:
SACommand cmd(
&con,
_TSA("SELECT FDATETIME FROM TEST"));
cmd.Execute();
(2)使用 SADate 时间、标准 C 结构 tm 或 Windows DATE 数据类型来获取数据并访问日期/时间列值:
while(cmd.FetchNext())
{
SADateTime dtValue = cmd.Field(_TSA("FDATETIME"));
struct tm tmValue = dtValue;
struct tm tmValue2 = (struct tm)cmd.Field(_TSA("FDATETIME")).asDateTime();
...
DATE dateValue = dtValue;
DATE dateValue2 = (DATE)cmd.Field(_TSA("FDATETIME")).asDateTime();
...
}