创建具有特定值的特定格式的char*
Creating a char* of specific format with specific values
我正试图使用程序其他区域给定的值,以特定格式创建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留下的评论解决了我的问题,我必须使用一个准备好的语句将我的值馈送到语句
相关文章:
- 如何在openssl-ecc中获取十六进制格式的私钥
- 将"打开的CV图像"中的"颜色"转换为整数格式
- TDateTime格式在C++Builder中不会更改
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 检查不带转换的扫描格式
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- 是否可以从格式字符串中检索"width"
- clang格式:宏的缩进
- clang格式:禁用排序包含
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 将RGB图像保存为PPM格式
- 询问在设计我的手臂模拟器功能表示格式1
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 带有Protobuf序列化的C++Hazelcast:字符串不是UTF-8格式的
- 如何将strftime中的格式错误作为异常捕获
- 将CHW格式的浮点向量转换为cv::Mat
- 如何将二进制格式的 C++ 对象的 std::vector 保存到磁盘?
- 如何以叮当格式设置评论的行长?
- 为什么我需要C++中不同的排序格式来对这个USACO代码上的数组和优先级队列进行排序
- Vulkan 中的动态顶点缓冲区格式设置