opencv_mv/QMainPro/QMacVisual/QWorld/SQLite.cpp

239 lines
4.2 KiB
C++
Raw Normal View History

2023-05-22 09:27:46 +00:00
#include "QWorld.h"
#include <QString>
#pragma region SQLite
#pragma region <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SQLite<74><65><EFBFBD>ݿ<EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SQLite<74><65><EFBFBD>ݿ<EFBFBD>
bool SQLite::CreateConnectDB(QString key, QString sql_name, QSqlDatabase &database)
{
if (key != "59226f4700f1cc8a5de755d501af308d2d2")
{
return false;
}
try
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD>
database = QSqlDatabase::addDatabase("QSQLITE");
database.setDatabaseName(sql_name);
bool bol_sql_open = database.open();
if (bol_sql_open == false)
{
QMessageBox::critical(NULL, "Prompt", database.lastError().text());
return false;
}
else
{
return true;
}
}
catch (...)
{
return false;
}
}
#pragma endregion
#pragma region <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bool SQLite::CreateTable(QString key, QString table_name)
{
if (key != "59226f4700f1cc8a5de755d501af308d2d2")
{
return false;
}
try
{
QSqlQuery sql_query;
QString sql = "create table " + table_name + "(USERNAME text, PASSWORD text)";
if (!sql_query.exec(sql))
{
QMessageBox::critical(NULL, "Prompt", sql_query.lastError().text());
return false;
}
else
{
return true;
}
}
catch (...)
{
return false;
}
}
#pragma endregion
#pragma region <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bool SQLite::InsertData(QString key, QString table_name, QString user_name, QString password)
{
if (key != "59226f4700f1cc8a5de755d501af308d2d2")
{
return false;
}
try
{
QSqlQuery sql_query;
QString sql = "insert into " + table_name + " values (?, ?)";
sql_query.prepare(sql);
sql_query.addBindValue(user_name);
sql_query.addBindValue(password);
if (!sql_query.exec())
{
QMessageBox::critical(NULL, "Prompt", sql_query.lastError().text());
return false;
}
else
{
return true;
}
}
catch (...)
{
return false;
}
}
#pragma endregion
#pragma region <20>޸<EFBFBD><DEB8><EFBFBD><EFBFBD><EFBFBD>
//<2F>޸<EFBFBD><DEB8><EFBFBD><EFBFBD><EFBFBD>
bool SQLite::ModifyData(QString key, QString user_name, QString password)
{
if (key != "59226f4700f1cc8a5de755d501af308d2d2")
{
return false;
}
try
{
QSqlQuery sql_query;
QString sql = "update LoginTable set PASSWORD = :PASSWORD where USERNAME = :USERNAME";
sql_query.prepare(sql);
sql_query.bindValue(":USERNAME", user_name);
sql_query.bindValue(":PASSWORD", password);
if (!sql_query.exec())
{
QMessageBox::critical(NULL, "Prompt", sql_query.lastError().text());
return false;
}
else
{
return true;
}
}
catch (...)
{
return false;
}
}
#pragma endregion
#pragma region <20><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD>
//<2F><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD>
bool SQLite::QueryData(QString key, QString user_name, QString &password)
{
if (key != "59226f4700f1cc8a5de755d501af308d2d2")
{
return false;
}
try
{
//eg: "select * from LoginTable where USERNAME == 'user0';"
QSqlQuery sql_query("select * from LoginTable where USERNAME == '" + user_name + "';");
if (!sql_query.exec())
{
QMessageBox::critical(NULL, "Prompt", sql_query.lastError().text());
return false;
}
else
{
while (sql_query.next())
{
password = sql_query.value(1).toString();
}
return true;
}
}
catch (...)
{
return false;
}
}
#pragma endregion
#pragma region ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bool SQLite::DeleteData(QString key, QString user_name)
{
if (key != "59226f4700f1cc8a5de755d501af308d2d2")
{
return false;
}
try
{
QSqlQuery sql_query("delete from LoginTable where USERNAME == '" + user_name + "';");
if (!sql_query.exec())
{
QMessageBox::critical(NULL, "Prompt", sql_query.lastError().text());
return false;
}
else
{
return true;
}
}
catch (...)
{
return false;
}
}
#pragma endregion
#pragma region ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bool SQLite::DeleteTable(QString key, QString table_name)
{
if (key != "59226f4700f1cc8a5de755d501af308d2d2")
{
return false;
}
try
{
QSqlQuery sql_query;
sql_query.exec("drop table " + table_name);
if (sql_query.exec())
{
QMessageBox::critical(NULL, "Prompt", sql_query.lastError().text());
return false;
}
else
{
return true;
}
}
catch (...)
{
return false;
}
}
#pragma endregion
#pragma region <20>ر<EFBFBD><D8B1><EFBFBD><EFBFBD>ݿ<EFBFBD>
//<2F>ر<EFBFBD><D8B1><EFBFBD><EFBFBD>ݿ<EFBFBD>
bool SQLite::CloseDB(QString key, QSqlDatabase database)
{
if (key != "59226f4700f1cc8a5de755d501af308d2d2")
{
return false;
}
try
{
database.close();
return true;
}
catch (...)
{
return false;
}
}
#pragma endregion
#pragma endregion