字符串转化成二进制,存到mysql中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "/usr/local/mysql/include/mysql.h"
int main()
{
MYSQL my_connection;
char buff[255]={0};
char sql[255];
int i,k,t,temp;
char a[]="this is test";
temp=strlen(a);
for(k=0;k<temp;k++)
{
for(i=7;i>=0;i--)
{
t=a[k]/2;
buff[8*k+i]=a[k]%2+'0';
a[k]=t;
}
}
int res;
printf("%c/n",buff[1]);
// res = mysql_query(&my_connection, "insert into userinfo(username,friend) values('zyz1',2)");
sprintf(sql,"insert into message(message) values(b'%s')",buff);
mysql_init(&my_connection);
if (mysql_real_connect(&my_connection, "192.168.1.214", "zyz", "12345678","gps2008",3306,NULL,CLIENT_FOUND_ROWS))
{
printf("Connection success/n");
printf("%s/n",sql);
res = mysql_query(&my_connection, sql);
if (!res)
{
printf("Inserted %lu rows/n",(unsigned long)mysql_affected_rows(&my_connection));
// printf("delete %lu rows/n",(unsigned long)mysql_affected_rows(&my_connection));
// printf("update %lu rows/n",(unsigned long)mysql_affected_rows(&my_connection));
}
mysql_close(&my_connection);
}
else
{
printf("Connection failed/n");
if (mysql_errno(&my_connection))
{
printf("Connection error %s/n",mysql_error(&my_connection));
}
}
return EXIT_SUCCESS;
}