Qt SQL:如何区分双精度和整型
Qt SQL: how to differentiate between a double and an int?
我在Qt中使用SQLite数据库。当用信息填充QTableWidget时,我想检查作为QVariant接收的值是否为整数,双精度,或字符串:
// query something here, put in in var named 'query'
while(query.next()){
// insert new row
for (int i=0; i<columnCount; i++){
if(query.value(i).toInt()!=0){
//add integer table item with special sorting/formatting
}else if(query.value(i).toDouble()!=0){
//add double table item with special sorting/formatting
}else {
//add standard qt table item
}
}
}
问题是,当使用toInt()时,双精度型被强制转换为整数,反之亦然,这就是为什么所有数值都被视为整数或所有双精度型,这取决于您先测试哪个。
是否有可能正确检查该数字是双精度数还是整数?
要测试QVariant
中存储的确切类型,您可以使用type()函数,然后打开它:
switch(query.value(i).type()) {
case QMetaType::Int:
// ....
break;
case QMetaType::Double:
// ...
break;
}
类型可以在这里找到。
也许@Victor的回答也有它的优点——我不确定QtSQL是如何处理类型的
元数据必须查询sqlite_master
您应该为字段type
运行一个单独的查询,条件name
作为字段名,tbl_name
作为表名。
查看这里可用的字段类型:
SQLite常见问题解答
相关文章:
- C 字符串返回字符串的整数/双精度/长整型值
- 为什么在传递长整型时调用具有两个双精度类型的参数的重载函数?
- 在 C++ 中获取长整型和双精度型模数的准确方法
- 初始化时无法将长整型*转换为双精度*
- Qt SQL:如何区分双精度和整型
- 对整型变量的双精度值-意外输出
- c++中的双精度到整型转换
- g++ abs()在短整型上似乎将其转换为双精度类型
- 两个整型到一个双精度c++
- 将整型转换为双精度
- 将数组整型值转换为双精度值
- 常数表达式可以是双精度类型而不是只能是整型吗?
- 在c++中从文件中读取字符串、整型和双精度的混合
- 生成随机整型和双精度的c++模板类
- 从整型转换为双精度
- 从字符串(如整型或双精度型)中提取数字
- 累加双精度与整型
- 为什么这种双精度到整型的转换不起作用?
- 将双精度指针转换为整型指针
- visual studio 2010 -浮点/双精度随机数的小范围,适用于整型(c++, VS2010)