Help - wxGrid + wxSQLite3

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
Post Reply
dannyyy
In need of some credit
In need of some credit
Posts: 2
Joined: Tue Sep 07, 2010 9:33 pm

Help - wxGrid + wxSQLite3

Post by dannyyy »

Hi, guys !

I am having a problem to make my program work correctly.

I need to create a grid with the following columns:
Name Score1 Score2 Score3 Score4 Score5 Score6

And each of the lines is an alumn, and his/her scores.

Those scores need to be stored inside a SQLite database, to be compared and filtered later.

The real problem is: I can't understand how to input data on wxGrid, and store each data on wxSQLite3 database. I'm trying to do that
for a MONTH now, but I can't do it right. The sample program doesn't help much, because it's a command line program. I tryed to read
the code from some programs that use wxSQLite3, such as CodeLite, wxquickrun and wxSQLitePlus, but they are much more complex than my
program. They have things that I don't need, such as encryption or memory buffer.

I know Ulrich Telle and the other guys don't have much time available, but I would be very thankful if you check the code attached to this post.

Code: Select all

#ifndef TESTSCORESAPP_H
#define TESTSCORESAPP_H

#include <wx/app.h>

class TestScoresApp : public wxApp
{
    public:
        virtual bool OnInit();
};

#endif // TESTSCORESAPP_H

Code: Select all

#include "TestScoresApp.h"

//(*AppHeaders
#include "TestScoresMain.h"
#include <wx/image.h>
//*)

IMPLEMENT_APP(TestScoresApp);

bool TestScoresApp::OnInit()
{
    //(*AppInitialize
    bool wxsOK = true;
    wxInitAllImageHandlers();
    if ( wxsOK )
    {
    	TestScoresFrame* Frame = new TestScoresFrame(0);
    	Frame->Show();
    	SetTopWindow(Frame);
    }
    //*)
    return wxsOK;

}

Code: Select all

#ifndef TESTSCORESMAIN_H
#define TESTSCORESMAIN_H

//(*Headers(TestScoresFrame)
#include <wx/grid.h>
#include <wx/toolbar.h>
#include <wx/frame.h>
//*)

class TestScoresFrame: public wxFrame
{
    public:

        TestScoresFrame(wxWindow* parent,wxWindowID id = -1);
        virtual ~TestScoresFrame();

    private:

        //(*Handlers(TestScoresFrame)
        void OnToolBarItemAddClicked(wxCommandEvent& WXUNUSED(ev));
        void OnToolBarItemCloseClicked(wxCommandEvent& event);
        //*)

        //(*Identifiers(TestScoresFrame)
        static const long ID_GRIDSCORES;
        static const long ID_TOOLADD;
        static const long ID_TOOLCLOSE;
        static const long ID_TOOLBARMAIN;
        //*)

        //(*Declarations(TestScoresFrame)
        wxToolBarToolBase* ToolBarItemAdd;
        wxToolBar* ToolBarMain;
        wxToolBarToolBase* ToolBarItemClose;
        wxGrid* GridScores;
        //*)

        DECLARE_EVENT_TABLE()
};

#endif // TESTSCORESMAIN_H

Code: Select all

#include "TestScoresMain.h"
#include <wx/msgdlg.h>

//(*InternalHeaders(TestScoresFrame)
#include <wx/string.h>
#include <wx/intl.h>
#include <wx/bitmap.h>
#include <wx/image.h>
#include <wx/artprov.h>
//*)

#include <wx/wxsqlite3.h>

//(*IdInit(TestScoresFrame)
const long TestScoresFrame::ID_GRIDSCORES = wxNewId();
const long TestScoresFrame::ID_TOOLADD = wxNewId();
const long TestScoresFrame::ID_TOOLCLOSE = wxNewId();
const long TestScoresFrame::ID_TOOLBARMAIN = wxNewId();
//*)

BEGIN_EVENT_TABLE(TestScoresFrame,wxFrame)
    //(*EventTable(TestScoresFrame)
    //*)
END_EVENT_TABLE()

