MySQL C++ 中的用户定义函数
MySQL User Defined Functions in C++
我正在尝试 运行 MySQL 用 C++ 编写的 UDF。它可以很好地编译并生成正确的输出,但在输出后会生成大量垃圾。我想知道这个垃圾的原因以及如何解决这个问题?我附上了我的代码和输出的屏幕截图。
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include "mysql-connector-c-6.1.9-linux-glibc2.5-x86_64/include/mysql.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/mysql_connection.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/sqlstring.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/resultset.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/datatype.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/resultset.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/resultset_metadata.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/exception.h"
using namespace std;
extern "C" {
char *hello_world (UDF_INIT *initid, UDF_ARGS *args,char *result, unsigned long length,char *is_null, char *error);
my_bool hello_world_init (UDF_INIT *initid, UDF_ARGS *args, char *message);
void hello_world_deinit (UDF_INIT *initid);
//template <typename T> T adder (UDF_INIT *initid, UDF_eARGS *args,string result, unsigned long length,string is_null, string error,T v);
}
char *hello_world (UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long length, char *is_null, char *error)
{
string res;
res = args->args[0];
res.append(" hello");
char *c = new char[res.size()];
strcpy(c, res.c_str());
return c;
}
my_bool hello_world_init (UDF_INIT *initid, UDF_ARGS *args, char *message)
{
return 0;
//cout<<"success"<<endl;
}
void hello_world_deinit (UDF_INIT *initid)
{
return;
}
enter image description here
hello_world
函数的签名错误。第四个参数应该是
unsigned long *length
该指针指向的值必须设置为返回字符串的长度。
char *c = new char[res.size() + 1];
strcpy(c, res.c_str());
*length = res.size();
return c;
我正在尝试 运行 MySQL 用 C++ 编写的 UDF。它可以很好地编译并生成正确的输出,但在输出后会生成大量垃圾。我想知道这个垃圾的原因以及如何解决这个问题?我附上了我的代码和输出的屏幕截图。
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include "mysql-connector-c-6.1.9-linux-glibc2.5-x86_64/include/mysql.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/mysql_connection.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/sqlstring.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/resultset.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/datatype.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/resultset.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/resultset_metadata.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/exception.h"
using namespace std;
extern "C" {
char *hello_world (UDF_INIT *initid, UDF_ARGS *args,char *result, unsigned long length,char *is_null, char *error);
my_bool hello_world_init (UDF_INIT *initid, UDF_ARGS *args, char *message);
void hello_world_deinit (UDF_INIT *initid);
//template <typename T> T adder (UDF_INIT *initid, UDF_eARGS *args,string result, unsigned long length,string is_null, string error,T v);
}
char *hello_world (UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long length, char *is_null, char *error)
{
string res;
res = args->args[0];
res.append(" hello");
char *c = new char[res.size()];
strcpy(c, res.c_str());
return c;
}
my_bool hello_world_init (UDF_INIT *initid, UDF_ARGS *args, char *message)
{
return 0;
//cout<<"success"<<endl;
}
void hello_world_deinit (UDF_INIT *initid)
{
return;
}
enter image description here
hello_world
函数的签名错误。第四个参数应该是
unsigned long *length
该指针指向的值必须设置为返回字符串的长度。
char *c = new char[res.size() + 1];
strcpy(c, res.c_str());
*length = res.size();
return c;