如果之前不存在特定的列(状态),我想更改表。我看了this link,但我尝试过,但我的查询无效。这是我的代码:
QMutex m_dbMutex;
m_dbcGui = QSqlDatabase::addDatabase("QSQLITE");
QStringList tables = m_dbcGui.tables(QSql::Tables);
checktable(){
bool result = true;
bool result1 = true;
QSqlQuery query(m_dbcGui);
if (result && tables.contains("groups"))
{
m_dbMutex.lock();
m_dbcGui.transaction();
result1 = result && query.prepare("SELECT EXISTS(SELECT NULL FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'groups ' and coloumn_name = 'kid')");
if (result1)
qDebug()<<"***exist kid***";
else{
qDebug()<<"***not exist kid***";
result = result && query.exec("ALTER TABLE 'groups ' ADD COLUMN 'kid");
if (result)
m_dbcGui.commit();
else
m_dbcGui.rollback();
query.finish();
m_dbMutex.unlock();
}
}
else if (result && !tables.contains("groups"))
{
m_dbMutex.lock();
m_dbcGui.transaction();
result = result && query.exec("CREATE TABLE groups ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"name VARCHAR(256) NOT NULL, "
"pid VARCHAR(256) UNIQUE NOT NULL, "
"nickname VARCHAR(256) NOT NULL)"
"state INTEGER NOT NULL DEFAULT 0)");
if (result)
m_dbcGui.commit();
else
m_dbcGui.rollback();
query.finish();
m_dbMutex.unlock();
}
}
checktable();
checktable();
首先,我成功创建了 groups 表,然后我想改变表并添加新列(kid),但它没有创建。有什么建议吗?