C++MySQL C api用户输入行

C++ MySQL c api user input into rows

本文关键字:输入 用户 api C++MySQL      更新时间:2023-10-16

尝试获取用户输入以将数据存储在Mysql数据库中。连接到数据库后,我有这个代码

....
string itemName;
double itemPrice;
int itemInv;
cout << "Add New Item: " << endl << "Item Name: " << endl;
cin >> itemName;
cin.ignore(256, 'n');
cout << "Item Price: " << endl;
cin >> itemPrice;
cin.ignore(256, 'n');
cout << "Item Count: " << endl;
cin >> itemInv;
cin.ignore(256, 'n');
if (mysql_query(con, "INSERT INTO kitchen (itemID, itemName, itemPrice, itemInv) VALUES(itemID, itemName, itemPrice, itemInv)")) {
finish_with_errors(con);
}
mysql_close(con);
exit(0);

它在输入信息后抛出语法错误。

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'itemInv) VALUES(`itemID`,` itemName`, `itemPrice`, `itemInv`)' at line 1

我想这里的问题是如何接受用户输入并将其作为VALUES传递到数据库中?

首先,感谢那些发表评论的人。因为没有一个建议真正涉及到我想要的东西。既然我已经解决了这个问题,我决定发布答案。。。。所以,我的查询语法不是问题所在,也不是真正的问题所在。我的问题更多的是关于C++以及如何接受用户输入并从中进行查询。这就是我最终要做的。。。

string itemName;
string itemPrice;
string itemInv;
cout << "Add New Item: " << endl << "Item Name: " << endl;
cin >> itemName;
cin.ignore(256, 'n');
cout << "Item Price: " << endl;
cin >> itemPrice;
cin.ignore(256, 'n');
cout << "Item Count: " << endl;
cin >> itemInv;
cin.ignore(256, 'n');
//Add user input to query
string sqlString =  "INSERT INTO kitchen (itemID, itemName, itemPrice, itemInv) VALUES(itemId," + itemName + "," + itemPrice + "," + itemInv + ")";
const char *newString = sqlString.c_str();
//Add data to database
if (mysql_query( con, newString )) {
finish_with_errors(con);
}
mysql_close(con);
exit(0);
}