创建具有特定值的特定格式的char*

Creating a char* of specific format with specific values

本文关键字:定格 格式 char 创建      更新时间:2023-10-16

我正试图使用程序其他区域给定的值,以特定格式创建char*类型,values((括号内的值是程序给定的值。格式应该是这样的:

char* sql = "INSERT INTO RecurringEvents (title,description,duration,recurtype,startfrom,endingtype,dateend,occurences,venueid) "  
"VALUES ('title','description','duration','recurtype','startfrom','endingtype','dateend',occurences,venueid); "

正如您所看到的,文本值必须在"标点符号内,而int值则单独保留,因此通常的命令可能是这样的:

"INSERT INTO RecurringEvents (title,description,duration,recurtype,startfrom,endingtype,dateend,occurences,venueid) "  
"VALUES ('thetitle','thedesc','theduration','recurtype','startfrom','enddddtype','dateend',2,4); ";

下面是需要此操作的函数,并不是说这一切都很重要,但为了解释,它将事件的(类(数据全部转换为string/int值,这样它们就可以用来形成INSERT命令(这就是问题所在(,然后在数据库上执行,一旦完成此操作(并且验证了记录的可用性(,它就会被添加到向量中,数据库就会关闭。

void addRecEvent(newRecurringEvent event, vector <newRecurringEvent> &events){
sqlite3 *db;
int rc;
char *sql;
int tableCheck;
char *zErrMsg = 0;
rc = sqlite3_open("data.sqlite", &db);
string title = event.getTitle();
string description = event.getDescription();
string duration = to_string(event.getDuration());
string recurType = recToString(event.getRecurType());
string startfrom = to_string( event.getStartFrom());
string endingtype = etypeToStr(event.getEndingType());
string dateend = to_string(event.getDateEnd());
int occurences = event.getOccurences();
int venueid = event.getVenuid();
/*CREATE INSERT COMMAND USING char*sql  IN FORMAT REQUIRED*/
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); //execute the command
if (rc != SQLITE_OK){
cout << stderr << "SQL error: %s n", zErrMsg;
}
else{
cout << stdout << "Records created succesfully";
events.push_back(event);
}
sqlite3_close(db);

}

我曾经试图在另一个函数中创建全字符串格式(通过将值传递给它(,然后将其作为char*返回,但遇到了在文本字段(如标题、描述等(上使用单引号的问题。

很抱歉,如果这些内容令人困惑,但简而言之,我只想按照第一段代码中的格式形成一个字符序列,该序列使用给定的值来形成其序列。感谢您的帮助,因为我是c++的新手。

whozcraig留下的评论解决了我的问题,我必须使用一个准备好的语句将我的值馈送到语句