Hi, how do I retrieve certain content like name from a table? I want to retrieve all the names and then set them to a label in my form. For now I just want to how to get all the names. So far I've tried using a select count query to first get the number of rows but it didn't work. I'm not sure what functions to use for this if anyone can please help.
Here's what I have done:
db.setDatabaseName(Path_to_DB);
if(checkFile.isFile())
{
if(db.open())
{
qDebug() << "Connected to database file";
}
}else{
qDebug() << "Database file not found";
}
query.prepare(qry);
if (query.exec())
{
int count = query.result(); //<<<Where I'm stuck
qDebug() << count;
for ( int i = 0; i < count; i++)
{
//create labels for each customer
Ui::MainWindow *ui;
ui->gridLayout->addWidget(label,0,0);
ui->gridLayout->addWidget(chkbox,0,1);
}
}
else
{
query.lastError();
}
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(Path_to_DB);
QFileInfo checkFile(Path_to_DB);
if(checkFile.isFile())
{
if(db.open())
{
qDebug() << "Connected to database file";
}
}else{
qDebug() << "Database file not found";
}
QSqlQuery query;
QString qry = QString("SELECT Count(*) FROM customers");
query.prepare(qry);
if (query.exec())
{
int count = query.result(); //<<<Where I'm stuck
qDebug() << count;
for ( int i = 0; i < count; i++)
{
//create labels for each customer
QLabel *label = new QLabel(QString());
QCheckBox *chkbox = new QCheckBox;
Ui::MainWindow *ui;
ui->gridLayout->addWidget(label,0,0);
ui->gridLayout->addWidget(chkbox,0,1);
}
}
else
{
query.lastError();
}
To copy to clipboard, switch view to plain text mode
Thanks in advance!
Bookmarks