maolike

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

example:

	QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
	db.setHostName("localhost");
	db.setDatabaseName("D:/TEST.pro");
	bool ok = db.open();
	if (!ok)
	{
		QSqlError err = db.lastError();
		QString dbtxt = err.databaseText();
		QString drtxt = err.driverText();
		QString alltxt = err.text();
	}

	db.transaction();

	QSqlQuery query(db);
	bool bok = query.exec("create table TESTDB"
		"("
		"PersonId intege,"
		"Age intege"
		"); ");
	if (!bok)
	{
		QSqlError err = db.lastError();
		QString dbtxt = err.databaseText();
		QString drtxt = err.driverText();
		QString alltxt = err.text();
	}
	query.prepare("insert into TESTDB(PersonId,Age)"
		"values(?,?)");
	query.bindValue(0, 111);
	query.bindValue(1, 22);
	bok = query.exec();
	if (!bok)
	{
		QSqlError err = db.lastError();
		QString dbtxt = err.databaseText();
		QString drtxt = err.driverText();
		QString alltxt = err.text();
	}
	db.commit();

	db.close();

  验证方法:GOOGLE插件SQLite Reader;插件可以执行SQL语句直接看到内容

 

posted on 2020-02-21 11:12  maolike  阅读(1003)  评论(2编辑  收藏  举报