SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如Tcl、C#、PHP、Java等,还有ODBC接口,同样比起Mysql、PostgreSQL这两款开源世界著名的数据库管理系统来讲,它的处理速度比他们都快。SQLite第一个Alpha版本诞生于2000年5月。 至今已经有12个年头,SQLite也迎来了一个版本SQLite3已经发布。
- SQLite, a lightweight database, is an ACID-compliant relational database management system. Its design goal is embedded, and it has been used in many embedded products. Its footprint is very low.It can support Windows/Linux/Unix and other mainstream operating systems. It can also integrate with many programming languages, such as Tcl, C#, PHP, Java, etc., as well as ODBC interface. It is also faster than Mysql and PostgreSQL, two world-famous open source database management systems.The first Alpha version of SQLite was released in May 2000.It's been 12 years since SQLite came out with a version of SQLite3.
(1)pro file:
QT += core gui sql
(2)init SQLite
/****************************************************************************
** file:
** brief: SQLITE 数据库初始化 包括 数据库的连接与表的创建
** Copyright (C)
** Author:
** E-Mail:
** Version 2.0.1
** Last modified: 2020.07.14
** Modified By: LLL-NEO-Karl
****************************************************************************/
void MainWindow::OnSqliteInit()
{
databaseBook = QSqlDatabase::addDatabase("QSQLITE");
databaseBook.setDatabaseName("./info.db");
if( !databaseBook.open())
{
qDebug() << databaseBook.lastError();
qFatal("Failed to connect.");
save_log.write_log("数据库连接失败!!!");
}
else
{
save_log.write_log("数据库连接成功!!!");
qDebug( "Connected!" );
QSqlQuery *m_pdQuery = new QSqlQuery(databaseBook);
QString strsql = QString::fromLocal8Bit("create table if not exists tempdataInfo( nodenumber varchar, nodecount varchar, grainsize varchar, posinfo varchar, constanttemp varchar, envtemp varchar, graintemp varchar, acctime varchar);");
if(!m_pdQuery->exec(strsql))
{
QSqlError str = m_pdQuery->lastError();
QString strerror = str.text();
QMessageBox::information( NULL, QString::fromLocal8Bit("提示"),strerror);
}
//=====================================================================================
//加载数据库中所有的数据
QSqlQuery query;
int i = 0, j = 0, nColumn, nRow;
query.prepare("select * from tempdataInfo");
query.exec();
query.last();
nRow = query.at() + 1;
ui->tableWidget->setRowCount(nRow);
nColumn = ui->tableWidget->columnCount();
query.first();
while(j < nRow)
{
for (i = 0; i < nColumn; i++)
{
ui->tableWidget->setItem(j, i, new QTableWidgetItem(query.value(i).toString()));
}
j++;
query.next();
}
//设置内容居中显示
for (int n = 0; n < nRow;n++)
{
for (int m = 0; m < nColumn;m++)
{
//如果内容不为空,就设置其内容居中
if(ui->tableWidget->item(n,m)->text()!="")
{
ui->tableWidget->item(n,m)->setTextAlignment(Qt::AlignCenter);
}
}
}
//=====================================================================================
}
}
(3)UI init
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//QTableWidget自适应宽、高
ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
ui->tableWidget->horizontalHeader()->setHidden(false);//设置表头可见
ui->tableWidget->horizontalHeader()->setDisabled(true);//设置列不能拖动
(4)query
if(ui->checkBox_size->isChecked() == true && ui->checkBox_count->isChecked() == false
&& ui->checkBox_ID->isChecked() == false && ui->checkBox_error->isChecked() == false)
{
ui->tableWidget->clearContents();
ui->tableWidget->setRowCount(0);
if(sql_query.exec(QString("select * from tempdataInfo where grainsize='%1'").arg(GrainSize)))
{
while(sql_query.next())
{
OnSerachTabledisplay(sql_query);
}
}
}
I hope I can help you,If you have any questions, please comment on this blog or send me a private message. I will reply in my free time.