如何将xml插入Mysql?

发布于 2024-09-14 17:16:41 字数 905 浏览 5 评论 0原文

这个问题看起来很简单,但我是 C++ 和 MySQL 的菜鸟,所以它仍然不起作用。

这是交易: 我有一个包含 xml 的字符串 (_bstr_t),我想将其存储在 MySql 中的 longblolb 列中。

我尝试失败的方法:

  1. 将我的xml写入本地文件并使用mysql命令LOAD_FILE,这在本地有效,但在服务器上无效,并且在服务器上使用此方法不安全
  2. use mysql_real_escape_string( )我认为这是可行的方法,但是这个函数需要 char* 并且我有多字节字符串,所以它不起作用

是否有其他方法在 MySQL 中存储 xml 或者是否有像 mysql_real_escape 这样的多字节函数。

strcpy 不起作用,它给了我“访问冲突读取位置...”当我尝试强制转换时,它看起来不错,但最后我遇到了 MySql 错误:

“错误:您的 SQL 中有错误 句法;检查手册 对应你的MySQL服务器 正确使用语法的版本 靠近 '?xml version=\"1.0\" 编码=\"UTF-16\"?>\r\n XV_Object 第 1 行的 noNamespaceSchem'

最后,对于你给我的第一个解决方案,我对 C++ 真的很陌生,我不知道在哪里放置 ::char*,我无法编译。

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

非常感谢非常感谢您的帮助!

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 XV_Object
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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹过旳痕迹 2024-09-21 17:16:41

据我所知,这真的很容易,因为微软为此提供了 ::char* 。

如果您的字符串位于 _bstr_t 对象中,只需执行 mysql_real_escape_string(your_object::char*) 即可。

如果失败,您还可以使用 strcncpy。
你可以这样做:

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

最后但并非最不重要的,你可以简单地从 _bstr_t 转换为 char*:

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

编辑:(在评论之后)

尼克,每当你使用“new”时,请确保你最后调用删除来摆脱不必要的内存错误。这与您的问题无关,这只是您应该遵循的一般建议。

对于我的第一个解决方案,将 ::char* 放在 _bstr_t 对象后面。例如,如果是这样的:

_bstr_t xml;

那就这样做。

xml::char*

并将其用作 mysql_real_escape 字符串中的参数。
再次进行转换,就像我用 just 编写的那样

char* somevar = (char*)xml;

,并将其传递给 mysql_real_escape_string。
下次有东西无法编译时,也发布错误,而不仅仅是没有编译。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文