Pergunta

I have created an sql database using Java, I am a noob when it comes to sql but have it configured and set up, i have a table created which has two columns, the first being a big integer which increments, the second I have tried defining it as a char, varchar and binary but im still not getting the desired function, say i try and store 0a a hex number into the char column i get an error, i appended 0x to the beginning a it seems to store, but when i print out the contents it is blank or in some cases i get characters such as '/' or '?', I also tried using sql explorer and it gives me the same result viewing the table,

My problem is i need to store an eight character hex string such as eb8d4ee6.

Could someone please advise me of how this can be done?

Foi útil?

Solução

Here's a great blog post I always refer to to remind myself of the proper handling of hex values and binary fields, and lays out some performance implications.

http://www.xaprb.com/blog/2009/02/12/5-ways-to-make-hexadecimal-identifiers-perform-better-on-mysql/

Outras dicas

See http://dev.mysql.com/doc/refman/5.5/en/hexadecimal-literals.html

MySQL supports hexadecimal values, written using X'val', x'val', or 0xval format, where val contains hexadecimal digits (0..9, A..F). Lettercase of the digits does not matter. For values written using X'val' or x'val' format, val must contain an even number of digits. For values written using 0xval syntax, values that contain an odd number of digits are treated as having an extra leading 0. For example, 0x0a and 0xaaa are interpreted as 0x0a and 0x0aaa.

In numeric contexts, hexadecimal values act like integers (64-bit precision). In string contexts, they act like binary strings, where each pair of hex digits is converted to a character:

You probably should store the Hex number in an integer column. You can then convert back to hex when selecting using the HEX() function.

E.g.,

INSERT INTO MyTable (`MyIntegerColumn`) VALUES (0xeb8d4ee6);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top