How do I update a value in a row in MySQL using Connector/C++

拟墨画扇 提交于 2021-01-28 11:40:46

问题


I have a simple database and want to update an int value. I initially do a query and get back a ResultSet (sql::ResultSet). For each of the entries in the result set I want to modify a value that is in one particular column of a table, then write it back out to the database/update that entry in that row.

It is not clear to me based on the documentation how to do that. I keep seeing "Insert" statements along with updates - but I don't think that is what I want - I want to keep most of the row of data intact - just update one column.

Can someone point me to some sample code or other clear reference/resource?

EDIT:

Alternatively, is there a way to tell the database to update a particular field (row/col) to increment an int value by some value?

EDIT:

So what is the typical way that people use MySQL from C++? Use the C api or the mysql++? I guess I chose the wrong API...


回答1:


From a quick scan of the docs it appears Connector/C++ is a partial implementation of the Java JDBC API for C++. I didn't find any reference to updateable result sets so this might not be possible. In Java JDBC the ResultSet interface includes support for updating the current row if the statement was created with ResultSet.CONCUR_UPDATABLE concurrency.

You should investigate whether Connector/C++ supports updateable resultsets.

EDIT: To update a row you will need to use a PreparedStatement containing an SQL UPDATE, and then the statement's executeUpdate() method. With this approach you must identify the record to be update with a WHERE clause. For example

update users set userName='John Doe' where userID=?

Then you would create a PreparedStatement, set the parameter value, and then executeUpdate().



来源:https://stackoverflow.com/questions/1842481/how-do-i-update-a-value-in-a-row-in-mysql-using-connector-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!