當我從組合框中選擇書籍編號時,我想將書名的值加載到文本框中。首先,我編寫了將Book Ids從數據庫加載到組合框的代碼,並且它完美加載。這是方法。qt C++中鏈接Qline編輯組合框
void FictionSection::loadComboVal()
{
DatabaseConnection mydb;
QSqlQueryModel *modl = new QSqlQueryModel();
dbConOpen();
QSqlQuery *query = new QSqlQuery(mydb.db) ;
query->prepare(" select material_id from fiction ");
bool flag = query->exec();
//assigning the values to a QTableView
if(flag == true)
{
modl->setQuery(*query);
ui->cmbxId->setModel(modl);
}
mydb.dbConClose();
}
然後我寫的代碼分配值文本框一旦ID是從combobox.This選擇這本書是代碼段。
// loading book names once user select the book id from combo box
void FictionSection::on_cmbxId_currentIndexChanged(int index)
{
QString value = ui->cmbxId->currentText();
int id;
if(!value.isEmpty())
{
id = value.toInt();
}
//Loading book table values to a table
dbC onOpen();
QSqlQuery query ;
QString ids = QString("values('") + QString::number(id);
query.prepare(" select material_title from book where material_id = '"+ids+"'");
bool flag = query.exec();
//assigning the values to a QTableView
if(flag == true)
{
ui->lneditFicNme->setText(query.value(1).toString());
}
dbConClose();
}
但隨後裝載值,組合框是不可見的,如我所料,我不能得到任何文本框的值。但沒有編譯或運行時錯誤。書本ID以數據庫中的varchar形式存儲爲int和書名。請幫我找到錯誤。先謝謝你。
我改變了代碼如下
void FictionSection::on_cmbxId_currentIndexChanged(int index)
{
QString value = ui->cmbxId->currentText();
int id;
if(!value.isEmpty())
{
id = value.toInt();
}
//Loading book table values to a table
dbConOpen();
QSqlQuery query ;
// QString ids = QString("values('") + QString::number(id);
query.prepare(" select material_title from book where material_id = ?");
query.bindValue(0,id);
bool flag = query.exec();
//assigning the values to a QTableView
if(flag == true)
{
ui->lneditFicNme->setText(query.value(1).toString());
}
else
{
QMessageBox :: critical(this,"Error",query.lastError().text());
}
dbConClose();
}
然後如下
QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
QSqlQuery::value: not positioned on a valid record
QSqlQuery::value: not positioned on a valid record
我編輯了上面的代碼,但我仍然無法得到預期的結果給出了一個錯誤。
void FictionSection::on_cmbxId_currentIndexChanged(int index)
{
DatabaseConnection con;
con.dbConOpen();
QString value = ui->cmbxId->currentText();
int id;
if(!value.isEmpty())
{
id = value.toInt();
}
//Loading book table values to a table
QSqlQuery query ;
// QString ids = QString("values('") + QString::number(id);
query.prepare(" select material_title from book where material_id = :id");
query.bindValue(":id",id);
bool flag = query.exec();
//assigning the values to a QTableView
if(flag == true)
{
while(query.next())
{
ui->lneditFicNme->setText(query.value(1).toString());
}
}
else
{
QMessageBox :: critical(this,"Error",query.lastError().text());
}
con.dbConClose();
}
請幫我解決這個問題。提前致謝
關閉大括號的'values('丟失..如果'flag!= true'您應該檢查db錯誤 – Aaron
另外我還記得在向db中添加值時使用'values()'。但是如果這個查詢適合你,那就沒問題了,最好是調試出最後的查詢 – Aaron
我不知道它是否正確關閉它,當它關閉時它會給出一個錯誤 – user3279893