views:

58

answers:

1

Hi, this questions looks really easy but I'm a noob in C++ and MySQL so it still doesn't work.

Here is the deal: I have a string (_bstr_t) that contains the xml and I want to store it in a longblolb column in MySql.

Ways I tried that failed:

  1. write my xml on a local file and use mysql command LOAD_FILE, this worked localy but not on the server, and it is not secure to use this method with a server
  2. use mysql_real_escape_string() I think ist the way to go but this function takes char* and I have multibyte string so it doesn't work

Are there other ways to store a xml in MySQL or is there a function like mysql_real_escape for multibyte.

The strcpy dosen't work it gives me "Access violation reading location..." When I tried the cast it look good but finaly I had a MySql error:

"Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?xml version=\"1.0\" encoding=\"UTF-16\"?>\r\n XVision_Objects noNamespaceSchem' at line 1"

And finaly, for the first solution you gave me, i'm really new to C++ and I don't know where to put the ::char*, I'm not able to compile.

char *to = new char[xml.length() * 2 + 1];  
char* from = (char*)xml::char*;  
mysql_real_escape_string(conn,to,from,xml.length());

thank you very much for your help!

A: 

As far as I know it's really easy as microsoft provides ::char* for that.

If you have your string in a _bstr_t object, just do mysql_real_escape_string(your_object::char*), that should do the trick.

If that fails, you can also use strcncpy. You could do that like this:

char xml[200];
_bstr_t the_xml_you_loaded;
strcncpy(xml,(char*)the_xml_you_loaded, sizeof(xml));

Last but not least, you could simply cast from _bstr_t to char*:

char* xml = (char*)the_xml_you_loaded;
mysql_real_escape_string(xml);

Edit: (after the comments)

Nick, whenever you use "new", make sure you call delete in the end to get rid of unwanted memory errors. This has nothing to do with your problem, it's just a general advice you should follow.

For my first solution, put ::char* behind the _bstr_t object. e.g. if it is like this:

_bstr_t xml;

Then do that.

xml::char*

and use it as a parameter in mysql_real_escape string. And again to the casting, do it like I wrote it with just

char* somevar = (char*)xml;

and pass it to mysql_real_escape_string. Next time something doesn't compile, also post the error and not just that it doesn't.

Robin
(part1)Hi Robin,thank you very much for your quick answer.The strcpy doen't work it gives me "Access violation reading location..." When I tried the cast it look good but finaly I had a MySql error: "Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '<?xml version=\"1.0\" encoding=\"UTF-16\"?>\r\n<XVision_Objects noNamespaceSchem' at line 1"And finaly, for the first solution you gave me, i'm really new to C++ and I don't know where to put the ::char*, I'm not able to compile.
Nick
(part2)For the cast I did this but it doesn't compile:char *to = new char[xml.length() * 2 + 1];char* from = (char*)xml::char*;mysql_real_escape_string(conn,to,from,xml.length());thank you very much for your help!
Nick
Thanks for your more or less detailed comment, I edited my post.
Robin
Hi Robin, thank you very much, it works now!Thank you very much for your time, and I'll try to give more details for my future posts.Have a nice day.
Nick