首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

请问用wxWidgets连接数据库的有关问题 卡在最后一步

2012-06-14 
请教用wxWidgets连接数据库的问题 卡在最后一步请教各位使用wxWidgets的高手,在下刚开始学习wxWidgets,在

请教用wxWidgets连接数据库的问题 卡在最后一步
请教各位使用wxWidgets的高手,在下刚开始学习wxWidgets,在使用ODBC连接数据库的时候发生了问题,折腾了一天,上网找了N多资料无法解决,实在是没辙了。

本机配置了一个ACCESS的测试用数据源,就一个极简单的表和两个字段。
数据源已使用MFC另行编译工程测试通过,配置和使用无问题。

IDE环境:Visual Studio 2008下挂上wxWidgets 2.8.10库,已编译多个程序未发现问题,并且wxWidgets库中Setup.h已修改,Samples\Db这个ODBC示范例子能正确运行。

问题描述:1、文本框没有显示绑定的字段数据;
2、点击按钮,提示“error3”,数据库连接指针没有正确的移动到下一行;

从预设的error0、1、2都没有提示来看,数据源已正确连接并打开,就是不知道为什么无法显示和获取。

以下是代码:

窗口头文件:

C/C++ code
#ifndef THEFRAME_2009_12_26_H_#define THEFRAME_2009_12_26_H_#pragma once#include "wx/wx.h"#include <wx/db.h>#include <wx/dbtable.h>class theFrame : public wxFrame{public:    theFrame(const wxString &Title, const wxPoint& pos, const wxSize& size);    virtual ~theFrame();    void OnSize(wxSizeEvent &Event);    void OnButton(wxCommandEvent &Event);    wxDbConnectInf                                *ConnectConfig;    wxDb                                        *theConnect;    wxStaticText                                *theTex;    wxDbTable                                    *tbl;    int                                            theID;    wxChar                                        theName[10];protected:private:    DECLARE_EVENT_TABLE()};#endif


窗口定义文件:
C/C++ code
#include "theFrame.h"//告诉引擎将事件和处理函数联系起来BEGIN_EVENT_TABLE(theFrame, wxFrame)    EVT_SIZE(theFrame::OnSize)    EVT_BUTTON(wxID_OK, theFrame::OnButton)END_EVENT_TABLE()theFrame::theFrame(const wxString& Title, const wxPoint& pos, const wxSize& size) : wxFrame((wxFrame *)NULL, wxID_ANY, Title, pos, size){    theID = 0;    memset(theName, 0, sizeof(theName));    tbl = NULL;    theConnect = NULL;    ConnectConfig = NULL;    //放置一个文本标签显示数据        theTex = new wxStaticText(this, wxID_ANY, theName, wxPoint(0, 0), wxSize(100, 100), 0, wxT("Text"));    wxColor theCo;    theCo.Set(255,255,255);    theTex->SetBackgroundColour(theCo);    //放置一个按钮进行逐行滚动    wxButton *theB = new wxButton(this, wxID_OK, wxT("Get"), wxPoint(0, 200));    //启动和配置数据库环境    ConnectConfig = new wxDbConnectInf(NULL, wxT("DB1"), wxT(""), wxT(""));    theConnect = NULL;    theConnect =  wxDbGetConnection(ConnectConfig);    if (!theConnect)    {        //判断链接数据库是否成功        wxMessageBox(wxT("Error0"));    }}theFrame::~theFrame(){    if (NULL != tbl) wxDELETE(tbl);    wxDbFreeConnection(theConnect);    theConnect = NULL;    wxDbCloseConnections();    if (NULL != ConnectConfig) delete ConnectConfig;}void theFrame::OnSize(wxSizeEvent &Event){}void theFrame::OnButton(wxCommandEvent &Event){    tbl=new wxDbTable(theConnect,wxT("S1"),10,wxT(""), wxDB_QUERY_ONLY, wxT(""));    if (!tbl->Open())    {        //判断是否正确打开表        wxMessageBox("error1");        return;    }    if (!tbl->GetDb())    {        //判断是否正确获取表数据        wxMessageBox("error2");        return;    }    //数据和变量绑定    tbl->SetColDefs(0, wxT("ID"), DB_DATA_TYPE_INTEGER, &theID, SQL_C_LONG, sizeof(theID), false, true);    tbl->SetColDefs(0, wxT("theName"), DB_DATA_TYPE_VARCHAR, theName, SQL_C_CHAR, sizeof(theName), false, true);    bool Test = tbl->GetNext();    if (false == Test)    {        //判断是否成功读取了下一行        wxMessageBox("error3");    }    //重置标签上的数据    theTex->SetLabel(theName);    theTex->SetClientSize(wxSize(100, 100));}


APP头文件:
C/C++ code
#ifndef THEAPP_2009_12_26_H_#define THEAPP_2009_12_26_H_#include "theFrame.h"//wxApp:程序基类class theApp : public wxApp{public:    virtual bool OnInit();protected:private:};#endif 



APP定义文件:
C/C++ code
#include "theApp.h"DECLARE_APP(theApp)IMPLEMENT_APP(theApp)bool theApp::OnInit(){    theFrame *Testf = new theFrame(wxT("Fuck"), wxPoint(100, 100), wxSize(640, 480));    Testf->Show(true);    return true;}


具体创建的工程已打包上传:
http://www.eyerecover.com/test/testODBC.rar

希望wxWidgets高人能不吝指教,谢谢了!

[解决办法]
看到了你的工程,
tbl=new wxDbTable(theConnect,wxT("S1"),10,wxT(""), wxDB_QUERY_ONLY, wxT(""));
应该为
tbl=new wxDbTable(theConnect,wxT("S1"),2,wxT(""), wxDB_QUERY_ONLY, wxT(""));

wxDbTable这个函数的第三个参数是你的数据和变量绑定的个数,你定义了2个绑定变量,所以这里应该为2
tbl->SetColDefs(0, wxT("ID"), DB_DATA_TYPE_INTEGER, &theID, SQL_C_LONG, sizeof(theID), false, true);
tbl->SetColDefs(0, wxT("theName"), DB_DATA_TYPE_VARCHAR, theName, SQL_C_CHAR, sizeof(theName), false, true);
//应该为
tbl->SetColDefs(1, wxT("theName"), DB_DATA_TYPE_VARCHAR, theName, SQL_C_CHAR, sizeof(theName), false, true);
就可以了
 

热点排行