c++
  • mysql
  • select
  • mysql++
  • 2010-09-23 33 views 2 likes 
    2

    我讀過的教程中,我一般得到的是如何工作的: http://tangentsoft.net/mysql++/doc/html/userman/tutorial.html#simple問題用mysql ++庫

    我想建立這個MySQL ++代碼,我得到一個錯誤:

    std::ostringstream query3; 
    query3<<"select pipe_id from pipe where version_id='"<<id<<"'"; 
    std::storeQueryResult ares=query3.store(); 
    
    for(size_t i=0;i<ares.num_rows();i++) 
        cout<<ares[i]["version_id"]<<ares[i]["pipe_id"]<<std::endl; 
    
    mysql_query(&mysql,query3.str().c_str()); 
    

    錯誤是store不是ostringstream的成員。我不知道如何解決問題。


    Merlyn嗨,

    感謝您的代碼,看着我的問題。

    我試圖上面的代碼,但再次我正在錯誤

    錯誤:請求部件「連接」「查詢」,其是非類類型「MYSQL *」

    在這條線的代碼

    // Construct a query object with the query string mysqlpp::Query query = 
    connection.query(query_string); 
    

    請問哪裏有問題?

    +0

    請編輯您的代碼,使其看起來像真正的C++代碼。 – Arkadiy 2010-09-23 19:38:27

    +0

    請嘗試更清楚您想要做什麼以及您的實際問題是什麼。 – 2010-09-23 21:31:52

    +0

    根據他以前的問題編輯該問題。 – 2010-09-23 21:45:55

    回答

    6

    問題是你必須使用mysql ++查詢對象來執行查詢,而不是ostringstream。 ostringstream只是讓你建立查詢字符串,但不會讓你執行查詢。

    有一個教程,顯示在基本用法: http://tangentsoft.net/mysql++/doc/html/userman/tutorial.html#simple

    從代碼到這樣的作品,你需要把你的動態查詢,把它變成一個字符串,並用它來構建查詢mysql ++查詢對象。

    // todo: create the connection here 
    
    // Construct the query string. You were already doing this in your code 
    std::ostringstream query_builder; 
    query_builder << "select pipe_id from pipe where version_id='" << id << "'"; 
    
    // Convert the ostringstream to a string 
    std::string query_string = query_builder.str(); 
    
    // Construct a query object with the query string 
    mysqlpp::Query query = connection.query(query_string); 
    
    // Perform the query 
    mysqlpp::StoreQueryResult result = query.store(); 
    for(size_t i = 0; i < result.num_rows(); i++) 
        std::cout << result[i]["version_id"] << result[i]["pipe_id"] << std::endl; 
    
    +0

    如果'connection.query(query_string)'不起作用,你可以嘗試'connection.query(query_string.c_str())'。 – 2010-09-23 21:57:16

    相關問題