【发布时间】:2016-07-25 13:44:57
【问题描述】:
我正在使用 MS Visual Studio 2015 开发一个使用 VC++ 和后端作为 SQLite 的小型应用程序。但是使用标准 SQLite3 C api 没有发生异常。
但是当我尝试为使用 SQLite 制作一个小包装器时。为了简化将函数用作 SQLite API,我制作了一个头文件。我收到read access violation 异常。
如何处理这个异常以及我应该在我的小包装器中进行哪些更改,以便我可以在应用程序的多个模块中使用它。
这是我的小包装器SQLite.cpp:
#include "inc\sqlite3.h"
#include <string.h>
#pragma once
class SQLiteConnection {
sqlite3 * conn;
public:
SQLiteConnection() {
conn = NULL;
}
~SQLiteConnection() {
sqlite3_close(conn);
}
int connect(char const * dbName) {
int res = sqlite3_open(dbName, &conn);
if (SQLITE_OK != res) {
printf("%s\n", sqlite3_errmsg(conn));
return res;
}
return res;
}
sqlite3 * getConn() {
return conn;
}
};
class Statement {
sqlite3_stmt * stmt;
public:
Statement() {
stmt = NULL;
}
int prepare(sqlite3 *,char *);
int bind_param_int(sqlite3 *,int , int);
int bind_param_text(sqlite3 * ,int , char const *);
int bind_param_double(sqlite3 * ,int , double);
bool step();
int reset();
char const * getColText(int idx);
void finalize() {
sqlite3_finalize(stmt);
}
};
int Statement::prepare(sqlite3 * conn, char *sql) {
int result;
result = sqlite3_prepare_v2(conn, sql, -1, &stmt, NULL);
if (SQLITE_OK != result) {
sqlite3_errmsg(conn);
return 0;
}
return SQLITE_OK;
}
int Statement::bind_param_int(sqlite3 * conn,int idx, int val) {
int res;
res = sqlite3_bind_int(stmt, idx, val);
if (SQLITE_OK != res) {
sqlite3_errmsg(conn);
return 0;
}
return SQLITE_OK;
}
int Statement::bind_param_text(sqlite3 * conn, int idx, char const * val) {
int res;
res = sqlite3_bind_text(stmt, idx, val, strlen(val)+1, SQLITE_STATIC);
if (SQLITE_OK != res) {
sqlite3_errmsg(conn);
return 0;
}
return SQLITE_OK;
}
int Statement::bind_param_double(sqlite3 * conn , int idx, double val) {
int res;
res = sqlite3_bind_double(stmt, idx, val);
if (SQLITE_OK != res) {
sqlite3_errmsg(conn);
return 0;
}
return SQLITE_OK;
}
bool Statement::step() {
int res = sqlite3_step(stmt);
if (SQLITE_DONE == res) return true;
if (SQLITE_ROW == res) return true;
return false;
}
int Statement::reset() {
int res = sqlite3_reset(stmt);
if (SQLITE_OK == res) return res;
return 0;
}
char const * Statement::getColText(int idx) {
return (char const *)sqlite3_column_text(stmt, idx);
}
这是我的主要 app.cpp 文件
#include <iostream>
#include <stdio.h>
using namespace std;
/*
* SQLite3 header file
* for getting Constants for verification of results.
*/
#include "inc\sqlite3.h"
#include "SQLite.h"
int main() {
SQLiteConnection con;
try {
if (SQLITE_OK == con.connect(":memory:")) {
cout << "Connected to DB";
Statement stmt;
if (SQLITE_OK == stmt.prepare(con.getConn(), "select 'Hello World'")) {
while (stmt.step())
{
cout << "\n" << stmt.getColText(0) << "\n";
}
stmt.finalize();
}
}
else {
return 1;
}
}
catch (const exception & e) {
cout << "Exception..."<< e.what();
}
getchar();
return 0;
}
第一次接触 Visual C++ 和 SQLite3 所以知识水平是初学者,我对现代 C++ 和 STL 也不太了解;(很快就会学习。 . 希望聪明的头脑能向我解释这里发生了什么,以及我将如何摆脱困境。
【问题讨论】:
-
您正在运行 Visual C++,它为 Windows 平台上的用户模式应用程序提供了世界上最好的集成调试器之一。 使用它。它将向您展示有关代码崩溃的方式/原因的惊人事情。
-
试试this
-
用 F11 和 F10 启动和调试程序,看看是哪个函数抛出了异常
标签: c++ visual-c++ exception-handling sqlite nullpointerexception