日期:2014-05-17 浏览次数:20725 次
#include <stdio.h> EXEC SQL INCLUDE SQLCA; void main() { /*声明宿主变量*/ EXEC SQL BEGIN DECLARE SECTION; VARCHAR usr[20],pass[20],serv[20]; char name[20]; int id,score; EXEC SQL END DECLARE SECTION; /*设置连接数据库的用户名、密码和数据库服务名*/ strcpy(usr.arr,"orcl"); usr.len=(unsigned short)strlen((char *)usr.arr); strcpy(pass.arr,"123456"); pass.len=(unsigned short)strlen((char *)pass.arr); strcpy(serv.arr,"orcl"); serv.len=(unsigned short)strlen((char *)serv.arr); EXEC SQL CONNECT :usr IDENTIFIED BY :pass USING :serv; /*如果正式编程,最好加入对sqlca.sqlcode的判断 if(sqlca.sqlcode) { printf("ORA-ERROR: sqlca.sqlcode=%d\n",sqlca,sqlcode); exit(0); }*/ printf("Connect!\n"); /*用户输入欲查询的ID号*/ id=4; strcpy(name,"liu"); score=98; /*执行动态的SQL查询语句*/ EXEC SQL INSERT INTO student(id,name,score) VALUES (:id,:name,:score); printf("id=%d Name=%s Score=%d\n",id,name,score); /*提交事务并断开与数据库的连接*/ EXEC SQL COMMIT WORK; EXEC SQL COMMIT WORK RELEASE; printf("Disconnect!\n"); }