TestScoresFrame::TestScoresFrame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(TestScoresFrame)
    Create(parent, id, _("Test Scores"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
    GridScores = new wxGrid(this, ID_GRIDSCORES, wxPoint(128,184), wxDefaultSize, 0, _T("ID_GRIDSCORES"));
    GridScores->CreateGrid(10,7);
    GridScores->EnableEditing(true);
    GridScores->EnableGridLines(true);
    GridScores->SetColLabelValue(0, _("Name"));
    GridScores->SetColLabelValue(1, _("Score 1"));
    GridScores->SetColLabelValue(2, _("Score 2"));
    GridScores->SetColLabelValue(3, _("Score 3"));
    GridScores->SetColLabelValue(4, _("Score 4"));
    GridScores->SetColLabelValue(5, _("Score 5"));
    GridScores->SetColLabelValue(6, _("Score 6"));
    GridScores->SetDefaultCellFont( GridScores->GetFont() );
    GridScores->SetDefaultCellTextColour( GridScores->GetForegroundColour() );
    ToolBarMain = new wxToolBar(this, ID_TOOLBARMAIN, wxDefaultPosition, wxDefaultSize, wxTB_HORIZONTAL|wxTB_LEFT|wxNO_BORDER, _T("ID_TOOLBARMAIN"));
    ToolBarItemAdd = ToolBarMain->AddTool(ID_TOOLADD, _("Add Line"), wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(_T("wxART_ADD_BOOKMARK")),wxART_TOOLBAR), wxNullBitmap, wxITEM_NORMAL, _("Add Line"), wxEmptyString);
    ToolBarItemClose = ToolBarMain->AddTool(ID_TOOLCLOSE, _("Close"), wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(_T("wxART_QUIT")),wxART_TOOLBAR), wxNullBitmap, wxITEM_NORMAL, _("Close"), wxEmptyString);
    ToolBarMain->Realize();
    SetToolBar(ToolBarMain);

    Connect(ID_TOOLADD,wxEVT_COMMAND_TOOL_CLICKED,(wxObjectEventFunction)&TestScoresFrame::OnToolBarItemAddClicked);
    Connect(ID_TOOLCLOSE,wxEVT_COMMAND_TOOL_CLICKED,(wxObjectEventFunction)&TestScoresFrame::OnToolBarItemCloseClicked);
    //*)
}

TestScoresFrame::~TestScoresFrame()
{
    //(*Destroy(TestScoresFrame)
    //*)
}


void TestScoresFrame::OnToolBarItemAddClicked(wxCommandEvent& WXUNUSED(ev))
{
    int totalLines = GridScores->GetNumberRows();
    GridScores->InsertRows(totalLines +1);
}

void TestScoresFrame::OnToolBarItemCloseClicked(wxCommandEvent& event)
{
    Close();
}

Code: Select all

#ifndef DBSCORES_H
#define DBSCORES_H

#include "wx/wxsqlite3.h"

class dbScores : public wxSQLite3Database
{
    wxSQLite3Database * ScoresDatabase;
    public:
        dbScores();
        virtual ~dbScores();
    protected:
    private:
};

#endif // DBSCORES_H

Code: Select all

#include "dbScores.h"

#include "TestScoresMain.h"

dbScores::dbScores()
{
    //ctor
    ScoresDatabase = new wxSQLite3Database();
}

dbScores::~dbScores()
{
    //dtor
    if (ScoresDatabase)
    {
        ScoresDatabase->Close;
        delete ScoresDatabase;
        ScoresDatabase = NULL;
    }
}

const wxString theDatabaseFile = wxGetCwd() + wxT("/scores.db");

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Re: Help - wxGrid + wxSQLite3

Post by Auria »

dannyyy wrote:Hi, guys !

I am having a problem to make my program work correctly.

I need to create a grid with the following columns:
Name Score1 Score2 Score3 Score4 Score5 Score6

And each of the lines is an alumn, and his/her scores.

Those scores need to be stored inside a SQLite database, to be compared and filtered later.

