In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Mysql how to store binary data streams, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
The following is the sample code-follow the instructions to compile and store the binary file with a little modification
View plaincopy to clipboardprint?
/ *
Mysql stores binary data linux
Purpose: use _ stmt_send_long_data () to write binary data streams to the blob field.
Note: it is important to note that the buffer_type field of the bind structure must match the data type to be entered
For example, write only one long data, then use MYSQL_TYPE_LONG, write character stream, and use MYSQL_TYPE_STRING
Write binary data stream, using MYSQL_TYPE_BLOB
For the meaning of each field of this parameter, please see mysql5.0 manual.
Compile: Gmail +-I/usr/include/mysql-L/usr/lib/mysql-lmysqlclient mysql_test.cpp
Preparatory work:
Create database test
Use test
CREATE TABLE `bread` (
`id`int (11) NOT NULL default 0
`data`blob
) ENGINE=MyISAM
, /
# include
# include
# include
# include
# define INSERT_QUERY "INSERT INTO bintest (id, data) VALUES (4,?)"
Void test ()
{
MYSQL_BIND bind [1]
Unsigned long length
Char blog_data [100] = {0}
Memset (blog_data, 0x01, sizeof (blog_data))
Char* pos = blog_data
Int size = 50
MYSQL * mysql = mysql_init (NULL)
If (! mysql) return
If (! mysql_real_connect (mysql
"192.168.xx.xxx"
"root"
"db_user_name"
"test"
3306, NULL, 0))
{
Int ret = mysql_errno (mysql)
Mysql_close (mysql)
Return
}
MYSQL_STMT * stmt = mysql_stmt_init (mysql)
If (! stmt)
{
Fprintf (stderr, "mysql_stmt_init (), out of memory)
")
Exit (0)
}
If (mysql_stmt_prepare (stmt, INSERT_QUERY, strlen (INSERT_QUERY))
{
Fprintf (stderr, "
Mysql_stmt_prepare (), INSERT failed ")
Fprintf (stderr, "
% s ", mysql_stmt_error (stmt))
Exit (0)
}
Memset (bind, 0, sizeof (bind))
/ / bind [0] .buffer _ type= MYSQL_TYPE_STRING
/ / bind [0] .buffer _ type = MYSQL_TYPE_LONG
Bind [0] .buffer = blog_data
/ / bind [0] .buffer _ type = MYSQL_TYPE_TINY
Bind [0] .buffer _ type = MYSQL_TYPE_BLOB
Bind [0] .length = & length
Bind [0] .is _ null= 0
/ * Bind the buffers * /
If (mysql_stmt_bind_param (stmt, bind))
{
Fprintf (stderr, "
Param bind failed ")
Fprintf (stderr, "
% s ", mysql_stmt_error (stmt))
Exit (0)
}
Int rc = 0
/ * Supply data in chunks to server * /
If (mysql_stmt_send_long_data (stmt,0, pos, size))
{
Fprintf (stderr, "
Send_long_data failed ")
Fprintf (stderr, "
% s ", mysql_stmt_error (stmt))
Exit (0)
}
Pos + = size
/ * Supply the next piece of data * /
If (mysql_stmt_send_long_data (stmt,0, pos, size))
{
Fprintf (stderr, "
Send_long_data failed ")
Fprintf (stderr, "
% s ", mysql_stmt_error (stmt))
Exit (0)
}
/ * Now, execute the query * /
If (mysql_stmt_execute (stmt))
{
Fprintf (stderr, "
Mysql_stmt_execute failed ")
Fprintf (stderr, "
% s ", mysql_stmt_error (stmt))
Exit (0)
}
}
Int main ()
{
Test ()
/ / sleep (1)
Return 0
}
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.