我正在尝试制作一个测试程序,看看我在结构中从 SQLite3 数据库获取和存储数据的想法是否可行,但我在代码中遇到了一些主要问题。在调试时,我不断遇到标题中的错误。以下是错误窗口的全文:
调试断言失败!
文件:include\xstring 行:929
表达式:无效的空指针
有关您的程序如何导致断言失败的信息,请参阅有关断言的 Visual C++ 文档。
这是代码,我将用箭头指示问题出在哪一行(<--;额外帮助请参阅开关的案例 2):
#include <iostream>
#include "data_placeholder.h"
#include "sqlite3.h"
#include <vector>
#include <conio.h>
#include <string>
#include <sstream>
using namespace std;
void openDB(sqlite3* dBase, int iID, string table, string operation, sqlite3_stmt* statement, vector<mission>& mission_1);
void createStatement(int iID, string table, string operation, sqlite3_stmt* statement, sqlite3* dBase, vector<mission>& mission_1);
void getMResults(string sqlStr, sqlite3_stmt* statement, sqlite3* dBase, vector<mission>& mission_1);
void returnMResult(vector<mission> mResults, vector<mission>& mission_1);
int main()
{
//Define Variables
vector<mission> mission_1;
sqlite3 *dBase;
sqlite3_stmt *statement;
int pInput;
mission_1.push_back(mission());
cout << "Input a number between 1 and 3" << endl;
cout << ">";
cin >> pInput;
cout << endl;
cout << endl;
openDB(dBase, pInput, "Mission_Data", "select from", statement, mission_1);
cout << mission_1.at(0).mName << ", " << mission_1.at(0).mDesc << ", " << mission_1.at(0).mCBELevel << ", " << mission_1.at(0).mSCReq << ", " << mission_1.at(0).mMWReq << ", " << mission_1.at(0).mTLimit << ", " << mission_1.at(0).mDifficulty << ", " << mission_1.at(0).mSector << ", " << mission_1.at(0).mSystem << ", " << mission_1.at(0).mTName << ", " << mission_1.at(0).mTSClass << ", " << mission_1.at(0).mBounty << ", " << mission_1.at(0).mXarn << ", " << mission_1.at(0).mRubies << ", " << mission_1.at(0).mDiamonds << ", " << mission_1.at(0).mDraconic << ", " << mission_1.at(0).mLithium << ", " << mission_1.at(0).mPlatinum << ", " << mission_1.at(0).mUranium << ", " << mission_1.at(0).mPlutonium << ", " << mission_1.at(0).mNWaste << ", " << mission_1.at(0).mCEXP << ", " << mission_1.at(0).mItem << ", " << mission_1.at(0).mType << ", " << endl;
_getch();
}
void openDB(sqlite3* dBase, int iID, string table, string operation, sqlite3_stmt* statement, vector<mission>& mission_1)
{
sqlite3_open("scDatabase.sqlite",&dBase);
createStatement(iID, table, operation, statement, dBase, mission_1);
}
void createStatement(int iID, string table, string operation, sqlite3_stmt* statement, sqlite3* dBase, vector<mission>& mission_1)
{
stringstream ss;
ss << iID;
string sID(ss.str());
string sqlStr = "Select * From " + table + " Where ID = " + sID;
getMResults(sqlStr, statement, dBase, mission_1);
}
void getMResults(string sqlStr, sqlite3_stmt* statement, sqlite3* dBase, vector<mission>& mission_1)
{
vector<mission> mResults;
mResults.push_back(mission());
if (sqlite3_prepare_v2(dBase, sqlStr.c_str(), sqlStr.size(), &statement, 0) == SQLITE_OK)
{
int cols;
int i;
cols = sqlite3_column_count(statement);
for (i =01; i <= cols; i++)
{
switch(i)
{
case 2:
mResults.at(0).mName = string((char*)sqlite3_column_text(statement,i)); //<-- Here is the line the assert fail happens
break;
/*
case 3:
mResults.at(0).mDesc = string(reinterpret_cast<const char*>(sqlite3_column_text(statement,i)));
break;
case 4:
mResults.at(0).mCBELevel = sqlite3_column_int(statement,i);
break;
case 5:
mResults.at(0).mSCReq = string(reinterpret_cast<const char*>(sqlite3_column_text(statement,i)));
break;
case 6:
mResults.at(0).mMWReq = string(reinterpret_cast<const char*>(sqlite3_column_text(statement,i)));
break;
case 7:
mResults.at(0).mTLimit = sqlite3_column_int(statement,i);
break;
case 8:
mResults.at(0).mDifficulty = string(reinterpret_cast<const char*>(sqlite3_column_text(statement,i)));
break;
case 9:
mResults.at(0).mSector = string(reinterpret_cast<const char*>(sqlite3_column_text(statement,i)));
break;
case 10:
mResults.at(0).mSystem = string(reinterpret_cast<const char*>(sqlite3_column_text(statement,i)));
break;
case 11:
mResults.at(0).mTName = string(reinterpret_cast<const char*>(sqlite3_column_text(statement,i)));
break;
case 12:
mResults.at(0).mTSClass = string(reinterpret_cast<const char*>(sqlite3_column_text(statement,i)));
break;
case 13:
mResults.at(0).mBounty = sqlite3_column_int(statement,i);
break;
case 14:
mResults.at(0).mXarn = sqlite3_column_int(statement,i);
break;
case 15:
mResults.at(0).mRubies = sqlite3_column_int(statement,i);
break;
case 16:
mResults.at(0).mDiamonds = sqlite3_column_int(statement,i);
break;
case 17:
mResults.at(0).mDraconic = sqlite3_column_int(statement,i);
break;
case 18:
mResults.at(0).mLithium = sqlite3_column_int(statement,i);
break;
case 19:
mResults.at(0).mPlatinum = sqlite3_column_int(statement,i);
break;
case 20:
mResults.at(0).mNWaste = sqlite3_column_int(statement,i);
break;
case 21:
mResults.at(0).mCEXP = sqlite3_column_int(statement,i);
break;
case 22:
mResults.at(0).mItem = sqlite3_column_int(statement,i);
break;
case 23:
mResults.at(0).mType = string(reinterpret_cast<const char*>(sqlite3_column_text(statement,i)));
break;
*/
default:
break;
}
}
}
else
{
cout << "something is wrong" << endl;
}
returnMResult(mResults, mission_1);
}
void returnMResult(vector<mission>mResults, vector<mission>& mission_1)
{
mission_1.at(0) = mResults.at(0);
}
当代码在通过 for 循环的第一次迭代时遇到此行时,错误会立即发生。没有编译器错误,我还尝试将前三个函数合并为一个函数,以防数据库和语句指针未正确传递;同样的问题。
编辑2:我已经减少了问题所在。它与我的结构向量有关。我将数据库查询从我设置为 mResults.at(0).mName 的链接中取出,然后将 const unsigned char 添加到字符串,但断言失败仍然发生。
编辑 3:在查看了我今年早些时候完成的一些代码之后,我已经弄清楚发生了什么,至少对于 SQLite 查询。您必须调用 step 才能实际执行查询。因为我没有这样做,所以指针总是返回无效,因为没有加载行,因此没有要查询的列。我的向量没有错。