【发布时间】:2019-11-12 06:27:25
【问题描述】:
我正在使用 c++ 中的 mySql 创建一个应用程序,问题就像我们可以在网站上编辑我们的帖子和 cmets,我想从 mySql 表中获取一个单元格并想要编辑特定的单元格。
例如:我正在获取一条记录,上面写着“嘿,这是一个测试帖子”,当我在我的应用程序中获取记录时,用户可以将帖子更新为“嘿,这是一个新帖子”。
现在的问题是,我无法更新 cout 语句。 如何获取记录、显示并在显示时修改其内容?
我尝试使用 strcpy 并通过获取记录并将其复制到字符串中来将其保存在本地字符串中,但这并没有按预期工作
void editPost(){
system("cls");
string dummy;
MYSQL* conn;
MYSQL_ROW row;
MYSQL_RES* res;
conn = mysql_init(0);
conn = mysql_real_connect(conn, "192.168.0.110", "admin", "admin", "search_engine", 0, NULL, 0);
string strBuff[1000];
if(conn){
int qstate = 0;
int id;
cout << "Enter id : ";
cin>>id;
stringstream ss;
ss<<"SELECT content FROM se__dbms where id = '" << id <<"'";
string query = ss.str();
const char* q = query.c_str();
if(conn){
int qstate = mysql_query(conn, q);
if(!qstate){
res = mysql_store_result(conn);
while(row = mysql_fetch_row(res)){
strBuff[1000]=row[0];
}
cout<<strBuff[1000]; /*problem over here,
what can i change in this cout statement
that it will even display the contents,
and the user can edit it as well */
ss << "UPDATE se__dbms SET content = " << strBuff << " WHERE id = '" << id << "'";
}
}
if(qstate == 0){
cout << "Record Updated..." << endl;
cout << "Press B to go back";
cin >> dummy;
}
else{
cout << "Insert Error" << mysql_error(conn) << endl;
cout << "Press B to go back";
cin >> dummy;
}
}else{
cout << "Connection Error" << endl;
cout << "Press B to go back";
cin >> dummy;
}
system("pause");
system("cls");
}
实际输出:
输入编号:1 嘿,这是一个测试帖子//不可编辑 按 b 退出
预期结果:
输入编号:1 嘿,这是一个新帖子//可编辑
【问题讨论】:
-
C++ 中没有任何东西可以重写已经写出的输出。为此,您需要一个像 ncurses 这样的库。此外,您对
strBuff[]的使用都是错误的,导致代码中出现未定义的行为 -
只是在检查我可以使其工作的不同方法,因此使用了 strBuff [],正如你所说我应该使用 ncurses,我可以使用它的代码块吗?我需要导入库吗?你能分享更多的见解吗?
标签: c++ command-line-interface