spatialite 数据查询c++

我在使用spatialite时想要查询表中的geometry字段

void LnyxMapToCsmapConverter::GetGeometryPoints(sqlite3_stmt *stmt, int index, std::vector &points) {
    u_char *pBolb = (u_char *)sqlite3_column_blob(stmt, index);
        if (pBolb != nullptr) {
        int count = sqlite3_column_bytes(stmt, index);

        gaiaGeomCollPtr geo = nullptr;
        geo = gaiaFromSpatiaLiteBlobWkb(pBolb,count);
        if (geo != nullptr) {
            if (geo->DimensionModel == GAIA_XY_Z) {
                int geoType = gaiaGeometryType(geo);
                if (geoType == GAIA_POINTZ) {
                    gaiaPointPtr p = geo->FirstPoint;
                    if (p != nullptr) {
                        if (p->DimensionModel == GAIA_XY_Z) {
                            Point3d pt;
                            pt.set_x(p->X);
                            pt.set_y(p->Y);
                            pt.set_z(p->Z);
                            points.emplace_back(pt);
                        }
                        p = p->Next;
                        

                    }
                }

这是一部分代码,pblob指针无法获取值,到底用什么方法可以查询geometry字段

可以使用 SQLite 的 sqlite3_column_blob 函数来获取查询结果中 geometry 字段的值。这个函数用于获取当前记录中指定列的二进制字段值。

u_char *pBolb = (u_char *)sqlite3_column_blob(stmt, index);

其中stmt 是你使用的 SQLite 预处理语句的指针,index 是你要获取的字段的索引(从 0 开始)。

如果获取成功,函数会返回二进制字段的指针,否则会返回 null。
还可以使用 Spatialite 库中的其他函数来处理这个几何对象。例如可以使用 gaiaGeometryType 函数来获取几何对象的类型,使用 gaiaPointPtr 函数来获取几何对象的首个点,以及使用 Next 属性来遍历所有的点。

while (p != nullptr) {
    Point3d pt;
    pt.set_x(p->X);
    pt.set_y(p->Y);
    pt.set_z(p->Z);
    points.emplace_back(pt);
    p = p->Next;
}

这段代码会从几何对象中取出每一个点,然后将其转换为一个 Point3d 对象,并把它加入到 points 向量中。

提供参考思路实例【MySQL空间查询(Geometry类)】,链接:https://blog.csdn.net/BIT_SKY/article/details/49999273?spm=1001.2101.3001.6650.7&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-7-49999273-blog-126656990.pc_relevant_3mothn_strategy_and_data_recovery&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-7-49999273-blog-126656990.pc_relevant_3mothn_strategy_and_data_recovery&utm_relevant_index=12
【看下实例中讲述的思路是否可以帮助到你】

SpatiaLite 使用指南
借鉴下
https://blog.csdn.net/jinking01/article/details/115731615

在这段代码中,你需要使用sqlite3_column_blob函数来获取表中的 geometry 字段的值。

这个函数接受两个参数:一个是stmt变量,表示预编译的 SQL 语句;另一个是表中 geometry 字段的索引,即列号。

如果获取成功,该函数会返回一个指向表中 geometry 字段值的指针,你可以使用这个指针来访问 geometry 字段的内容。

如果获取失败,该函数会返回 nullptr。你可以在代码中加入判断,当返回值为 nullptr 时,输出错误信息或者执行其他处理。

希望这些信息能帮到你。

首先,您需要确保表中存在geometry字段,并且在查询语句中正确使用这个字段
SELECT * FROM table WHERE geometry IS NOT NULL

看起来你的代码正在尝试从一个 SQLite3 数据库中的表中获取一个 "geometry" 字段的值。

首先,你需要使用 sqlite3_prepare_v2 函数来预处理一条 SQL 语句,然后使用 sqlite3_step 函数执行这条语句,并使用 sqlite3_column_blob 函数获取查询结果中的 "geometry" 字段。

例如:

const char *sql = "SELECT geometry FROM my_table WHERE id = ?";
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
    // 处理错误
}

// 绑定参数
sqlite3_bind_int(stmt, 1, 123);

// 执行查询
rc = sqlite3_step(stmt);
if (rc == SQLITE_ROW) {
    // 获取 "geometry" 字段的值
    u_char *pBlob = (u_char *)sqlite3_column_blob(stmt, 0);
    if (pBlob != nullptr) {
        // 解析和处理 blob 数据
    }
}

// 释放资源
sqlite3_finalize(stmt);

希望这些信息能帮到你!