创建具有特定值的特定格式的字符*

时间:2018-07-18 15:46:16

标签: c++ sqlite type-conversion

我正在尝试使用程序其他区域给出的值以特定格式创建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); ";

下面是需要此功能的函数,它并不是很重要,而是要说明,它将事件的(类)数据全部转换为字符串/整数值,因此可用于形成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 ++的新手。

1 个答案:

答案 0 :(得分:0)

whozcraig留下的评论解决了我的问题,我必须使用准备好的语句将我的价值观提供给该语句

相关问题