0

我正在尝试创建一个构造函数,它将填充来自 sqlite 数据库的数据。当我运行创建对象并填充我的表的 for 循环时,一切似乎都在工作,我得到了一些奇怪的输出。我认为这可能与内存指针有关,但我对 C++ 很陌生,我不确定我做错了什么。

库存构造器

Inventory::Inventory(int id)
{
CC_Number cn;
char result[100];   // array to hold the result.
strcpy(result,"SELECT * FROM Inventory Where Inventory_ID = ");
strcat(result,cn.int_to_char(id));
strcat(result,";");
db = new CC_Database("CC_Prepper_Inventory.sqlite");

vector<vector<string> > r = db->query(result);
db->close();
for(vector<vector<string> >::iterator it = r.begin(); it < r.end(); ++it)
{
    vector<string> row = *it;
    CC_String cs;
        setInv_ID(atoi(cs.toConChar(row.at(0))));
        Item item(atoi(cs.toConChar(row.at(1))));
        setItem(item);
        setQnty(atof(cs.toConChar(row.at(2))));
        Location loc(atoi(cs.toConChar(row.at(3))));
        setLoc(loc);
}

}

物品构造器

Item::Item(int id)
{
CC_Number cn;
char result[100];   // array to hold the result.
strcpy(result,"SELECT * FROM Item Where Item_ID = ");
strcat(result,cn.int_to_char(id));
strcat(result,";");
db = new CC_Database("CC_Prepper_Inventory.sqlite");

vector<vector<string> > r = db->query(result);
db->close();
for(vector<vector<string> >::iterator it = r.begin(); it < r.end(); ++it)
{
    vector<string> row = *it;
    CC_String cs;
        setItem_ID(atoi(cs.toConChar(row.at(0))));

        setCat_ID(atoi(cs.toConChar(row.at(1))));
        setItem_Name(cs.toConChar(row.at(2)));
}

}

位置构造器

Location::Location(int id)
{
CC_Number cn;
char result[100];   // array to hold the result.
strcpy(result,"SELECT Loc_ID, Loc_Name FROM Location Where Loc_ID = ");
strcat(result,cn.int_to_char(id));
strcat(result,";");
db = new CC_Database("CC_Prepper_Inventory.sqlite");

vector<vector<string> > r = db->query(result);
db->close();
for(vector<vector<string> >::iterator it = r.begin(); it < r.end(); ++it)
{
    vector<string> row = *it;
    CC_String cs;
        setLoc_ID(id);
        setLoc_Name(cs.toConChar(row.at(1)));
}
}

类别构造函数

Category::Category(int id)
{
CC_Number cn;
char result[100];   // array to hold the result.
strcpy(result,"SELECT * FROM Category Where Cat_ID = ");
strcat(result,cn.int_to_char(id));
strcat(result,";");
db = new CC_Database("CC_Prepper_Inventory.sqlite");

vector<vector<string> > r = db->query(result);
db->close();
for(vector<vector<string> >::iterator it = r.begin(); it < r.end(); ++it)
{
    vector<string> row = *it;
    CC_String cs;
        setCat_ID(id);
        setCat_Name(cs.toConChar(row.at(1)));
}
}

用于调用构造函数的 For 循环

void MainWindow::fillTable()
{
ui->mainTable->clear();
db = new CC_Database("CC_Prepper_Inventory.sqlite");
vector<vector<string> > result = db->query("SELECT Inventory_ID FROM Inventory;");
db->close();
ui->mainTable->setRowCount(result.size());
int j = 0;
for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it)
{
    vector<string> row = *it;
    Inventory i(atoi(cs.toConChar(row.at(0))));

    cout << i.getItem().getItem_Name() << endl;
    cout << i.getItem().getCategory().getCatName() << endl;
    cout << cn.dbl_to_char(i.getQnty()) << endl;
    cout << i.getLoc().getLocName() << endl;

    QTableWidgetItem *newItem1 = new QTableWidgetItem(QString::fromStdString(i.getItem().getItem_Name()));
    QTableWidgetItem *newItem2 = new QTableWidgetItem(QString::fromStdString(i.getItem().getCategory().getCatName()));
    QTableWidgetItem *newItem3 = new     QTableWidgetItem(QString::fromStdString(cn.dbl_to_char(i.getQnty())));
    QTableWidgetItem *newItem4 = new     QTableWidgetItem(QString::fromStdString(i.getLoc().getLocName()));
    ui->mainTable->setItem(j,1,newItem1);
    ui->mainTable->setItem(j,2,newItem2);
    ui->mainTable->setItem(j,3,newItem3);
    ui->mainTable->setItem(j,4,newItem4);
    i.clear();
    j++;
}

QStringList qsl;
qsl << "Item" << "Category" << "Quantity" << "Location";
ui->mainTable->setHorizontalHeaderLabels(qsl);
}

这是我计算结果时的输出。

Food
1
B.O.L.

Food
3
B.O.L.

Food
4

0�an error
Food
3.14
LE Item (
Item_ID INTEGER PRIMARY KEY AUTOINCREMENT,
Cat_ID INTEGER NOT NULL,
Item_Name TEXT NOT NULL)P++Ytablesqlite_sequencesqlite_sequenceCREATE TABLE sqlite_sequence(name,seq)� ��

Food
5    

我知道这是信息过载,但我不知道如何解释我的问题。

4

1 回答 1

0

我相信问题不在于您发布的线路,而在于其他地方。但是,您的代码要求进行一些修改:

  1. 不要使用字符数组来保存 SQL 查询,使用 std::string

  2. 不要为每个查询打开与数据库的连接...打开一次并执行多个查询

于 2013-06-20T19:03:32.063 回答