Domanda

I am writing my first User Defined Function in C to be called from mysql. Code is very simple:

char *getKey(UDF_INIT *initid __attribute__((unused)),
    UDF_ARGS *args, char *result, unsigned long *length,
    char *is_null, char *error __attribute__((unused)))
{
    const char *word=args->args[0];
    strcpy(result,"EOOOOOOOO");

    return result;
}

I have compiled and loaded it with no problems.

CREATE FUNCTION getKey RETURNS STRING SONAME 'udf_key.so';

To test it i was calling it within an Insert query:

INSERT INTO `seed4c`.`users` (`iduser`,`userscol`) VALUES (1,getkey('aa'));

userscol is a Varchar(45), so there should be enough space for the returning string. I am geting a warning

1 row(s) affected, 1 warning(s): 1265 Data truncated for column 'userscol' at row 1

When making a Select in the table the field has the expected value (EOOOOOOOO) and some strange chars, non alfanumeric.

What am I doing wrong?

È stato utile?

Soluzione

Add:

*length = strlen(result);

See the documentation

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top