DatabaseLayer No Results Topic is solved

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
Post Reply
wagb278
Earned a small fee
Earned a small fee
Posts: 13
Joined: Mon Mar 16, 2009 6:43 pm

DatabaseLayer No Results

Post by wagb278 » Sun Mar 29, 2009 5:09 pm

My first attempt at using DatabaseLayer is not working for me - I'm sure I'm doing something wrong and probably stupid. See code.

I have a Sqlite3 database with 3 rows. The [select * from contacts] Query with Results seems to return nothing - steps through the while() only once not finding the data I seek.

Code: Select all

void DbConfigDialog::OnbtnTestOnlyClick(wxCommandEvent& event)
{
    DatabaseLayer* pMyDatabase = new SqliteDatabaseLayer(_("/home/jim/test.db"));

    int nRows = -1;
    DatabaseResultSet* pResultSet = pMyDatabase->RunQueryWithResults(_("SELECT COUNT(*) FROM contacts"));


    if ( pResultSet )
    {
        if ( pResultSet->Next() )
        {
            nRows = pResultSet->GetResultInt(1);
        }
        pMyDatabase->CloseResultSet(pResultSet);
    }

    wxString strfname = (_("DUMMY"));
    DatabaseResultSet* pResults = pMyDatabase->RunQueryWithResults(_("SELECT * FROM contacts"));
    if (pResults)
    {
        while (pResults->Next());
        {
            strfname = pResults->GetResultString(2);

            wxLogMessage(wxString::Format(_T("firstname is: %s"), strfname.c_str()));
        }
        pMyDatabase->CloseResultSet(pResults);
    }
    else
    {
        wxMessageBox(_("Results Error"));
    }

    pMyDatabase->Close();

}
The first first result set gives expected results (nRows = 3).

I tried GetResultString(_("fname")) and changed it to the field number (base 1) because I see no method to accept the field name in the DatabaseLayer documentation. This change had no affect.

Can I access fields via name? Using the field name compiled Ok, implying there is a method that accepts strings, vice integer field number. But that does not explain why I don't step through the result set three times - so there I have a bigger problem not filling the result set.

I am using Code::Blocks SVN-5489 (wx2.8.8) on Linux, if that matters. DatabaseLayer is as of March 2009.

The library in use is the Sqlite-2.8.so file and The header files in use are:

Code: Select all

#include "wx_pch.h"
#include "DbConfigDialog.h"
#include <wx/log.h>

//(* DatabaseLayer related header files
#include "/usr/include/wx-2.8/wx/databaselayer/DatabaseLayer.h"
#include "/usr/include/wx-2.8/wx/databaselayer/SqliteDatabaseLayer.h"
#include "/usr/include/wx-2.8/wx/databaselayer/DatabaseErrorCodes.h"
#include "/usr/include/wx-2.8/wx/databaselayer/DatabaseLayerException.h"
#include "/usr/include/wx-2.8/wx/databaselayer/ResultSetMetaData.h"
//*)

#ifndef WX_PRECOMP
	//(*InternalHeadersPCH(DbConfigDialog)
	//*)
#endif
//(*InternalHeaders(DbConfigDialog)
#include <wx/xrc/xmlres.h>
//*)
What am I doing wrong? Any suggestions?

One other question I have is how to view string value in a debug watch. I set a watch on "strfname" but I get wxString and what I presume is a memory address, not the value of the string - is there a way to see the value of the string in the Code::Blocks debug watch window?

bubo
In need of some credit
In need of some credit
Posts: 7
Joined: Tue Mar 24, 2009 7:37 pm
Location: Slovakia

Post by bubo » Mon Mar 30, 2009 4:35 pm

How do you fill data into table? Maybe the filler (some sqlite db browser) don't support UTF-8, or translates it bad. I had similar troubles when I used the browser in Windows (with ANSI encoding) - table has rows but fields was empty.

wagb278
Earned a small fee
Earned a small fee
Posts: 13
Joined: Mon Mar 16, 2009 6:43 pm

SQLite3

Post by wagb278 » Mon Mar 30, 2009 4:56 pm

The database was populated from the command line interface; which I also used to verify the contents. So your idea is a possibility.

I would hope encoding is not the answer, but I need to learn writing to the database anyway.

I will try loading data using DatabaseLayer and see if that makes a difference. It may take a couple of days, but I will report back.

Thanks

wagb278
Earned a small fee
Earned a small fee
Posts: 13
Joined: Mon Mar 16, 2009 6:43 pm

Post by wagb278 » Thu Apr 02, 2009 1:47 am

I added logic to create a new table and insert three records into that new table using DatabaseLayer. I verified the new table is correct Exists and has the inserted records using Sqlite3 command line access. Then changed the SELECT stmt to read from the new table in the code provided in a previous post (above). I got the same results - what appears as an empty result set.

So, how the table is populated does not change my situation. Actually this is the expected result of this experiment - how the data was populated should not change the ability to read the data.

Any other ideas why I can't read data from a Sqlite database using wxDatabaseLayer?

I am considering switching to other wrappers (pure Sqlite C++ and MySQL++) and experiment with those.

jb_coder
Super wx Problem Solver
Super wx Problem Solver
Posts: 267
Joined: Mon Oct 18, 2004 10:55 am

Post by jb_coder » Thu Apr 02, 2009 10:51 am

Change

Code: Select all

while (pResults->Next());
to

Code: Select all

while (pResults->Next())

wagb278
Earned a small fee
Earned a small fee
Posts: 13
Joined: Mon Mar 16, 2009 6:43 pm

Post by wagb278 » Fri Apr 03, 2009 1:07 am

Amazing what the correct syntax will do for you -

Boy do I feel stupid

Thanks!

Post Reply