字符串转换成二进制,存入mysql

字符串转化成二进制,存到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;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值