Add Qtableview data to the database

Asked

Viewed 287 times

1

Hello. How do I add all the data inside tableView in the database?

I’m adding data to tableView with QStandardItem

//TableView 
count =  ui->tableView->verticalHeader()->count();
verticalHeader.append(QString::number(count + 1));
model.setVerticalHeaderLabels(verticalHeader);

item00 = new QStandardItem(ui->comboBox->currentText());
model.setItem(count, 0, item00);
ui->tableView->setModel(&model);

//insert sqlite
QSqlQuery qry;
qry.prepare("INSERT INTO entrada (entrada_produto) VALUES (?)");
qry.addBindValue(ui->comboBox->currentText());
qry.exec();
  • You should take a look at that link.

  • In the link only tells how to update in the selected field. I want to add all data from tableView in db.

  • No more details of DB is difficult. Put what you have tried, there gives to help better.

  • @Bacco Hello. What I would like is to pick up each row(Row) just a column from the tableView and add to db. DB = | id PRIMARY KEY | lista | I edited the Post with additional code.

  • @Bacco Sqlite. Post updated.

  • @user628298 put a version based on yours, with the query together, if someone read the answer and want the full version.

  • Blz. No problem, you can leave your code :)

Show 2 more comments

2 answers

2

int col = ui->tableView_pedido->verticalHeader()->count();

QSqlQuery qry;
QString pedido;
qry.prepare("INSERT INTO pedidos (pedido) VALUES (:pedido)");

for (int i = 0; i < col ; i++){
   pedido = ui->tableView_pedido->model()->data(
      ui->tableView_pedido->model()->index(i,0)
   ).toString()
   qry.bindValue(":pedido",pedido); // Creio que pode ser tirado do loop também.
   qry.exec();
}
  • (this is the same code as user628298, I added the query itself)

1


Solved:

    int col = ui->tableView_pedido->verticalHeader()->count();

    for (int i = 0; i < col ; ++i){
        QString pedido = ui->tableView_pedido->model()->data(ui->tableView_pedido->model()->index(i,0)).toString()
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.