创建MYSQL对象,初始化并建立链接,设置默认字符集
MYSQL* mysql = mysql_init(NULL);
if (mysql_real_connect(mysql, "127.0.0.1", "root", "yjy", "order_system2", 3306, NULL, 0) == NULL){
printf("mysql连接失败%s\n", mysql_error(mysql));
return NULL;
}
mysql_set_character_set(mysql, "utf8");
插入数据
bool Insert(const Json::Value& dish){
char sql[1024 * 4] = { 0 };
sprintf(sql, "insert into dish_table values(null,'%s',%d)", dish["name"].asCString(), dish["price"].asInt());
int ret = mysql_query(mysql_, sql);
if (ret != 0){
printf("DishTable Insert failed! %s\n", mysql_error(mysql_));
return false;
}
printf("DishTable Insert Ok!\n");
return true;
}