我有一个类python
脚本。
import sqlite3
from db_layer_core import DbTableCore
class ItemTable(DbTableCore):
q_create = """create table item_table (
item_id text primary key not null,
name text not null,
description text )"""
q_drop = 'drop table item_table'
q_select = 'select item_id, name, description from item_table'
q_insert = 'insert into item_table(item_id, name, description) values (?, ?, ?)'
q_update = 'update item_table set name = ? , description = ? where item_id = ?'
def create_table(self):
try:
with sqlite3.connect('p_storage.db') as db_conn:
db_conn.execute(q_create)
except sqlite3.OperationalError:
print 'such table exists'
def drop_table(self):
try:
with sqlite3.connect('p_storage.db') as db_conn:
db_conn.execute(q_drop)
except sqlite3.OperationalError:
print 'such table probably does not exist'
def insert_items(items):
with sqlite3.connect('p_storage.db') as db_conn:
for row in items:
db_conn.execute(q_insert, (row[0], row[1], row[2]))
db_conn.commit()
def select_items(self):
with sqlite3.connect('p_storage.db') as db_conn:
cursor = db_conn.execute(q_select)
for row in cursor:
print row
def update_items(items):
with sqlite3.connect('p_storage.db') as db_conn:
for row in items:
db_conn.execute(q_update, (row[0], row[1], row[2]))
db_conn.commit()
db_layer_core
文件看起来像这样
from abc import ABCMeta, abstractmethod
class DbTableCore:
__metaclass__ = ABCMeta
@abstractmethod
def create_table(self):
return
@abstractmethod
def drop_table(self):
return
@abstractmethod
def insert_items(self, items):
return
@abstractmethod
def select_items(self):
return
@abstractmethod
def update_items(items):
return
我想将ItemTable
类导入我的c++
代码。最初我想使用boost.python
,但由于缺乏导入示例(虽然有很多导出示例)但我决定坚持基本方法。
我的main.cpp
文件看起来像这样
#include <cmath>
#include <boost/python.hpp>
#include <list>
#include <iostream>
using namespace boost::python;
class Item {
private:
std::string item_id;
std::string name;
std::string description;
friend class ItemTableWrapper;
};
class DbTableCoreWrapper {
public:
virtual void create_table() = 0;
virtual void drop_table() = 0;
virtual void insert_items() = 0;
virtual void select_items() = 0;
virtual void update_items() = 0;
virtual ~DbTableCoreWrapper() {
}
};
class ItemTableWrapper: public DbTableCoreWrapper {
public:
ItemTableWrapper() {
Py_Initialize();
script_name = PyString_FromString("item_table.py");
pModule = PyImport_Import(script_name);
PyErr_Print();
pDict = PyModule_GetDict(pModule);
pClass = PyDict_GetItemString(pDict, "ItemTable");
if (PyCallable_Check(pClass)) {
python_class_instance = PyObject_CallObject(pClass, NULL);
}
}
~ItemTableWrapper() {
Py_DECREF(pModule);
Py_DECREF(script_name);
Py_DECREF(pDict);
Py_DECREF(pClass);
Py_DECREF(python_class_instance);
Py_Finalize();
}
private:
PyObject *script_name;
PyObject *pModule;
PyObject *pDict;
PyObject *pClass;
PyObject *python_class_instance;
public:
virtual void create_table() {
PyObject_CallMethod(python_class_instance, "create_table", NULL);
}
virtual void drop_table() {
PyObject_CallMethod(python_class_instance, "drop_table", NULL);
}
virtual void insert_items() {
// clean for now
}
virtual void select_items() {
PyObject* result = PyObject_CallMethod(python_class_instance,
"select_items", NULL);
if (result != NULL) {
printf("Not empty select!");
} else {
PyErr_Print();
printf("Empty select!");
}
}
virtual void update_items() {
// empty
}
};
int main() {
ItemTableWrapper *itw = new ItemTableWrapper();
itw->select_items();
delete itw;
return 0;
}
但是当我尝试运行应用程序时,我得到了带有application stopped working
的消息窗口和控制台中的消息
Program received signal SIGSEGV, Segmentation fault.
0x000000001e0c45d9 in python27!PyModule_GetDict () from C:\WINDOWS\system32\python27.dll
pDict = PyModule_GetDict(pModule);
行失败。
但我发现奇怪的是script_name
看起来像这样
Name : script_name
Details:0x20b1f30
Default:0x20b1f30
Decimal:34283312
Hex:0x20b1f30
Binary:10000010110001111100110000
Octal:0202617460
而pModule
看起来像这样
Name : pModule
Details:0x0
Default:0x0
Decimal:0
Hex:0x0
Binary:0
Octal:0
这意味着它没有加载模块。
有什么问题?
此外,我的db_layer_core.py
和item_table.py
与application
exe位于同一文件夹中。