CREATE ,INSERT a table.
==============================================================================*/EXEC SQL BEGIN DECLARE SECTION ;
CHAR sno[10],sname[10],city[10];
INT status;
EXEC SQL END DECLARE SECTION ;
EXEC SQL INCLUDE USERCA;main()
{
int i;
EXEC SQL CONNECT "cobase:cobase" ;EXEC SQL CREATE TABLE S_TEST /* Create a table named S_TEST */
(SNO CHAR (9),
SNAME CHAR (20),
STATUS INT ,CITY CHAR(10));printf(" create table success,insert?");
printf(" 0 ---- no ");
printf(" 1 ---- yes ");
printf(" choice:");
scanf("%d",&i);
while(i)
{
printf("input sno:");
scanf("%s",sno);
printf("input sname:");
scanf("%s",sname);
printf("input status:");
scanf("%d",&status);
printf("input city:");
scanf("%s",city);
EXEC SQL INSERT INTO S_TEST(SNO,SNAME,STATUS,CITY)
VALUES ((:sno,:sname,:status,:city));
printf("continue?");
printf(" 0 ----terminate ");
printf(" 1 ----continue ");
printf(" choice:");
scanf("%d",&i);}
EXEC SQL COMMIT;
EXEC SQL DISCONNECT; /*log off database*/
exit(0);
}
例题程序2 (修改和删除表中的数据)/*=============================================================================
This is a sample program which include SQL sentence about
UPDATE,DELETE a table.
==============================================================================*/EXEC SQL BEGIN DECLARE SECTION ;
CHAR sno[10],sname[10],city[10];
INT status;
EXEC SQL END DECLARE SECTION ;
EXEC SQL INCLUDE USERCA;main()
{
int i;
printf("Now exec connect ...");
EXEC SQL CONNECT "COBASE:COBASE" ;
printf(" update?");
printf(" 0 ---- no ");
printf(" 1 ---- yes ");
printf(" choice:");
scanf("%d",&i);
while(i)
{
printf("input sno:");
scanf("%s",sno);
printf("input sname:");
scanf("%s",sname);
printf("input status:");
scanf("%d",&status);
printf("input city:");
scanf("%s",city);EXEC SQL UPDATE S_TEST SET sname=:sname,city=:city ,status=:status
WHERE sno=:sno;printf(" continue?");
printf(" 0 ----no ");
printf(" 1 ----yes ");
printf(" choice:");
scanf("%d",&i);
}
printf(" delete?");
printf(" 0 ---- no ");
printf(" 1 ---- yes ");
printf(" choice:");
scanf("%d",&i);
while(i)
{
printf("input sno:");
scanf("%s",sno);EXEC SQL DELETE FROM S_TEST WHERE sno=:sno;printf(" continue?");
printf(" 0 ----no ");
printf(" 1 ----yes ");
printf(" chioce:");
scanf("%d",&i);}EXEC SQL COMMIT;
EXEC SQL DISCONNECT;
exit(0);
}第三章 查 询
在ESQL程序中,查询可以分为两大类型:返回一行的查询和返回多行的查询.对于查询,我们不仅对其执行得成功与否感兴趣,其结果更为有用.多行查询要用到游标的概念,本章就介绍查询语句和游标的概念和使用.
3.1 SELECT 语句SELECT语句是用于完成查询功能的SQL语句,查询语句因为有返回的结果,故ESQL中的SELECT语句比SQL的SELECT语句多一INTO子句,INTO子句的主变量表对应于程序中主变量,用于存放查询返回的结果.
SELECT语句格式如下:EXEC SQL SELECT <列名> [,<列名>[,...]] INTO <主变量表>
FROM <表名> [,<表名>[,...]] [WHERE <检索条件>];其中:1.检索条件中允许有主变量和嵌套子查询语句.
2.INTO后的主变量可以是数组.
3.主变量前要用":"标志.执行该语句时,COBASE找出表中满足检索条件的行,并把结果传送到INTO子句中所对应的主变量中.该语句的查询结果可以是一行或多行.WHERE后的主变量叫输入主变量,它提供了查询所需的信息.INTO子句中的主变量叫输出主变量,它保存SELECT语句运行后的结果.例题程序3 (返回一行的查询)/*=============================================================================
This is a sample program which uses SELECT statement of Esql.
It is an example of how to do querIEs that return one row.
==============================================================================*/
推荐阅读
- Awk 编程学习笔记之二
- UNIX SHELL脚本编程
- 自学编程能找到工作吗
- c++/c 编程实现获得当前用户名和组名的源程序
- Unix C 编程问题精粹
- packages/ports 使用说明
- FreeBSD 5.3 + PostgreSQL 8.0安装经验
- Sun RPC 编程简介
- 什么是编程课 什么是编程
- 计算机编程语言包括哪些