插入MySQL DB中的表之后,错误消息
Error message after inserting to a table in MYSQL db
我有此c 代码正常,我可以从表中阅读并写入表:
:int main()
{
// Try to get a driver to use to connect to our DBMS
try
{
driver = get_driver_instance();
}
catch (sql::SQLException e)
{
cout << "Could not get a database driver. Error message: " << e.what() << endl;
system("pause");
exit(1);
}
// Try to connect to the DBMS server
try
{
dbConn = driver->connect(server, username, password);
}
catch (sql::SQLException e)
{
cout << "Could not connect to database. Error message: " << e.what() << endl;
system("pause");
exit(1);
}
stmt = dbConn->createStatement(); // Specify which connection our SQL statement should be executed on
// Try to query the database
try
{
stmt->execute("USE test"); // Select which database to use. Notice that we use "execute" to perform a command.
res = stmt->executeQuery("INSERT INTO users (fName, lName, age) VALUES ('fname', 'lname', 25)"); // Perform a query and get the results. Notice that we use "executeQuery" to get results back
//res = stmt->executeQuery("SELECT * FROM users");
//return 0;
}
catch (sql::SQLException e)
{
cout << "SQL error. Error message: " << e.what() << endl;
system("pause");
exit(1);
}
sql::ResultSetMetaData *res_meta = res -> getMetaData();
int columns = res_meta -> getColumnCount();
// While there are still results (i.e. rows/records) in our result set...
while (res->next())
{
for (int i = 1; i <= columns; i++) {
cout << res->getString(i) << " | " ;
}
cout << endl;
}
delete res;
delete stmt;
delete dbConn;
//system("pause");
return 0;
}
所以,这将插入表格,但是我得到了此错误消息
SQL错误。错误消息:SH:1:暂停:找不到
如果我使用"选择"。
不会发生这种情况。我也知道这个问题已经在这里问过,但不幸的是它没有答案,所以我又问了。
您的问题看起来与MySQL查询有关,但会引发异常。
executeQuery()
假设SQL查询应返回sql::ResultSet
,但是您的INSERT INTO
查询不。您可以改用execute()
,返回真或错误:
try
{
stmt->execute("USE test");
stmt->execute("INSERT INTO users (fName, lName, age) VALUES ('fname', 'lname', 25)");
}
catch (sql::SQLException e)
{
cout << "SQL error. Error message: " << e.what() << endl;
exit(1);
}
插入不是查询。尝试使用executeUpdate((而不是executeQuery((。在此处查看官方MySQL示例。
替换此行
res = stmt->executeQuery("INSERT INTO users (fName, lName, age) VALUES ('fname', 'lname', 25)"); // Perform a query and get the results. Notice that we use "executeQuery" to get results back
使用以下行(您可能需要一个新的.h文件(:
sql::PreparedStatement *pstmt;
pstmt = con->prepareStatement("INSERT INTO users (fName, lName, age)
VALUES ('fname', 'lname', 25)");
res = pstmt->executeUpdate();
delete pstmt;
您也可以尝试使用execute((,如此stackoverflow问题所示。函数execute((用于通用sql命令,但可能与更高的函数(返回布尔值(中的返回值不像详细词一样。
相关文章:
- strncpy之后的char数组的错误行为
- 为什么在编译此代码时会出现此错误?(在~之后调用terminate)
- 错误:函数声明符之后的预期函数体
- strcpy_s之后的strcat_s给出了错误
- 是什么导致了构造函数之后的这些"意外令牌"错误?
- 分段错误发生在索引离开C++数组边界之后
- 插入MySQL DB中的表之后,错误消息
- 如果在 C++ 中的 malloc() 之后调用 free() 则会出现错误
- 我正在尝试在Eclipse中构建一个C 项目,但是即使在使用MINGW路径设置路径变量之后,也会获得构建错误
- 在 std::move 之后使用对象不会导致编译错误
- progam在std :: getline AM之后停止响应,代码中是否存在错误
- C 错误:在公众之后,没有在此范围内声明此范围
- 在 GetTransfer 之后,我在帐户 1 收到未分配变量错误。我无法弄清楚我搞砸了什么逻辑错误
- 在方法 getline C++ 之后,开关状态反应错误
- 欧拉项目任务#8,代码在某个点之后开始返回错误的答案
- 为什么我的 char* 在循环中分配了一个字符串,但之后却"(null)"?编译器错误?
- 在包含windows.h:语法错误之后无法定义枚举
- 在pop_back()之后无法访问vector(分段错误)
- 在继承错误之后访问方法
- 为什么在有条件地包含stdafx.h之后会出现编译错误?