Access to binary data in MySQL

Source: Internet
Author: User
Today, I read MySQL documents and some online articles and learned how to process binary data in MySQL. Many examples are used to access image data, however, what I want to access is not image data, but it does not matter. It is binary data and there is no difference.

I will summarize how to use C to access binary data in Linux.

To insert binary data, follow these steps:

1. Define a buffer char Buf [Eb] (what EB? Enought big, hehe ~~~)

2. Fill in the standard SQL statement with functions such as sprintf or strcpy until the binary is inserted. Insert the binary in the following step.

3. Read Binary data to tmpbuf [size] in some way;
For example, if you want to insert image file data, use fread (tmpbuf, 1, sizeof (tmpbuf), filestream), and then use
The mysql_real_escape_string () function continues to fill the Buf (note that the end character ''' of the string to be entered in step 1 should be entered, that is, no ''''), this function is prototype:
Unsigned long mysql_real_escape_string (MySQL * MySQL, char * To, const char * From, unsigned long length)
The MySQL pointer is valid and a successfully connected handle. In other words, MySQL must be the handle returned by successfully executing the mysql_real_connect () function.

If you see a function like mysql_escape_string, do not use it. It is almost abolished.

4. If you still have data to insert, continue step 2 and 3.

5. Add the remaining SQL language to form the complete SQL language.

6. You can use mysql_real_query () to execute the SQL language. Its prototype is as follows:
Int mysql_real_query (MySQL * MySQL, const char * query, unsigned long length)

Note: There is one important point to note, because binary data contains many characters such as 0 and so on, which may be interpreted as other meanings in the string, so we use the above mysql_real_escape_string () function to copy data, so we must have a variable to record the start and end positions of the query string Buf [Eb] so that we can know how long the query string is, at last, pass the length to the last parameter of the mysql_real_query function.

If it is to query binary data, it is also very easy, I just want to say a few points:
1. Construct a query string.
2. Execute mysql_real_query query.
3. Use mysql_store_result to store the result.
4. Use mysql_fetch_row to retrieve a record for processing.
To determine the length of binary data, use the mysql_fetch_lengths function to obtain the length.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.