1,将sqlite.h和sqlite.lib放到你的工程中
2,在.pro中加上LIBS +=E:\qtCore\day07\Button\sqlite3.lib
bool Button::CreateSqliteDB_Type(QString DBName, QString TableName, vector<QString>& field_str)
{
QString str="";
int rc(1);
char * err_msg = nullptr;
const char* tempDBName=DBName.toStdString().c_str();
rc = sqlite3_open(tempDBName, &m_pDB);// 打开数据库,不存在时创建
if (rc)
{
sqlite3_close(m_pDB); //打开失败!
QMessageBox::warning(nullptr,"warning","Create DB 数据库打开失败,请检查后再操作!",QMessageBox::Yes|QMessageBox::No);
return false;
}
const char* sqlcode;
char **resultp = nullptr;
int nrow(0), ncolumn(0);
string sqlStr="select name from sqlite_master where name = '"+TableName.toStdString()+"'";
sqlcode=sqlStr.c_str();
rc = sqlite3_get_table(m_pDB, sqlcode, &resultp, &nrow, &ncolumn, &err_msg);
OutputDebugStringA(sqlcode);
if (rc == 0 && nrow > 0)
{
str= "已经存在" + TableName;
OutputDebugStringA(str.toStdString().c_str());
if (resultp != nullptr) {
sqlite3_free_table(resultp);
resultp = nullptr;
}
if (err_msg != nullptr) {
sqlite3_free(err_msg);
err_msg = nullptr;
}
sqlite3_close(m_pDB);
return true;
}
if (resultp != nullptr) {
sqlite3_free_table(resultp);
resultp = nullptr;
}
if (err_msg != nullptr) {
sqlite3_free(err_msg);
err_msg = nullptr;
}
strcpy(SQL_CREATE_TABLE, "Create table if not exists ");
strcat(SQL_CREATE_TABLE, "Particle");
strcat(SQL_CREATE_TABLE, "(");
size_t vecSize = field_str.size();
for (size_t ii = 0; ii < vecSize; ii++)
{
if (ii == vecSize - 1)
{
strcat(SQL_CREATE_TABLE, field_str[ii].toStdString().c_str());
strcat(SQL_CREATE_TABLE, ")");
}
else
{
strcat(SQL_CREATE_TABLE, field_str[ii].toStdString().c_str());
strcat(SQL_CREATE_TABLE, ",");
}
};
#ifdef _cout
cout << "SQL = " << SQL_CREATE_TABLE << endl;
#endif
string cs_temp;
cs_temp = SQL_CREATE_TABLE;
const char * s =SQL_CREATE_TABLE;
OutputDebugStringA(cs_temp.c_str());
int res;
res = sqlite3_exec(m_pDB, s, nullptr, nullptr, &err_msg);
if (res != SQLITE_OK)
{
QMessageBox::warning(nullptr,"warning","操作失败,数据库链表生成错误!",QMessageBox::Yes|QMessageBox::No);
str=QString("创建表'%1'操作失败,数据库链表生成错误!res=%1").arg(TableName).arg(res);
OutputDebugStringA(str.toStdString().c_str());
return false;
}
sqlite3_close(m_pDB);
return true;
}