环境:oracle11g、vs2022
1、编写pc文件
EXEC SQL BEGIN DECLARE SECTION; struct datatable { int id; char name[30]; } data; EXEC SQL END DECLARE SECTION;View Code
//#define SQLCA_STORAGE_CLASS extern //增加该行报错 EXEC SQL INCLUDE connect_oracle.h; #include <sqlca.h> #include <iostream> using namespace std; EXEC SQL BEGIN DECLARE SECTION; char* pid ="scott/tiger@orcl"; int ids; EXEC SQL END DECLARE SECTION; int connectdb(){ EXEC SQL CONNECT :pid; if(sqlca.sqlcode!=0){ cout<< "connect err "<<sqlca.sqlcode<<endl; return sqlca.sqlcode;; } cout<< "connect ok"<<endl; return sqlca.sqlcode;; } int selectData(datatable& data){ EXEC SQL SELECT * INTO :data FROM TEST where id=1; return sqlca.sqlcode; } int insertData(datatable& data){ exec sql insert into test(id,name) values (:data.id,:data.name); if(sqlca.sqlcode!=0){ cout<< "insert err "<<sqlca.sqlcode<<endl; return sqlca.sqlcode;; } exec sql commit; return sqlca.sqlcode; } int updateData(datatable& data){ exec sql update test set name=:data.name where id=:data.id; if(sqlca.sqlcode!=0){ cout<< "update err "<<sqlca.sqlcode<<endl; return sqlca.sqlcode;; } exec sql commit; return sqlca.sqlcode; } int deleteData(int id){ ids =id; exec sql delete from test where id=:ids; if(sqlca.sqlcode!=0){ cout<< "delete err "<<sqlca.sqlcode<<endl; return sqlca.sqlcode;; } exec sql commit; return sqlca.sqlcode; } void disconnect(){ EXEC SQL COMMIT RELEASE; }View Code
2、编译pc文件
在dos中执行 proc code=cpp ./connect_oracle.pc
3.在vs新建工程,添加新增的cpp文件
#define SQLCA_STORAGE_CLASS extern #include <iostream> #include <sqlca.h> using namespace std; struct datatable { int id; char name[30]; }; extern int connectdb(); extern int selectData(datatable& data); extern int insertData(datatable& data); extern int updateData(datatable& data); extern int deleteData(int id); extern void disconnect(); int main() { connectdb(); datatable da; selectData(da); datatable insertdata; insertdata.id = 2; strcpy(insertdata.name, "bbb"); insertData(insertdata); strcpy(insertdata.name, "ccc"); updateData(insertdata); deleteData(2); disconnect(); return 0; }View Code
标签:EXEC,int,insertdata,c++,extern,SQL,oracle,proc,datatable From: https://www.cnblogs.com/ZJ199012/p/17450882.html