c/c++調用mysql預存程序,並獲得傳回值

來源:互聯網
上載者:User

文章來自:http://blog.csdn.net/jccz_zys/archive/2007/07/08/1682810.aspx

    mysql5.0以後就支援預存程序了,目前mysql的6.0Alpha版也已經推出。6.0不僅支援大型資料庫如oracle等的絕大部分功能,如預存程序、視圖、觸發器、job等等,而且修正了這些功能所存在的bug,其中6.0.1還支援64位windows,以及資料表空間。

在c/c++中訪問mysql,常見的只是一些簡單的業務,使用c中嵌入簡單的查詢、插入、更新等操作即可。隨著業務的複雜化,完成一個業務功能需要非常多的sql操作環節,把這些語句都嵌入c代碼中會導致c代碼越來越繁瑣、不清晰,這時候,自然就想到了預存程序來封裝所有的資料庫邏輯,通過c簡單調用mysql預存程序介面即可達到目的,極大地減輕了c程式員的工作量,也便於前端業務處理邏輯與資料庫處理邏輯的分離。下面就介紹c語言調用預存程序的簡單的方法。 1、首先建立一張表 用於存放使用者資訊Create table student(      id int auto_increment,       name varchar(20),       age tinyint,       remark varchar(50),        primary key(id) ); 2、插入幾條資訊 Insert into student values(1,"zhouys",90, "");commit; 3、查看使用者資訊 mysql> select * from student;+------+-----------+------+----------+| id   | name   | age | remark | +------+-----------+------+----------+|    1 | zhouys |   90 |        | +------+-----------+------+-----------+1 row in set (0.00 sec) mysql> 4、建立預存程序 如下:delimiter // create procedure querystudent(       in in_id int ,   #0- 字元id 1-數字id            #        out out_ret int,                # 返回結果       out out_name varchar(20),        # 名字       out out_age   int                # 年齡     )label_a:begin       declare v_name varchar(20) ;        declare v_age tinyint ;        # 參數判斷       if (in_id<=0) then               set out_ret=-1; #id error               leave label_a;        end if;                      SELECT name,age into v_name,v_age from student where id=in_id limit 1;               if v_age is NULL then               set out_ret=-2; #don't found               leave label_a;        end if;                          set out_ret=0;        set out_name=v_name;        set out_age=v_age; end;//delimiter ; 5、c語言調用預存程序 調用方法或步驟: 5.1 、初始化 Mysql 控制代碼 if(!mysql_init(&mysql))       {               printf("mysql_init failed!/n");               return 0;        } 5.2 、串連到 mysql //login or connect       if(!mysql_real_connect(&mysql,"localhost","root","","billingdb",0,NULL,CLIENT_MULTI_STATEMENTS))        {               printf("mysql_real_connect() failed!/n");               mysql_close(&mysql);               return 0;        } 5.3 、調用預存程序        //call        strcpy(query,"call querystudent (1,@ret,@ out_name,@ out_age)");        printf("query sql=[%s]/n",query);     ret= mysql_real_query(&mysql,query,(unsigned int)strlen(query)); 5.4 、查詢結果集並儲存 mysql_query(&mysql, "SELECT @ret,@ out_name,@ out_age ");        //get result        if (ret)     {               printf("Error exec query: %s/n",mysql_error(&mysql));     }     else        {               printf("[%s] exec.../n", query);     }         results = mysql_store_result(&mysql); 5.5 、擷取查詢結果 while((record = mysql_fetch_row(results))) {              printf("[%s]-[%s]-[%s]/n", record[0], record[1],record[2]);        } 一般預存程序只會有一行的返回結果,^_^. 5.6 、釋放資源與 mysql 串連控制代碼 mysql_free_result(results);mysql_close(&mysql); 6、結束語        Mysql 預存程序可以實現相當強大的功能,這裡只是拋磚引玉的做一些實踐,期望能夠給大家一些協助。也歡迎大家交流…

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.