gpt4 book ai didi

c++ - 大型 Qt Sqlite SELECT 语句

转载 作者:行者123 更新时间:2023-11-28 07:27:11 25 4
gpt4 key购买 nike

我有一个数据库,其中的表包含约 1.5 亿行。这些列只是:

id (INTEGER), value_one (INTEGER), value_two (INTEGER), value_3 (INTEGER)

我需要将所有这些数据导入到 QList 中,但我遇到了一个问题,Qt 断言 qAllocMore: 'Requested size is too large!', file tools\qbytearray.cpp,第 73 行 当我运行 SELECT 查询时。我能够在包含约 700 万个条目的表上运行相同的代码,并且它可以正常工作。

这是我的SELECT语句:

 bool e = query.exec("SELECT * FROM DocumentTerms");
if (!e) {
qWarning() << __FUNCTION__ << "\tError: " << query.lastError().text();
}
while (query.next()) {
int docId = query.value(1).toInt();
int termId = query.value(2).toInt();
int frequency = query.value(3).toInt();
//store it in a QHash<int, QPair<int, int>>
}

看起来它正在遍历 query.next 循环,但断言在约 1600 万次迭代后弹出。知道是什么原因造成的吗?

最佳答案

我之前的回答是废话。愚蠢的计算错误。但是,我想我现在有了解决方案。这不是一般的内存,你错过了什么,而是连续的内存。

我尝试了以下方法:

QList<int> testlist;
for(int i = 0; i < 150000000;++i){
testlist << i << i << i << i;
}

愚蠢的小代码,什么都不做,只是将 4 个整数 150000000 次放入列表中。几秒钟后我得到:

terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc

或多或少:内存不足。

现在我更改上面的代码:

QList<int> testlist;
testList.reserve(150000000*4);
for(int i = 0; i < 150000000;++i){
testlist << i << i << i << i;
}

这段代码与之前的代码没有任何关系。 QList 的大小与之前完全相同。但是,我在开始循环之前保留所有内存。结果?该列表不需要增长并不断请求更多内存。有了这个版本,我一点问题都没有。我得到了我的名单。

关于c++ - 大型 Qt Sqlite SELECT 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18563985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com