在C++中的Sybase表上运行SQL命令
Running SQL Commands on Sybase Table in C++
用屏幕截图中的修改代码更新
有一个Sybase数据库设置,提供的isql脚本可以连接到它。我现在要做的是能够连接到数据库,在数据库上运行一些sql命令(无论是否使用isql脚本),并将输出存储在数据结构中,以便在c++中处理每一行。我在网上发现了一小段代码,它应该允许我通过将命令作为c样式字符串传入来运行isql脚本,但试图编译cpp文件时会出现未在范围中声明的popen和pclose错误。如果有其他连接数据库的方法,或者如果您知道如何解决该错误,请告诉我。
原始代码:
#include <stdio.h>
#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
std::string exec(const char* cmd) {
char buffer[128];
std::string result = "";
std::shared_ptr<FILE> pipe( popen(cmd, "r"), pclose());
if (!pipe) throw std::runtime_error("popen() failed!");
while (!feof(pipe.get())) {
if (fgets(buffer, 128, pipe.get()) != NULL)
result += buffer;
}
return result;
}
使用GNU++14编译代码以响应ildjarn
使用lambda函数为管道编译代码以响应max66
是否在作用域中声明?
你确定吗?
我的编译器说pclose()
需要一个参数。
我建议您使用自定义deleter修改pipe
声明;一个简单的lambda函数应该可以工作;类似的东西
std::shared_ptr<FILE> pipe( popen(cmd, "r"), [](auto ptr) { if ( ptr ) pclose(ptr); });
相关文章:
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 代码在main()中运行,但在函数中出现错误
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何在运行中期切换GTK CSS style_context
- 如何在MS Visual Studio 2019中运行QT UI
- 如何通过cpp程序运行shell脚本
- IPC使用多个管道和分支进程来运行Python程序
- 删除指向指针的指针是运行时错误吗
- 如何用参数值调用函数(仅在运行时已知)
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 如何使用c++在VS 2019上运行SQL查询
- 如何使用 C 编程在运行时在 SQL 语句中传递文件路径
- 在正在运行的C++程序中自动创建 SQL Server 数据库
- 如何在 c++ 中运行.sql脚本文件
- 用c++在SQL Server上运行select查询最快的方式是什么?
- 在C++中的Sybase表上运行SQL命令