mysql sql語句 C++變數 數字型變數 整形變數 字串變數

來源:互聯網
上載者:User

c++版本中的例子,編碼問題在另一個文章裡:http://blog.csdn.net/lbd2008/article/details/8426778

string name = "test";

int age  = 23;

string sql_insert = "insert into mytest (name,age) values (  '   " + name + "  ' , " + IntToStr(age)+ " ) ; " ;

其中IntToStr()實現將int轉換為string類型,這個方法是自己想出來的,網上說到的數字型變數直接用 “+ age+ ”這種形式在vs2010裡編譯通不過,還望高手多多指教!

PS:當要插入的變數含有單引號等逸出字元時,需要提前處理下。比如 I‘m happy ,轉換成I’‘m happy ,這樣sql語句才不會出錯。其他的轉移字元可以自己轉換下。

string IntToStr(int num){
stringstream ss;
ss.clear();
ss << num;
return ss.str();
}

全部代碼為:

#include <iostream>#include <string>#include <sstream>#include <libxl.h>#include <Windows.h>#include "winsock.h"  #include "mysql.h"using namespace std;using namespace libxl;string IntToStr(int num){stringstream ss;ss.clear();ss << num;return ss.str();}int main(int argc, char* argv[]){const char *host = "localhost";//主機const char *user = "root";//使用者名稱const char *passwd = "abc123";//密碼const char *db = "test";//資料庫名unsigned int port = 3306;//mysql連接埠const char *unix_socket = 0;unsigned long client_flag = 0;MYSQL mysql;MYSQL_RES *result;MYSQL_FIELD *field;    MYSQL_ROW sql_row;string name = "中國";int age = 12;string sql_insert = "insert into mytest (name,age) values ('"+name+"',"+IntToStr(age)+");";string sql_select = "select * from mytest where age = " + IntToStr(age)+"";cout<<sql_insert<<endl;cout<<sql_select<<endl;mysql_library_init(0,NULL,NULL);//初始化MYSQL C APImysql_init(&mysql);mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"GBK");if(mysql_real_connect(&mysql,host,user,passwd,db,port,unix_socket,client_flag)){cout<<"database connection OK!"<<endl;/*if(!mysql_query(&mysql,sql_insert.c_str())){cout<<"insert success!!!!!!!!"<<endl;}else{cout<<mysql_error(&mysql)<<endl;}*/int res = mysql_query(&mysql,sql_select.c_str());if(!res){//mysql_query執行成功返回0cout<<"success!"<<endl;result = mysql_store_result(&mysql);if(result){int i,j;                cout<<"number of result: "<<(unsigned long)mysql_num_rows(result)<<endl;cout<<endl;                //擷取列名cout<<"num of cols: "<<mysql_num_fields(result)<<endl;cout<<endl;field = mysql_fetch_fields(result);for(i = 0; i < mysql_num_fields(result); i++){cout<<field[i].name<<"";}cout<<endl;while(sql_row = mysql_fetch_row(result)){for(i=0;i<mysql_num_fields(result);i++)                    {                        cout<<sql_row[i]<<"";                    }                    cout<<endl;}}}else{cout<<mysql_error(&mysql)<<endl;}}system("pause");return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.