如何在与c ++连接的sql中插入数组元素? [英] How to insert array elements in sql connecting with c++?

查看:217
本文介绍了如何在与c ++连接的sql中插入数组元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我连接到数据库后,我一定会逐个插入元素到我的表,但我不知道从数组中插入元素,vector..etc到表。
下面的查询是我试过,但没有对表的影响。

After my connection to database, I surely insert elements to my table one by one, but I do not have any idea about inserting elements from an array, vector..etc to tables. Following query is that I tried, but no effects on table.

mysql_query(connection,"insert into mytable (id) values(arr[0])");  


推荐答案

C / C ++不会将值插入字符串像大多数脚本语言一样。您必须使用字符串操作来构建查询字符串,例如(在伪代码中):

C/C++ don't interpolate values into a string as most scripting languages do. You'll have to use string operations to build the query string, e.g. (in pseudo-code):

str = "insert into mytable(id) values (" + arr[0] + ")";

。 C绝对没有办法知道在该查询字符串中的 arr [0] 应该被当作数组引用,而不仅仅是看起来像一个的纯文本。因此,必须自己构建字符串。

instead. C has absolutely no way of knowing that arr[0] in that query string should be treated as an array reference, and not just plain text that happens to look like one. Hence having to build the string yourself.

这篇关于如何在与c ++连接的sql中插入数组元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