SQL Database is dropping non-ascii characters in my C program
by cov from LinuxQuestions.org on (#6HXJN)
Here's my code:
Code:MYSQL *sqlconnect=NULL;
void initialize_database(char *dBase, char *user, char *pw)
{
sqlconnect=mysql_init(sqlconnect);
if (!mysql_real_connect(sqlconnect, "localhost", "dwgman", "x47Y)CmK^o,tr|EE/:oU", dBase, 0, NULL, 0))
{
fprintf(stderr, "Unable to connect to MariaDB server\n");
exit(1);
}
mysql_set_character_set(sqlconnect, "utf8mb4");
}
wchar_t decodeTextCodes(char *ch)
{
wchar_t uni;
if(ch[0]){
uni=atoi(ch);
}else
uni=0xA3;
return uni;
}
void to_usChar(wchar_t c, char *uni)
{
int i=0;
if(c<128)
uni[i++]=c&0xFF;
else if(c<2047){
uni[i++]=192+((c>>6)&15);//1100+00+7-8
uni[i++]=128+(c&63);//10+1-6 LSB
}else if(c<65536){
uni[i++]=224+((c>>12)&15);//1110+13-16
uni[i++]=128+((c>>6)&63);//10+7-12
uni[i++]=128+(c&63);//10+1-6 LSB
}else if(c<0x110000){
uni[i++]=240+((c>>18)&7);//11110+19-21
uni[i++]=128+((c>>12)&63);//10+13-18
uni[i++]=128+((c>>6)&63);//10+7-12
uni[i++]=128+(c&63);//10+1-6 LSB
}
uni[i]=0;
}
int main(int argc, char **argv)
{
int i=0;
char ch[255];
char query[255];
wchar_t c;
if(argc>3)
initialize_database(argv[1],argv[2],argv[3]);
if(argc>4)
strcpy(ch,argv[4]);
else
ch[0]=0;
sprintf(query,"INSERT INTO text(drawing, eID, txt) VALUES(9,14063,'");
c=decodeTextCodes(ch);
wchar2char(c,ch);
strcat(query,ch);
i=strlen(query);
query[i++]='5';
query[i++]='1';
query[i++]='7';
query[i++]='9';
query[i++]=39;
query[i++]=41;
query[i++]=59;
query[i]=0;
printf("\n====\n%s\n====\n",query);
if(argc>3){
mysql_query(sqlconnect,query);
mysql_close(sqlconnect);
}
}This outputs
Quote:
However, the mariadb drops the 'i' character.
Code:+---------+-------+--------------------+
| drawing | eID | txt |
+---------+-------+--------------------+
| 9 | 14063 | 5179 |
+---------+-------+--------------------+Can anyone see what I'm doing wrong?
Code:MYSQL *sqlconnect=NULL;
void initialize_database(char *dBase, char *user, char *pw)
{
sqlconnect=mysql_init(sqlconnect);
if (!mysql_real_connect(sqlconnect, "localhost", "dwgman", "x47Y)CmK^o,tr|EE/:oU", dBase, 0, NULL, 0))
{
fprintf(stderr, "Unable to connect to MariaDB server\n");
exit(1);
}
mysql_set_character_set(sqlconnect, "utf8mb4");
}
wchar_t decodeTextCodes(char *ch)
{
wchar_t uni;
if(ch[0]){
uni=atoi(ch);
}else
uni=0xA3;
return uni;
}
void to_usChar(wchar_t c, char *uni)
{
int i=0;
if(c<128)
uni[i++]=c&0xFF;
else if(c<2047){
uni[i++]=192+((c>>6)&15);//1100+00+7-8
uni[i++]=128+(c&63);//10+1-6 LSB
}else if(c<65536){
uni[i++]=224+((c>>12)&15);//1110+13-16
uni[i++]=128+((c>>6)&63);//10+7-12
uni[i++]=128+(c&63);//10+1-6 LSB
}else if(c<0x110000){
uni[i++]=240+((c>>18)&7);//11110+19-21
uni[i++]=128+((c>>12)&63);//10+13-18
uni[i++]=128+((c>>6)&63);//10+7-12
uni[i++]=128+(c&63);//10+1-6 LSB
}
uni[i]=0;
}
int main(int argc, char **argv)
{
int i=0;
char ch[255];
char query[255];
wchar_t c;
if(argc>3)
initialize_database(argv[1],argv[2],argv[3]);
if(argc>4)
strcpy(ch,argv[4]);
else
ch[0]=0;
sprintf(query,"INSERT INTO text(drawing, eID, txt) VALUES(9,14063,'");
c=decodeTextCodes(ch);
wchar2char(c,ch);
strcat(query,ch);
i=strlen(query);
query[i++]='5';
query[i++]='1';
query[i++]='7';
query[i++]='9';
query[i++]=39;
query[i++]=41;
query[i++]=59;
query[i]=0;
printf("\n====\n%s\n====\n",query);
if(argc>3){
mysql_query(sqlconnect,query);
mysql_close(sqlconnect);
}
}This outputs
Quote:
==== INSERT INTO text(drawing, eID, txt) VALUES(9,14063,'i5179'); ==== |
Code:+---------+-------+--------------------+
| drawing | eID | txt |
+---------+-------+--------------------+
| 9 | 14063 | 5179 |
+---------+-------+--------------------+Can anyone see what I'm doing wrong?