c++mysql c api用户输入到行中

g52tjvyc  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(286)

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

....
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

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

3pmvbmvn

3pmvbmvn1#

首先,感谢那些评论。因为没有一个建议真正涉及到我在寻找什么。既然我已经解决了这个问题,我决定把答案贴出来。。。。所以,我的查询语法不是问题所在,也不是真正的问题所在。我的问题更多的是关于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);
}

相关问题