求vs2010 C++用OCCI连接oracle10成功,但getstring()报内存错的解决办法

kyt23570 2012-01-25 11:32:13
网上找了很多,但都不行,折腾好多天了,快疯了!跪求高人指点!
具体情况如下:
VS2010中用C++的MFC基于对话框的程序里,点按钮后连接数据库,读数据,显示数据
数据库环境为oracle10g
数据库连接肯定成功,并且第一项数据用getInt()取出来了,用rs->getString(2)报内存错。
#include "stdafx.h"
#include "test2.h"
#include "test2Dlg.h"
#include "afxdialogex.h"
#include <string>
#include <iostream>
#ifndef OCCI_ORACLE
#include "occi.h"
using namespace oracle::occi;
using namespace std;
#endif

void Ctest2Dlg::OnBnClickedOk()
{
Environment *env;
Connection *con;
env = Environment::createEnvironment(Environment::DEFAULT);
con = env->createConnection("scott","sa1111","orcl");

Statement *stmt = NULL;
ResultSet *rs= NULL;
string sqlString;
sqlString = "SELECT EMPNO, ENAME FROM SCOTT.EMP";
stmt = con->createStatement(sqlString);
rs = stmt->executeQuery();
rs->next();
int empno;
empno = rs->getInt(1);
string s2;
s2=rs->getString(2);

stmt->closeResultSet(rs);
con->terminateStatement(stmt);
env->terminateConnection(con);
Environment::terminateEnvironment(env);
getchar();
CDialogEx::OnOK();
}
...全文
726 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-09-18
  • 打赏
  • 举报
回复
VS IDE中,在不明白的符号getString上点鼠标右键,选转到定义。
sunjun913 2014-09-17
  • 打赏
  • 举报
回复
用heap保护了,我晕。static string 才能接啊,娘的侧比。 试试 CStringList list; while (rs->next()) { static string col1 = rs->getString(1); static string col2 = rs->getString(2); static string col3 = rs->getString(3); static string col4 = rs->getString(4); static string col5 = rs->getString(5); static string col6 = rs->getString(6); list.AddTail(......); }
wangzhh_dl2012 2013-12-15
  • 打赏
  • 举报
回复
TO: blueskiner ORACLE 10g 64位,OCCI 可以在VS2010中使用么? 我最近取数据一直失败。 谢谢
赵4老师 2013-10-11
  • 打赏
  • 举报
回复
参考demo\*.*
Program Name                              Features Illustrated
cdemo81.c                                 Using basic SQL processing with release 8 and later functionality.
cdemo82.c                                 Performing basic processing of user-defined objects.
cdemocor.c                                Using complex object retrieval (COR) to improve performance.
cdemodr1.c,cdemodr2.c,cdemodr3.c          Using INSERT/UPDATE/DELETE statements with RETURNING clause used with basic datatypes, LOBs and REFs.
cdemodsa.c                                Describing information about a table.
cdemodsc.c                                Describing information about an object type.
cdemofo.c                                 Registering and operating application failover callbacks.
cdemolb.c                                 Create and insert LOB data and then read, write, copy, append and trim the data.
cdemolb2.c                                Writing and reading of CLOB/BLOB columns with stream mode and callback functions.
cdemolbs.c                                Writing and reading to LOBs with the LOB buffering system.
cdemobj.c                                 Pinning and navigation of REF object.
cdemorid.c                                Using INSERT, UPDATE, DELETE statements and fetches to get multiple rowids in one round-trip.
cdemoses.c                                Using session switching and migration.
cdemothr.c                                Using the OCIThread package.
cdemosyev.c                               Registering predefined subscriptions and specifying a callback function to be invoked for client notifications (for more information about Advanced Queuing, see Oracle Streams Advanced Queuing User’s Guide and Reference).
ociaqdemo00.c,ociaqdemo01.c,ociaqdemo02.c Advanced queuing.
cdemodp.c,cdemodp_lip.c                   Loading data with the direct path load functions.
cdemdpco.c                                Loading a column object with the direct path load functions.
cdemdpno.c                                Loading a nested column object with the direct path load functions.
cdemdpin.c                                Loading derived type (inheritance) - direct path.
cdemdpit.c                                Loading an object table with inheritance - direct path.
cdemdpro.c                                Loading a reference with the direct path load functions.
cdemdpss.c                                Loading SQL strings with the direct path load functions.
cdemoucb.c,cdemoucbl.c                    Using static and dynamic user callbacks.
cdemoupk.c,cdemoup1.c,cdemoup2.c          Using dynamic user callbacks with multiple packages.
cdemodt.c                                 Datetime and interval example.
cdemosc.c                                 Scrollable cursor.
cdemol2l.c                                Accesses LOBs using the LONG API.
cdemoin1.c                                Inheritance demo which modifies an inherited type in a table and displays a record from the table.
cdemoin2.c                                Inheritance demo to do attribute substitutability.
cdemoin3.c                                Inheritance demo that describes an object, inherited types, object tables, and a sub-table.
cdemoanydata1.c                           Anydata demo. Inserts and selects rows to and from anydata table.
cdemoanydata2.c                           Anydata demo. Creates a type piecewise using OCITypeBeginCreate() and then describes the new type created.
cdemosp.c                                 Session pooling.
cdemocp.c                                 Connection pooling.
cdemocpproxy.c                            Connection pooling with proxy functionality.
cdemostc.c                                Statement caching.
cdemouni.c                                Program for OCI UTF16 API.
nchdemo1.c                                Shows nchar implicit conversion feature and codepoint feature.
pangdawa 2013-10-11
  • 打赏
  • 举报
