开发者

Encoded character buffer storage problem in MySQL varchar using C

开发者 https://www.devze.com 2022-12-20 11:46 出处:网络
I have a encoded character buffer array of size 512 in C, and a database field of varchar in MySQL. Is it possible to store the encoded character buffer into varchar?

I have a encoded character buffer array of size 512 in C, and a database field of varchar in MySQL. Is it possible to store the encoded character buffer into varchar?

I have tried this, but the problem which I face is that it only stores the limited area of the buffer into the database and ignore. What is the a开发者_如何转开发ctual problem, and how do I solve this problem?


It is not clear what you mean by encoded.

If you mean that you have an arbitrary string of byte values, then varchar is a bad fit because it will attempt to trim trailing spaces. A better choice in such cases is to use varbinary fields.

If the string you are inserting contains control characters, you might be best converting that into a hex string and inserting it like follows:

create table xx ( 
    v varbinary(512) not null );

insert into xx values ( 0x68656C6C6F20776F726C64);

This will prevent any component in the tool chain from choking on NUL characters and so forth.


What size is your varchar declared for the table?

Often varchar fields are set to 255 bytes, not characters. Starting with MySQL 5.0.3 you can have longer varchar fields.

Sounds like you need a varchar(512) field, is that what you have?

See http://dev.mysql.com/doc/refman/5.0/en/char.html

0

精彩评论

暂无评论...
验证码 换一张
取 消