《嵌入式编程.pdf》由会员分享,可在线阅读,更多相关《嵌入式编程.pdf(10页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、静态嵌入式,查询一个元组,localhost #include int main() EXEC SQL WHENEVER SQLWARNING SQLPRINT; EXEC SQL WHENEVER SQLERROR SQLPRINT; EXEC SQL CONNECT TO postgreslocalhost:5432 USER postgres USING 123456; EXEC SQL BEGIN DECLARE SECTION; int mm; char tt20=abcd; int yy; EXEC SQL END DECLARE SECTION; mm=2; yy=9; prin
2、tf(11mm=%d, tt=%s, yy=%d!n,mm,tt,yy); scanf(%d, printf(22mm=%d, tt=%s, yy=%d!n,mm,tt,yy); EXEC SQL SELECT eename,eeyear INTO :tt,:yy FROM public.exam WHERE eeid=:mm; EXEC SQL DISCONNECT; printf(33mm=%d, tt=%s, yy=%d!n,mm,tt,yy); return 0; 静态嵌入式,IP 链接,修改 #include int main() EXEC SQL WHENEVER SQLWARNI
3、NG SQLPRINT; EXEC SQL WHENEVER SQLERROR SQLPRINT; EXEC SQL CONNECT TO postgres192.168.1.101:5432 USER postgres USING 123456; EXEC SQL BEGIN DECLARE SECTION; int mm; char tt20=abcd; int yy; EXEC SQL END DECLARE SECTION; mm=2; yy=9; printf(11mm=%d, tt=%s, yy=%d!n,mm,tt,yy); scanf(%d, printf(22mm=%d, t
4、t=%s, yy=%d!n,mm,tt,yy); EXEC SQL SELECT eename,eeyear INTO :tt,:yy FROM public.exam WHERE eeid=:mm; printf(33mm=%d, tt=%s, yy=%d!n,mm,tt,yy); EXEC SQL UPDATE public.exam SET eeyear=eeyear+2000 WHERE eeid=:mm; EXEC SQL SELECT eename,eeyear INTO :tt,:yy FROM public.exam WHERE eeid=:mm; printf(44mm=%d
5、, tt=%s, yy=%d!n,mm,tt,yy); EXEC SQL UPDATE public.exam SET eeyear=eeyear-2000 WHERE eeid=:mm; EXEC SQL SELECT eename,eeyear INTO :tt,:yy FROM public.exam WHERE eeid=:mm; printf(55mm=%d, tt=%s, yy=%d!n,mm,tt,yy); EXEC SQL DISCONNECT; printf(66mm=%d, tt=%s, yy=%d!n,mm,tt,yy); return 0; Cursor,循环取元组 #
6、include int main() EXEC SQL WHENEVER SQLWARNING SQLPRINT; EXEC SQL WHENEVER SQLERROR SQLPRINT; EXEC SQL CONNECT TO postgres192.168.1.101:5432 USER postgres USING 123456; EXEC SQL BEGIN DECLARE SECTION; int mm; char tt20=abcd; int yy; EXEC SQL END DECLARE SECTION; mm=189; yy=9; EXEC SQL DECLARE query
7、c CURSOR FOR SELECT eeid,eename,eeyear FROM public.exam ; printf(11mm=%d, tt=%s, yy=%d!n,mm,tt,yy); EXEC SQL OPEN queryc; do EXEC SQL FETCH FROM queryc INTO :mm,:tt,:yy; if (!strcmp(SQLSTATE,02000)break; printf(66mm=%d, tt=%s, yy=%d!n,mm,tt,yy); while (strcmp(SQLSTATE,02000); EXEC SQL DISCONNECT; pr
8、intf(88mm=%d, tt=%s, yy=%d!n,mm,tt,yy); return 0; Scroll cursor 自由滑动 #include int main() EXEC SQL WHENEVER SQLWARNING SQLPRINT; EXEC SQL WHENEVER SQLERROR SQLPRINT; EXEC SQL CONNECT TO postgres192.168.1.101:5432 USER postgres USING 123456; EXEC SQL BEGIN DECLARE SECTION; int mm; char tt20=abcd; int
9、yy; int i=0; EXEC SQL END DECLARE SECTION; EXEC SQL DECLARE querycc SCROLL CURSOR FOR SELECT eeid,eename,eeyear FROM public.exam ; EXEC SQL OPEN querycc; do i=i+1; EXEC SQL FETCH FROM querycc INTO :mm,:tt,:yy; if (!strcmp(SQLSTATE,02000)break; printf(%d%dmm=%d, tt=%s, yy=%d!n,i,i,mm,tt,yy); while (s
10、trcmp(SQLSTATE,02000); EXEC SQL DECLARE queryc SCROLL CURSOR FOR SELECT eeid,eename,eeyear FROM public.exam ; EXEC SQL OPEN queryc; EXEC SQL FETCH NEXT FROM queryc INTO :mm,:tt,:yy; if (strcmp(SQLSTATE,02000) printf(NEXTmm=%d, tt=%s, yy=%d!n,mm,tt,yy); EXEC SQL FETCH LAST FROM queryc INTO :mm,:tt,:y
11、y; if (strcmp(SQLSTATE,02000) printf(LASTmm=%d, tt=%s, yy=%d!n,mm,tt,yy); EXEC SQL FETCH NEXT FROM queryc INTO :mm,:tt,:yy; if (strcmp(SQLSTATE,02000) printf(NEXTmm=%d, tt=%s, yy=%d!n,mm,tt,yy); EXEC SQL FETCH FIRST FROM queryc INTO :mm,:tt,:yy; if (strcmp(SQLSTATE,02000) printf(FIRSTmm=%d, tt=%s, y
12、y=%d!n,mm,tt,yy); EXEC SQL FETCH NEXT FROM queryc INTO :mm,:tt,:yy; if (strcmp(SQLSTATE,02000) printf(NEXTmm=%d, tt=%s, yy=%d!n,mm,tt,yy); EXEC SQL FETCH relative -1 FROM queryc INTO :mm,:tt,:yy; if (strcmp(SQLSTATE,02000) printf(-1mm=%d, tt=%s, yy=%d!n,mm,tt,yy); EXEC SQL FETCH relative -2 FROM que
13、ryc INTO :mm,:tt,:yy; if (strcmp(SQLSTATE,02000) printf(-2mm=%d, tt=%s, yy=%d!n,mm,tt,yy); EXEC SQL FETCH relative -2 FROM queryc INTO :mm,:tt,:yy; if (strcmp(SQLSTATE,02000) printf(-2mm=%d, tt=%s, yy=%d!n,mm,tt,yy); EXEC SQL FETCH FIRST FROM queryc INTO :mm,:tt,:yy; if (strcmp(SQLSTATE,02000) print
14、f(FIRSTmm=%d, tt=%s, yy=%d!n,mm,tt,yy); EXEC SQL FETCH LAST FROM queryc INTO :mm,:tt,:yy; if (strcmp(SQLSTATE,02000) printf(LASTmm=%d, tt=%s, yy=%d!n,mm,tt,yy); EXEC SQL CLOSE querycc; EXEC SQL CLOSE queryc; EXEC SQL DISCONNECT; return 0; 动态构造 pgSQL 语句,对数据进行更新。 #include int main() EXEC SQL WHENEVER
15、SQLWARNING SQLPRINT; EXEC SQL WHENEVER SQLERROR SQLPRINT; EXEC SQL CONNECT TO postgres192.168.1.101:5432 USER postgres USING 123456; EXEC SQL BEGIN DECLARE SECTION; char *tt=update exam set eeyear=eeyear+? where eeid=?; char *ttc=commit; int ii=199;int jj=20000; EXEC SQL END DECLARE SECTION; EXEC SQ
16、L PREPARE mmtt from :tt; EXEC SQL EXECUTE mmtt USing :jj,:ii; EXEC SQL EXECUTE IMMEDIATE :ttc; EXEC SQL DISCONNECT; return 0; 执行前数据库状态: 执行后数据库状态: 如果定义了SCROLL,那么游标可以向后滚动;如果定义了NO SCROLL,那么向后取的动作 会被拒绝; 如果二者都没有定义,那么是否进行向后取的动作会根据查询来判断。 执行从键盘输入的一个 pgSQL 语句,查询一个数据项。 #include int main() EXEC SQL WHENEVER SQ
17、LWARNING SQLPRINT; EXEC SQL WHENEVER SQLERROR SQLPRINT; EXEC SQL CONNECT TO postgres192.168.1.101:5432 USER postgres USING 123456; EXEC SQL BEGIN DECLARE SECTION; char *tt,str100; int ii,jj; EXEC SQL END DECLARE SECTION; tt=str; scanf(%n,tt); printf(%sn,tt); printf(qqqqqn,tt); EXEC SQL PREPARE mystmt FROM :tt; EXEC SQL EXECUTE mystmt INTO :ii; printf(eeyear=%d,ii); EXEC SQL DISCONNECT; return 0;