簡體   English   中英

如何更新已經存在的 cout 語句的內容

[英]How to update contents of an already existing cout statement

我正在使用 c++ 中的 mySql 創建一個應用程序,問題就像我們可以在網站上編輯我們的帖子和評論一樣,我想從 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");
}

實際 output:

enter id: 1 嘿,這是一個測試帖 //不可編輯 按 b 退出


預期結果:

輸入 id: 1 嘿,這是一個新帖子//可編輯

您似乎正在嘗試制作某種交互式應用程序,允許用戶編輯表中的值。 這不能通過更改已經寫入 output 的內容來實現。 相反,您可以提示用戶輸入來自cin的新值(就像按下“B”按鈕一樣)。 然后您可以將更新的值存儲在表中。 像這樣的東西(未經測試的代碼):

cout << "current value: " << strBuff[1000] << endl;
cout << "Enter new value (ENTER to keep value): " << flush;
string newvalue;
cin >> newvalue;
if ( newvalue.size() > 0 ) {
  stringstream update;
  update << "UPDATE se__dbms SET content = " << newvalue << " WHERE id = '" << id << "'";
  // execute the SQL statement
  ...
}

如果您需要看起來更像表格編輯器的東西(用戶可以就地編輯數據),那么您將不得不使用像 ncurses 這樣的東西,它允許您在控制台中的某些位置讀取字符,甚至是某種圖形用戶界面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM