apoorv569
apoorv569

Reputation: 163

wxWidgets - How to not push back anything to a wxVector?

I have a application in which user can insert files into wxDataViewListCtrl, and have a database to store all the information about the files. And I have a function called LoadDatabase() to the load the database back to application, when user closes, and relaunches the application, which looks like this,

wxVector<wxVector<wxVariant>> Database::LoadDatabase(wxVector<wxVector<wxVariant>>& vecSet, wxTreeCtrl& favorite_tree, wxTreeItemId& favorite_item, wxTreeCtrl& trash_tree, wxTreeItemId& trash_item)
{
    try
    {
        if (sqlite3_open("Samples.db", &m_Database) != SQLITE_OK)
        {
            wxLogDebug("Error opening DB");
            throw sqlite3_errmsg(m_Database);
        }

        std::string load = "SELECT FAVORITE, FILENAME, SAMPLEPACK, TYPE, \
                            CHANNELS, LENGTH, SAMPLERATE, BITRATE, TRASHED \
                            FROM SAMPLES;";

        rc = sqlite3_prepare_v2(m_Database, load.c_str(), load.size(), &m_Stmt, NULL);

        if (rc == SQLITE_OK)
        {
            int row = 0;

            while (SQLITE_ROW == sqlite3_step(m_Stmt))
            {
                wxLogDebug("Record found, fetching..");

                int c1 = sqlite3_column_int(m_Stmt, 0);
                wxString c2 = wxString(std::string(reinterpret_cast< const char* >(sqlite3_column_text(m_Stmt, 1))));
                wxString c3 = wxString(std::string(reinterpret_cast< const char* >(sqlite3_column_text(m_Stmt, 2))));
                wxString c4 = std::string(reinterpret_cast<const char *>(sqlite3_column_text(m_Stmt, 3)));
                int c5 = sqlite3_column_int(m_Stmt, 4);
                int c6 = sqlite3_column_int(m_Stmt, 5);
                int c7 = sqlite3_column_int(m_Stmt, 6);
                int c8 = sqlite3_column_int(m_Stmt, 7);
                int c9 = sqlite3_column_int(m_Stmt, 8);

                wxVector<wxVariant> vec;

                if (c1 == 1)
                {
                    vec.push_back(true);
                    vec.push_back(c2);
                    vec.push_back(c3);
                    vec.push_back(c4);
                    vec.push_back(wxString::Format("%d",c5));
                    vec.push_back(wxString::Format("%d",c6));
                    vec.push_back(wxString::Format("%d",c7));
                    vec.push_back(wxString::Format("%d",c8));

                    favorite_tree.AppendItem(favorite_item, c2);
                }
                else if (c9 == 1)
                {
                    vec.push_back(false);
                    vec.push_back(c2);
                    vec.push_back(c3);
                    vec.push_back(c4);
                    vec.push_back(wxString::Format("%d",c5));
                    vec.push_back(wxString::Format("%d",c6));
                    vec.push_back(wxString::Format("%d",c7));
                    vec.push_back(wxString::Format("%d",c8));

                    trash_tree.AppendItem(trash_item, c2);
                }
                else
                {
                    vec.push_back(false);
                    vec.push_back(c2);
                    vec.push_back(c3);
                    vec.push_back(c4);
                    vec.push_back(wxString::Format("%d",c5));
                    vec.push_back(wxString::Format("%d",c6));
                    vec.push_back(wxString::Format("%d",c7));
                    vec.push_back(wxString::Format("%d",c8));
                }

                vecSet.push_back(vec);

                row++;
            }
        }
        else
        {
            wxMessageDialog* msgDialog = new wxMessageDialog(NULL, "Error! Cannot load data from table.", "Error", wxOK | wxICON_ERROR);
            msgDialog->ShowModal();
            sqlite3_free(m_ErrMsg);
        }

        rc = sqlite3_finalize(m_Stmt);

        sqlite3_close(m_Database);
    }
    catch (const std::exception &exception)
    {
        wxLogDebug(exception.what());
    }

    return vecSet;
}

What I want to do is, when c9 == 1 don't push back anything to the vector. But if I don't push back to the vector, i.e comment all the lines in the else if statement, except trash_tree.AppendItem(trash_item, c2); then it gives me error about index.

EDIT: I call this LoadDatabase() as,

void Browser::RestoreDatabase()
{
    try
    {
        wxVector<wxVector<wxVariant>> dataset;

        if (db.LoadDatabase(dataset, *m_CollectionView, rootNode, *m_TrashedItems, trash_root_node).empty())
        {
            wxLogDebug("Error! Database is empty.");
        }
        else
        {
            for (auto data : dataset)
            {
                m_SampleListView->AppendItem(data);
            }
        }
    }
    catch (...)
    {
        std::cerr << "Error loading data." << std::endl;
    }
}

SampleListView here is wxDataViewListCtrl, I want that when c9 == 1 don't return anything, just append that item's filename to trash_tree. Because if I push back anything to vec it will get added to SampleListView.

This is the error I get, enter image description here

Upvotes: 0

Views: 266

Answers (2)

apoorv569
apoorv569

Reputation: 163

I modified the code according to the one you shared above,

wxVector<wxVector<wxVariant>> Database::LoadDatabase(wxVector<wxVector<wxVariant>>& vecSet, wxTreeCtrl& favorite_tree, wxTreeItemId& favorite_item, wxTreeCtrl& trash_tree, wxTreeItemId& trash_item)
{
    try
    {
        if (sqlite3_open("Samples.db", &m_Database) != SQLITE_OK)
        {
            wxLogDebug("Error opening DB");
            throw sqlite3_errmsg(m_Database);
        }

        std::string load = "SELECT FAVORITE, FILENAME, SAMPLEPACK, TYPE, \
                            CHANNELS, LENGTH, SAMPLERATE, BITRATE, TRASHED \
                            FROM SAMPLES;";

        rc = sqlite3_prepare_v2(m_Database, load.c_str(), load.size(), &m_Stmt, NULL);

        if (rc == SQLITE_OK)
        {
            int row = 0;

            while (SQLITE_ROW == sqlite3_step(m_Stmt))
            {
                wxLogDebug("Record found, fetching..");

                int c1 = sqlite3_column_int(m_Stmt, 0);
                wxString c2 = wxString(std::string(reinterpret_cast< const char* >(sqlite3_column_text(m_Stmt, 1))));
                wxString c3 = wxString(std::string(reinterpret_cast< const char* >(sqlite3_column_text(m_Stmt, 2))));
                wxString c4 = std::string(reinterpret_cast<const char *>(sqlite3_column_text(m_Stmt, 3)));
                int c5 = sqlite3_column_int(m_Stmt, 4);
                int c6 = sqlite3_column_int(m_Stmt, 5);
                int c7 = sqlite3_column_int(m_Stmt, 6);
                int c8 = sqlite3_column_int(m_Stmt, 7);
                int c9 = sqlite3_column_int(m_Stmt, 8);

                wxVector<wxVariant> vec;

                if (c9 == 1)
                {
                    trash_tree.AppendItem(trash_item, c2);
                }
                else
                {
                    if (c1 == 1)
                    {
                        vec.push_back(true);

                        favorite_tree.AppendItem(favorite_item, c2);
                    }
                    else
                        vec.push_back(false);

                    vecSet.push_back(vec);
                    vec.push_back(c2);
                    vec.push_back(c3);
                    vec.push_back(c4);
                    vec.push_back(wxString::Format("%d", c5));
                    vec.push_back(wxString::Format("%d", c6));
                    vec.push_back(wxString::Format("%d", c7));
                    vec.push_back(wxString::Format("%d", c8));
                }

                row++;
            }
        }
        else
        {
            wxMessageDialog* msgDialog = new wxMessageDialog(NULL, "Error! Cannot load data from table.", "Error", wxOK | wxICON_ERROR);
            msgDialog->ShowModal();
            sqlite3_free(m_ErrMsg);
        }

        rc = sqlite3_finalize(m_Stmt);

        sqlite3_close(m_Database);
    }
    catch (const std::exception &exception)
    {
        wxLogDebug(exception.what());
    }

    return vecSet;
}

It gives me this error,

enter image description here

Also why pushing back to vecSet before pushing back the other items to vec.

EDIT:

After moving the line vecSet.push_back(vec) after we push everything to vec first, it seems to do what I was intending it to do.

                wxVector<wxVariant> vec;

                if (c9 == 1)
                {
                    trash_tree.AppendItem(trash_item, c2);
                }
                else
                {
                    if (c1 == 1)
                    {
                        vec.push_back(true);

                        favorite_tree.AppendItem(favorite_item, c2);
                    }
                    else
                        vec.push_back(false);

                    vec.push_back(c2);
                    vec.push_back(c3);
                    vec.push_back(c4);
                    vec.push_back(wxString::Format("%d", c5));
                    vec.push_back(wxString::Format("%d", c6));
                    vec.push_back(wxString::Format("%d", c7));
                    vec.push_back(wxString::Format("%d", c8));

                    vecSet.push_back(vec);
                }

So I guess its fixed now.

Upvotes: 1

Devolus
Devolus

Reputation: 22094

Not sure I understand your question right, but do you mean like this?

wxVector<wxVariant> vec;

if (c9 == 1)
{
    trash_tree.AppendItem(trash_item, c2);
}
else
{
    if (c1 == 1)
    {
        vec.push_back(true);

        favorite_tree.AppendItem(favorite_item, c2);
    }
    else
        vec.push_back(false);

    vecSet.push_back(vec);
    vec.push_back(c2);
    vec.push_back(c3);
    vec.push_back(c4);
    vec.push_back(wxString::Format("%d",c5));
    vec.push_back(wxString::Format("%d",c6));
    vec.push_back(wxString::Format("%d",c7));
    vec.push_back(wxString::Format("%d",c8));
}

Upvotes: 2

Related Questions