The real problem is: I can't understand how to input data on wxGrid, and store each data on wxSQLite3 database.
I'm afraid that your question here is WAY too vague for anyone of us to really help you at this point :?

What parts of that have you managed to do, what specific parts are you stuck on at this time?
I don't see any SQL in the code you posted. Do you know SQL?
etc.
"Keyboard not detected. Press F1 to continue"
-- Windows
dannyyy
In need of some credit
In need of some credit
Posts: 2
Joined: Tue Sep 07, 2010 9:33 pm

Post by dannyyy »

You're right, Auria, I wasn't clear enough. Sorry.

The code above is adopted from the program I'm writing, just a part of it.

What I need to know:
- How to "initialize" the database when the program starts
- How the database file is created
- How to make every new data entered on a new row be commit into the database and thus make the db updated.

I am a SQLite firsttimer, so I don't know how the data entered on the wxGrid can be stored on the database. I hope my questions are not too stupid...I really need help....
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

I don't know the specifics of wxSQLite, but it shouldn't very very different from generic SQLite

http://souptonuts.sourceforge.net/readm ... orial.html

This page shows some example SQL commands to create a table and insert data into it. Do you know SQL? If you don't know SQL, learn it first, you will be very lost otherwise.


EDIT: and looking at http://wxcode.svn.sourceforge.net/viewv ... iew=markup , I imagine something like this :

Code: Select all

wxSQLite3Database::InitializeSQLite(); 
wxSQLite3Database db; 
db.Open(dbFile); 
db.ExecuteUpdate(wxT("create table emp(empno int, empname char(20), salary double);")); 
(disclaimer : I never used wxSQLite3 myself so I don't know if I missed something there)
"Keyboard not detected. Press F1 to continue"
-- Windows
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Post by evstevemd »

Hi,
Check with Zetcodetutorials. It have got SQLite3 tutorial and wxPython tutorial that will give you a glimpse of pythonic/wx dealing of database. The two should give you Idea

And here is a code I adapted to test wxSQLite3. Note it is a way for testing than doing anything useful but it will give you an idea

Code: Select all

#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#include <iostream>
#include <ctime>
#include "wxsqlite3.h"

using namespace std;

int main () {
    wxString dbName(wxT("testdb.db"));
    if (wxFileExists(dbName)) {
        wxRemoveFile(dbName);
    }
    wxSQLite3Database* db = new wxSQLite3Database();
    db->Open(dbName);
    if(db->IsOpen()) {
    cout<<"Succesfully Created!"<<endl;

    if(wxSQLite3Database::HasEncryptionSupport()) {
            cout<<"Passwording.....!"<<endl;
            db->ReKey( wxT("pi3.14"));
        }
        else{
            cout<<"No Encryption Support.....!"<<endl;
        }


        time_t tmStart, tmEnd;
        db->ExecuteUpdate(wxT("create table emp(empno int, empname char(20), salary double);"));
        int nRowsToCreate(50000);
        cout << endl << "Transaction test, creating " << nRowsToCreate;
        cout << " rows please wait..." << endl;
        tmStart = time(0);
        db->Begin();
        int i;
        for (i = 0; i < nRowsToCreate; i++) {
            char buf[128];
            sprintf(buf, "insert into emp values (%d, 'empname%06d', %d.50);", i, i, i);
            db->ExecuteUpdate(buf);
        }

        db->Commit();
        tmEnd = time(0);

    } else {
        cout<<"UnSuccesfully Operation!"<<endl;
    }

    wxString sql = wxT("SELECT empno, empname, salary FROM emp;");
    wxSQLite3ResultSet rs = db->ExecuteQuery(sql);
    int e=0;
    while(rs.NextRow()) {
    cout<<rs.GetAsString(0).mb_str()<<"\t"
        <<rs.GetAsString(1).mb_str()<<"\t"
        <<rs.GetAsString(2).mb_str()<<"\t"<<"\n";
        if(e>11) {
            break;
        }
        e++;
    }
    
    if (wxSQLite3Database::HasBackupSupport())
    {
      cout << endl << "Have Backup and restore database" << endl;
     } 


    return 0;
}
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Post Reply