LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-17-2024, 02:59 AM   #1
cov
Member
 
Registered: Apr 2005
Location: Durban
Posts: 436

Rep: Reputation: 30
SQL Database is dropping non-ascii characters in my C program


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:
====
INSERT INTO text(drawing, eID, txt) VALUES(9,14063,'£5179');
====
However, the mariadb drops the '£' character.
Code:
+---------+-------+--------------------+
| drawing | eID   | txt                |
+---------+-------+--------------------+
|       9 | 14063 | 5179               |
+---------+-------+--------------------+
Can anyone see what I'm doing wrong?
 
Old 01-17-2024, 03:57 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
`to_usChar` is defined, but not used; 'wchar2char` is used, but not defined.

Try this in mysql.exe:
Code:
SELECT drawing,eID,hex(txt) FROM text;

Last edited by NevemTeve; 01-17-2024 at 04:03 AM.
 
Old 01-17-2024, 02:30 PM   #3
cov
Member
 
Registered: Apr 2005
Location: Durban
Posts: 436

Original Poster
Rep: Reputation: 30
Cheers!

I've been using a number of different routines to try different things to get it to work. I thought that I'd removed them, but obviously not.

Your suggestion of
Code:
SELECT drawing,eID,hex(txt) FROM text;
yields interesting results:

Code:
+---------+-------+--------------+
| drawing | eID   | hex(txt)     |
+---------+-------+--------------+
|       9 | 14063 | C2A335313739 |
+---------+-------+--------------+
Clearly the unicode character is there.

Thanks, I'll need to look at whether it will disply in my PHP front end.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Rant about non ascii characters disappearing when using the non advanced editor Didier Spaier LQ Suggestions & Feedback 1 02-08-2016 12:36 PM
file command sees regular ASCII text file as ASCII Java program text bbraml Linux - Software 6 08-30-2013 08:52 AM
mouse keys and non-ascii characters elyk Slackware 8 12-02-2005 12:46 PM
Non ASCII (language specific) characters in filenames? milicic.marko Linux - General 2 03-19-2005 05:15 PM
Deleting non ASCII characters Thinkgeekness Linux - Networking 4 03-04-2003 01:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration