覆盖字符串的引号C++消失

The quotes that cover the string disappear C++

本文关键字:C++ 消失 字符串 覆盖      更新时间:2023-10-16

我尝试用引号覆盖字符串,以便将其放入SQL记录中

int pictureId = picture.getId();
std::string pictureName = "'" + picture.getName() + "'";
std::string picturePath = "'" + picture.getPath() + "'";
std::string pictureCreationTime = "'" + picture.getCreationDate() + "'";
std::string TRY = "Insert Into Pictures(Id, Name, Location, Creation_Date, 
Album_id) Values(" + std::to_string(picture.getId()) + ',' + 
picture.getName() + "," + picture.getPath() + ',' + 
picture.getCreationDate() + ',' + '1' + ");";
res = sqlite3_exec(db, TRY.c_str(), nullptr, nullptr, &errMessage);

行后

std::string TRY = "Insert Into Pictures(Id, Name, Location, Creation_Date, 
Album_id) Values(" + std::to_string(picture.getId()) + ',' + 
picture.getName() + "," + picture.getPath() + ',' + 
picture.getCreationDate() + ',' + '1' + ");";

引号消失我能做什么才能使报价不会消失

单引号消失在您说导致问题的行上,您使用 picture.getName(( 而不是使用您使用单引号创建的 pictureName 字符串。

int pictureId = picture.getId();
std::string pictureName = "'" + picture.getName() + "'";
std::string picturePath = "'" + picture.getPath() + "'";
std::string pictureCreationTime = "'" + picture.getCreationDate() + "'";
std::string TRY = "Insert Into Pictures(Id, Name, Location, Creation_Date, 
Album_id) Values(" + std::to_string(pictureId ) + ',' + 
pictureName  + "," + picturePath + ',' + 
pictureCreationTime + ',' + '1' + ");";