Inserting hex value mysql

十年热恋 提交于 2019-12-21 17:42:27

问题


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?


回答1:


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/




回答2:


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);


来源:https://stackoverflow.com/questions/4306219/inserting-hex-value-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!