我用SQLite编写了一个C++测试应用程序,我做了一个小包装器。
一件奇怪的事情:我现在显示返回的语句指针(通过printf(“%p”)),第一次读取时它总是0048D228。撞车的是666767。
以下是我课程的重要部分:
class SQLiteResultSet
{
private:
sqlite3_stmt *stmt;
public:
inline SQLiteResultSet(sqlite3_stmt *pStmt) : stmt(pStmt) {}
inline bool next() { return sqlite3_step(stmt) == SQLITE_ROW; }
inline const unsigned char *textColumn(int colNum) {
return sqlite3_column_text(stmt, colNum);
}
inline const int intColumn(int colNum) {
return sqlite3_column_int(stmt, colNum);
}
inline void close() {
if(isOpen()) {
printf("----closing %p\n",stmt);
sqlite3_finalize(stmt);
printf("----closed\n");
stmt = NULL;
}
}
inline bool isOpen() { return stmt != NULL; }
inline ~SQLiteResultSet() { close(); }
};
class SQLiteGateway
{
private:
sqlite3 *db;
SQLiteResultSet *rs;
void resetResultSet();
public:
SQLiteGateway(char *dbName);
SQLiteResultSet *select(char *query);
bool tableExists(char *tableName);
void execute(char *query);
public:
~SQLiteGateway(void);
};
SQLiteGateway::SQLiteGateway(char *dbName)
{
db = NULL;
rs = NULL;
int error = sqlite3_open(dbName, &db);
if (error)
{
throw sqlite3_errmsg(db);
}
}
void SQLiteGateway::resetResultSet()
{
if(rs != NULL)
{
delete rs;
rs = NULL;
}
}
SQLiteResultSet *SQLiteGateway::select(char *query)
{
sqlite3_stmt *res;
const char *tail;
resetResultSet();
int error = sqlite3_prepare_v2(db,query,-1,&res,&tail);
if (error != SQLITE_OK || res == NULL)
{
throw sqlite3_errmsg(db);
}
rs = new SQLiteResultSet(res);
return rs;
}
SQLiteGateway::~SQLiteGateway(void)
{
resetResultSet();
if(db != NULL)
{
sqlite3_close (db);
}
}
编辑:测试程序!
注意:从SQLiteGateway的声明中删除tableExists()和execute(),我没有在这里复制它们的主体,它们也没有在这个测试中使用。
int main()
{
SQLiteGateway *sqlg = NULL;
try
{
sqlg = new SQLiteGateway("./apptest.db");
for(int i=0; i<5; i++)
{
SQLiteResultSet *rs = sqlg->select("select x from mytable");
if(rs->next()) printf("%d\n", rs->intColumn(0));
delete rs;
}
}
catch(char *str)
{
printf("Abnormal termination: %s\n", str);
}
if(sqlg != NULL)
{
delete sqlg;
}
}
0
----closing 0048D1E0
----closed
0
----closing 0048D1E0
----closed
----closing 0048DFF8