因此,在获得技巧后研究MySQL用户定义的函数,我能够使它起作用。
在Code :: Blocks中,我编写了这个简单的库文件
#include <stdlib.h>#include <stdio.h>#include <string.h>typedef long long longlong; #include <mysql.h>#include <ctype.h> #include <sys/time.h>#include <unistd.h>static pthread_mutex_t LOCK_hostname;extern "C" longlong PreciseNow(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error);extern "C" my_bool PreciseNow_init(UDF_INIT *initid, UDF_ARGS *args, char *message);extern "C" void PreciseNow_deinit(UDF_INIT *initid);my_bool PreciseNow_init(UDF_INIT *initid, UDF_ARGS *args, char *message){ return 0; //optional}void PreciseNow_deinit(UDF_INIT *initid){ //optional}longlong PreciseNow(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error){ struct timeval start; long mtime, seconds, useconds; gettimeofday(&start, NULL); return start.tv_sec * 1000000 + start.tv_usec;}它唯一要做的就是返回当前系统时间。我将lib文件放在’/ usr / lib / mysql / plugin /’中。其次,在数据库中,执行以下查询:
创建函数PreciseNow返回整数SONAME’libhrt.so’
最后,我将时间戳列转换为bigint(8字节整数)。也许这不是必需的,但是看到C函数返回相同的对象类型,为什么不呢?
现在,当运行SQL查询“ SELECT PreciseNow()”时,我获得了内置的“
NOW()”的微秒级精确版本!由于我对MySQL的了解非常有限,因此我认为这是对我的问题的完全合法和有效的解决方案?