回复
感谢楼上几位!!!!
「已注销」 2013-08-17
  • 打赏
  • 举报
回复
下载 http://www.oracle.com/technetwork/topics/winsoft-085727.html 最新客户端库用,可以解决兼容问题
昌云技术 2013-07-28
  • 打赏
  • 举报
回复
终于找到问题原因了,先把解答的人所发的帖子贴上: http://stackoverflow.com/questions/4592563/oracle-occi-getstring-gives-block-type-is-validphead-nblockuse 感谢stackoverflow.com 请先正确下载OCCI VS2010版本,http://www.oracle.com/technetwork/database/occidownloads-083553.html 创建环境对象时,最好指定字符集,推荐这么干。然后,由于OCCI的开发包里有.lib .dll LIB只提供函数符号,具体实现估计在DLL里,VS2010的工程Debug配置要设置成动态库多线程Debug模式,Multi-threaded Debug DLL (/MDd),如果是Release配置,就要设成Multi-threaded DLL (/MD)
昌云技术 2013-07-28
  • 打赏
  • 举报
回复
我擦,大家都遇到了getString返回后,内存报错的问题了。又没有现成的文档可以供参考下。Oracle相关人士解释下啊。
seth34 2013-01-12
  • 打赏
  • 举报
回复
同样问题,怎么搞呀
rategy 2012-10-08
  • 打赏
  • 举报
回复
我也遇到同样的问题,怀疑是OCCI本身的问题,可能解决不了。
我做的测试有:
Debug下编译,出现和报错和你一样。怀疑和返回值有关,因为返回值是std::string
Release下编译,噩梦出现了std::string采用了写时复制(WOC)参数传递出现很多问题。
最后连Environment::createEnvironment("ZHS16GBK","UTF8")这样的语句都会报出异常。

OCCI的封装实在是成问题,分析原因是大量使用了兼容性差的string对象
kaizibaby1208 2012-09-26
  • 打赏
  • 举报
回复
朋友,你这个问题解决了没啊。帮我下啊, 我也碰到了相同的问题。快疯了
mosaic 2012-01-27
  • 打赏
  • 举报
回复
一般是while取出所有记录:
while(rs->next())
{
int empno;
empno = rs->getInt(1);
.....
string s2;
s2=rs->getString(2);
....
}

其他没什么问题。 内存访问错误可能是之前的代码引起的,需要仔细检查。
呆呆猫 2012-01-26
  • 打赏
  • 举报
回复
if(rs->next())
{
int empno;
empno = rs->getInt(1);
string s2;
s2=rs->getString(2);
}
mosaic 2012-01-26
  • 打赏
  • 举报
回复
那个表里面有没有记录啊?

你没有判断rs->next()的返回值,应该只有在返回true的时候才能去取字段。

没有看出还有什么其他问题.


kyt23570 2012-01-26
  • 打赏
  • 举报
回复
数据库没问他,有20条数据,第一项number(4),第二项varchar2(10)

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