在网上查到在sqlite数据库中,判断某列在表中是否存在的语句,如下.如果该列存在,则返回1;否则返回空.
select 1 from PRAGMA_TABLE_INFO('table_name') WHERE name='column_name'
这个在sqlite中直接运行是可以的,但在qt中运行就会报如下的错误:
near \"(\": syntax error Unable to execute statement
一开始以为是括号的问题,后来意识到是qt中不能够识别"PRAGMA_TABLE_INFO"关键字,也是醉了.后来采用别的方法去解决这个问题.
c++ - Qt/SQL - Get column type and name from table without record - Stack Overflow
QSqlDatabase db = QSqlDatabase::addDatabase("QSLITE", "demo_conn"); //create a db connection
QString strDBPath = "db_path";
db.setDatabaseName(strDBPath); //set the db file
QSqlRecord record = db.record("table_name"); //get the record of the certain table
int n = record.count();
bool bHasField = false;
for(int i = 0; i < n; i++)
{
QString strField = record.fieldName(i);
if(strField.compare("Field1", Qt::CaseInsensitive) == 0)
{
bHasField = true;
break;
}
}
QSqlDatabase::removeDatabase("demo_conn"); //remove the db connection