-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.cpp
More file actions
87 lines (73 loc) · 2.38 KB
/
binary.cpp
File metadata and controls
87 lines (73 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <cstring>
#include <mysql/mysql.h>
using namespace std;
int main() {
// MySQL authentication
const char *server = "localhost";
const char *user = "root";
const char *password = "honglong1305";
const char *database = "long";
// Message to binary
const char *message = "Hello, this is Long!";
unsigned long message_length = strlen(message);
// Initialize MySQL connection
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
conn = mysql_init(NULL);
if (conn == NULL) {
cerr << "mysql_init() failed\n";
return EXIT_FAILURE;
}
// Connect to the database
if (mysql_real_connect(conn, server, user, password, database, 0, NULL, 0) == NULL) {
cerr << "mysql_real_connect() failed\n";
mysql_close(conn);
return EXIT_FAILURE;
}
// Create a query to insert the binary data
string query = "INSERT INTO 'binary' VALUES (?, ?)";
MYSQL_STMT *stmt = mysql_stmt_init(conn);
if (!stmt) {
cerr << "mysql_stmt_init() failed\n";
mysql_close(conn);
return EXIT_FAILURE;
}
if (mysql_stmt_prepare(stmt, query.c_str(), query.length())) {
cerr << "mysql_stmt_prepare() failed: " << mysql_stmt_error(stmt) << "\n";
mysql_stmt_close(stmt);
mysql_close(conn);
return EXIT_FAILURE;
}
// Bind the parameters
MYSQL_BIND bind[2] = {};
// Bind for message_text (string)
bind[0].buffer_type = MYSQL_TYPE_STRING;
bind[0].buffer = (char *)message;
bind[0].buffer_length = message_length;
bind[0].is_null = 0;
bind[0].length = &message_length;
// Bind for message_binary (binary)
bind[1].buffer_type = MYSQL_TYPE_BLOB;
bind[1].buffer = (char *)message;
bind[1].buffer_length = message_length;
bind[1].is_null = 0;
bind[1].length = &message_length;
if (mysql_stmt_bind_param(stmt, bind)) {
cerr << "mysql_stmt_bind_param() failed: " << mysql_stmt_error(stmt) << "\n";
mysql_stmt_close(stmt);
mysql_close(conn);
return EXIT_FAILURE;
}
// Execute the statement
if (mysql_stmt_execute(stmt)) {
cerr << "mysql_stmt_execute() failed: " << mysql_stmt_error(stmt) << "\n";
} else {
cout << "Message stored successfully.\n";
}
// Clean up
mysql_stmt_close(stmt);
mysql_close(conn);
return EXIT_SUCCESS;
}