How to insert array elements in sql connecting wit

2019-08-22 08:32发布

问题:

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])");  

回答1:

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] + ")";

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.