C++与Qt实现MySQL数据库的增删改查

头文件

MySQL数据库的安装与环境配置,网上大把,可参考:
https://www.cnblogs.com/xtu-wlf1212/p/8764015.html
头文件包含:

#include 
#include 
#include 
#include 

demo一览

具体Demo实例:https://download.csdn.net/download/birenxiaofeigg/11244437
C++与Qt实现MySQL数据库的增删改查_第1张图片

数据库连接,增删改查实现:

连接

void TestSql::on_pbtConnect_clicked()
{
	ui.tbShow->append(QString::fromLocal8Bit("数据库连接中......"));

	testDB = QSqlDatabase::addDatabase("QMYSQL");
	testDB.setHostName("localhost");//127.0.0.1
	testDB.setPort(3306);
	testDB.setUserName("root");
	testDB.setPassword("123456");

	testDB.open();
	if (!testDB.open())
	{
		ui.tbShow->append(QString::fromLocal8Bit("MySql数据库连接失败!"));
	}
	else
	{
		ui.tbShow->append(QString::fromLocal8Bit("MySql数据库连接成功!"));

		ui.pbtExit->setEnabled(true);
		ui.pbtFindAll->setEnabled(true);

		ui.pbtAdd->setEnabled(true);
		ui.pbtChange->setEnabled(true);
		ui.pbtMove->setEnabled(true);
		ui.pbtFind->setEnabled(true);
	}
}

增加

void TestSql::on_pbtAdd_clicked()
{
	uint id = ui.leID1->text().toUInt(0, 10);
	QString name = ui.leName1->text();
	uint age = ui.leAge1->text().toUInt();
	QString borndate = ui.leBorn1->text();

	QSqlQuery* query = new QSqlQuery(testDB);
	QString strCmd = QString("insert into manage.testForm values(%1,'%2',%3,'%4')")
		.arg(id)
		.arg(name)
		.arg(age)
		.arg(borndate);

	query->prepare(strCmd);
	if (query->exec())
	{
		ui.tbShow->append(QString::fromLocal8Bit("testForm表,数据添加成功!"));
		ui.leID1->setText(QString::number(id + 1));
	}
	else
	{
		ui.tbShow->append(QString::fromLocal8Bit("数据添加失败,请检查!"));
	}

}

删除

void TestSql::on_pbtMove_clicked()
{
	uint id = ui.leID3->text().toUInt();
	QString name = ui.leName3->text();
	uint age = ui.leAge3->text().toUInt();
	QString dateTime = ui.leBorn3->text();

	QSqlQuery* query = new QSqlQuery(testDB);
	QString strCmd;
	if (id)
		strCmd = QString("delete from manage.testForm where ID=%1").arg(id);
	else if (!name.isEmpty())
		strCmd = QString("delete from manage.testForm where Name='%1'").arg(name);
	else if (age)
		strCmd = QString("delete from manage.testForm where Age=%1").arg(age);
	else if (!dateTime.isEmpty())
		strCmd = QString("delete from manage.testForm where BornDate='%1'").arg(dateTime);
	else
	{
		ui.tbShow->append(QString::fromLocal8Bit("数据为空,删除失败"));
		return;
	}


	query->prepare(strCmd);
	if (query->exec())
		ui.tbShow->append(QString::fromLocal8Bit("删除成功!"));
	else
		ui.tbShow->append(QString::fromLocal8Bit("出现意外,删除失败!"));
}

更改

void TestSql::on_pbtChange_clicked()
{
	uint id = ui.leID4->text().toUInt();
	QString name = ui.leName4->text();
	uint age = ui.leAge4->text().toUInt();
	QString dateTime = ui.leBorn4->text();

	QSqlQuery* query = new QSqlQuery(testDB);
	QString strCmd;
	if (id)
	{
		if (!name.isEmpty())
		{
			strCmd = QString("update manage.testForm set Name='%1' where ID='%2'")
					.arg(name).arg(id);
			query->prepare(strCmd);
			if (query->exec())
				ui.tbShow->append(QString::fromLocal8Bit("Name更改成功!"));
			else
				ui.tbShow->append(QString::fromLocal8Bit("Name更改失败!"));
		}			
		if (age)
		{
			strCmd = QString("update manage.testForm set Age=%1 where ID='%2'")
				.arg(age).arg(id);
			query->prepare(strCmd);
			if (query->exec())
				ui.tbShow->append(QString::fromLocal8Bit("Age更改成功!"));
			else
				ui.tbShow->append(QString::fromLocal8Bit("Age更改失败!"));
		}
			
		if (!dateTime.isEmpty())
		{
			strCmd = QString("update manage.testForm set BornDate='%1' where ID='%2'")
				.arg(dateTime).arg(id);
			query->prepare(strCmd);
			if (query->exec())
				ui.tbShow->append(QString::fromLocal8Bit("BornDate更改成功!"));
			else
				ui.tbShow->append(QString::fromLocal8Bit("BornDate更改失败!"));
		}
			
	}	
	else
	{
		ui.tbShow->append(QString::fromLocal8Bit("ID为空,改动失败"));
		return;
	}
	
}

查询

void TestSql::on_pbtFind_clicked()
{
	uint id = ui.leID2->text().toUInt();
	QString name = ui.leName2->text();
	uint age = ui.leAge2->text().toUInt();
	QString dateTime = ui.leBorn2->text();

	QSqlQuery* query = new QSqlQuery(testDB);
	QString strCmd;
	if (id)// ID唯一
	{
		strCmd = QString("select * from manage.testForm where ID=%1").arg(id);
		query->prepare(strCmd);
		if (query->exec())
		{
			ui.tbShow->append(QString::fromLocal8Bit("ID查询成功!"));
			ui.tbShow->append(QString::fromLocal8Bit("根据ID查询到的记录为:"));
			......
			......
			......
	}

}

实现结果:

连接-查询全部-断开

C++与Qt实现MySQL数据库的增删改查_第2张图片

数据库增加

C++与Qt实现MySQL数据库的增删改查_第3张图片

数据库查询

C++与Qt实现MySQL数据库的增删改查_第4张图片

数据库删除

C++与Qt实现MySQL数据库的增删改查_第5张图片

数据库更改

C++与Qt实现MySQL数据库的增删改查_第6张图片
本样例下载连接:https://download.csdn.net/download/birenxiaofeigg/11244437

你可能感兴趣的:(C++,数据库)