动态SQL的概念和实例


解决问题:动态SQL:依据条件动态构造SQL语句,但欲访问的表名和字段名对编程者是已知的

     嵌入式SQL语句如何被database执行

1.静态SQL的特点

  (1)将SQL语句的检索结果参数(属性分量)传递到高级语言的变量中

2.动态SQL的特点

  (1)SQl语句可以在程序中动态构造,形成一个字符串(eg:sqltext),然后交给DBMS执行,交给DBMS执行时仍旧可以传递变量

3.动态SQL举例

 1 #include 
 2 exec sql include sqlca;
 3 
 4 exec sql begin declare section;
 5     char user_name[] = "Scott";
 6     char user_pwd[] = "tiger";
 7     char sqltext[] = "delete from customers where cid = \'c006\'";    //动态SQL语句的构造 
 8 exec sql end declare section; 
 9 
10 int main()
11 {
12     exec sql whenever sqlerror goto report_error;
13     exec sql connect :user_name identified by :user_pwd;
14     exec sql execute immediate :sqltext;    //动态SQL的执行 
15     exec sql commit release;    //提交 
16     return 0;    
17     
18     report_error:
19         print_dberror();
20         exec sql rollback release;
21         return 1;
22 } 
Example

4.动态SQL与QBE的联系应用

  (1)基于QBE设计用户操作界面

  (2)基于QBE设计动态SQL

  (3)动态SQL到达DBMS进行处理运算

5.SQl的动态构造

  (1)已知关系:Customeres(Cid, Cname, City, discnt)

    从Customers表中删除满足条件的行

    1)Y_N_Y_   Customer name is Mbale Jamson

    2)Y_N_N_   Customer is in city ()

    3)Y_N_N_   Customer discount is in range from () to ()

    程序构造:

 1 #include 
 2 #include "prompt.h"
 3 //声明变量
 4 char Vcname[];
 5 char Vcity[];
 6 double range_from, range_to;
 7 int Cname_chose,City_chose, Discnt_chose;
 8 Cname_chose = 0;
 9 City_chose = 0;
10 Discnt_chose = 0;
11 int sql_sign = 0;
12 char continue_sign[];
13 
14 exec sql include sqlca;
15 exec sql begin declare section;
16     char user_name[20], user_pwd[20];
17     char sqltext[] = "delete from customers where";    //动态SQL 
18 exec sql end declare section;
19 
20 int main()
21 {
22     exec sql whenever sqlerror goto report_error;    //设置错误状态捕获语句
23     strcpy(user_name,"poneilsql");
24     strcpy(user_pwd,"xxxx");
25     //数据库连接
26     exec sql connect :user_name identified
27         by :user_pwd;
28     
29     while(1)
30     {
31         memset(Vcname,'\0',20);
32         memset(Vcity,'\0',20);
33         if(GetCname(Vcname))    //getCname是获取用户输入Vcname的函数
34             Cname_chose = 1;
35         if(GetCity(Vcity))
36             City_chose = 1;
37         if(GetDiscntRange(&range_from,&range_to))
38             Discnt_chose = 1;
39         //选择了Cname,构造了sqltext字符串,构造是关键 
40         if(Cname_chose == 1)
41         {
42             sql_sign = 1;
43             strcat(sqltext,"Cname = \'");    //strcat表示将两个字符串连接在一起,存放在前面的字符串中 
44             strcat(sqltext,Vcname);
45             strcat(sqltext,"\'") 
46         }
47         if(City_chose == 1)
48         {
49             sql_sign = 1;
50             if(Cname_chose)
51             {
52                 strcat(sqltext,"and City = \'");
53             }
54             else
55             {
56                 strcat(sqltext,"City = \'");
57             }
58             strcat(sqltext,Vcity);
59             strcat(sqltext,"\'");
60         }
61         //继续在构造sqltext 
62         if(Discnt_chose)
63         {
64             sql_sign = 1;
65             if(Cname_chose == 0 and City_chose == 0)
66                 strcat(sqltext,"discnt>");
67             else    
68                 strcat(sqltext,"and (discnt >");
69             strcat(sqltext,dtoa(range_from));
70             strcat(sqltext,"and discnt <");
71             strcat(sqltext,dtoa(range_to));
72             strcat(sqltext,")");
73 
74         } 
75         
76         if(sql_sign)
77         {
78             exec sql execute immediate :sqltext;    //将sqltext字符串传递给DBMS,变成真正的SQL语句进行执行 
79             exec sql commit work;            
80         }
81         scanf("contiune (y/n) %1s",continue_sign);
82         if(continue_sign == "n")
83         {
84             exec sql commit release;
85             return 0;
86         }
87         //else继续执行循环 
88     }
89     //错误处理 
90     report_error:
91         print_dberror();
92         exec sql rollback release;
93         return 1; 
94 }

  (2)编写一个用户界面,依据用户输入条件构造SQL语句并执行

    1)对象:复选框对象(选中与否),文本框对象(输入文字或数字),按钮对象,

    2)对象都有对应的属性