1.sqllite数据库
获取sqlite数据库的数据表字段的关键是 PRAGMA table_info('tablename')
bool getTableHeader(QSqlQuery &query, QString tableName, QString &header)
{
header.clear();
QString str = QString("PRAGMA table_info('%1')").arg(tableName);
query.prepare(str);
if (query.exec())
{
while (query.next())
{
header += query.value(1).toString();
header += ",";
}
header += "\n";
return true;
}else{
qDebug() << query.lastError();
return false;
}
}
bool outPutTableInfo(QString tabNmae){
QSqlQuery query;
QString strTableNmae = tabNmae;
QString str = "PRAGMA table_info(" + strTableNmae + ")";
query.prepare(str);
if (query.exec())
{
while (query.next())
{
qDebug() << QString(QString::fromLocal8Bit("字段数:%1 字段名:%2 字段类型:%3")).arg(query.value(0).toString()).arg(query.value(1).toString()).arg(query.value(2).toString());
}
return true;
}else{
qDebug() << query.lastError();
return false;
}
}
例子:将db文件转换为CSV文件
void updateTableToCSV()
{
QSqlQuery query(db);
QString header;
QStringList tableList;
tableList <&l