5
I’m calling a method another class after entering data. But it is returning an Sqlite error:
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.
Code:
for example_insert.cpp
void DialogVenda::inserte_venda(){
//QSqlQuery etc....
if (qry.lastInsertId()>0){
QMessageBox::information(this,"Cadastro de pedido", "Pedido cadastrado com sucesso.");
MainPrincipal *pt= new MainPrincipal(this);
pt->tableView_listaVendas();
}
}
Mainprincipal.cpp
MainPrincipal::MainPrincipal(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainPrincipal){
ui->setupUi(this);
base = new connection(NAME_BASE);
if(!base->openBD()){
QMessageBox::critical(this, "Erro", ERRO_BASE);
return;
}
}
void MainPrincipal::tableView_listaVendas(){
model = new QSqlQueryModel;
model->setQuery("SELEC * FROM empresa_estoqueSaida WHERE strftime('%Y-%m-%d', saida_dataCadastro)='"+dateTime.toString("yyyy-MM-dd")+"'");
model->setHeaderData(0, Qt::Horizontal, tr("Cliente"));
model->setHeaderData(1, Qt::Horizontal, tr("Endereço"));
model->setHeaderData(2, Qt::Horizontal, tr("Valor"));
model->setHeaderData(3, Qt::Horizontal, tr("Pagamento"));
model->setHeaderData(6, Qt::Horizontal, tr("Data"));
ui->tableView_vendas->setModel(model);
}
The goal is to update the tableView_vendas
after the insertion of the sale.
You are opening more than one instance of the database connection in your application?
– Dherik
I have in the Mainmain builder
base_dados = new Conexao(NOME_BASE_DADOS);
– Hy-brazil
The connection is once in
MainPrincipal::MainPrincipal(){base_dados = new Conexao(NOME_BASE_DADOS);}
. You thought the problem was opening upnew MainPrincipal
he’s running around all class again.– Hy-brazil
So that’s it. This connection needs to be opened and exist only once in the application, right?
– Dherik
Yes. Look at the top of the code, I added the constructor that has the database link.
– Hy-brazil
Try using thread lock
– Lucas Henrique