再用mysql存储过程,网上用c api调用的例子比较少
请问谁有一些调用例子,不胜感激,谢谢

解决方案 »

  1.   

    简单示例:
    if(!mysql_init(&mysql))
           {
                  printf("mysql_init failed!\n");
                  return 0;
           }
    //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;
           }
      //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));
    ql_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);
    while((record = mysql_fetch_row(results))) {
                  printf("[%s]-[%s]-[%s]\n", record[0], record[1],record[2]);
           }
    mysql_free_result(results);
    mysql_close(&mysql);
      

  2.   

    需要有mysql的C++connector包,mysql官网上有
      

  3.   

    MySQL的官方手册中就有这些函数的说明和例子。21.6.5.1. MySQL Connector/C++ Connecting to MySQL
    A connection to MySQL is established by retrieving an instance of sql::Connection from a sql::mysql::MySQL_Driver object. A sql::mysql::MySQL_Driver object is returned by sql::mysql::MySQL_Driver::get_mysql_driver_instance(). sql::mysql::MySQL_Driver *driver;
    sql::Connection *con;driver = sql::mysql::MySQL_Driver::get_mysql_driver_instance();
    con = driver->connect("tcp://127.0.0.1:3306", "user", "password");delete con;
    Make sure that you free the sql::Connection object as soon as you do not need it any more. But do not explicitly free the connector object! 
      

  4.   

    21.6.5.2. MySQL Connector/C++ Running a simple query
    For running simple queries you can use the methods sql::Statement::execute(), sql::Statement::executeQuery() and sql::Statement::executeUpdate(). The method sql::Statement::execute() should be used if your query does not return a result set or if your query returns more than one result set. See the examples/ directory for more on this. sql::mysql::MySQL_Driver *driver;
    sql::Connection *con;
    sql::Statement *stmtdriver = sql::mysql::get_mysql_driver_instance();
    con = driver->connect("tcp://127.0.0.1:3306", "user", "password");stmt = con->createStatement();
    stmt->execute("USE " EXAMPLE_DB);
    stmt->execute("DROP TABLE IF EXISTS test");
    stmt->execute("CREATE TABLE test(id INT, label CHAR(1))");
    stmt->execute("INSERT INTO test(id, label) VALUES (1, 'a')");delete stmt;
    delete con;
    Note that you have to free sql::Statement and sql::Connection objects explicitly using delete. 
      

  5.   

    21.6.5.3. MySQL Connector/C++ Fetching results
    The API for fetching result sets is identical for (simple) statments and prepared statements. If your query returns one result set you should use sql::Statement::executeQuery() or sql::PreparedStatement::executeQuery() to run your query. Both methods return sql::ResultSet objects. The preview version does buffer all result sets on the client to support cursors. // ...
    sql::Connection *con;
    sql::Statement *stmt
    sql::ResultSet  *res;
    // ...
    stmt = con->createStatement();
    // ...res = stmt->executeQuery("SELECT id, label FROM test ORDER BY id ASC");
    while (res->next()) {  
      // You can use either numeric offsets...
      cout << "id = " <&;t; res->getInt(0);
      // ... or column names for accessing results. »
        The latter is recommended.
      cout << ", label = '" << »
        res->getString("label") << "'" << endl;
    }delete res;
    delete stmt;
    delete con;
    Note that you have to free sql::Statement, sql::Connection and sql::ResultSet objects explicitly using delete. The usage of cursors is demonstrated in the examples contained in the download package. 
    
      

  6.   

    21.6.5.5. MySQL Connector/C++ Complete Example 1
    The following code shows a complete example of how to use MySQL Connector/C++: /* Copyright 2008 Sun Microsystems, Inc.This program is free software; you can redistribute it and/or modify
    it under only the terms of the GNU General Public License as published by
    the Free Software Foundation; version 2 of the License.There are special exceptions to the terms and conditions of the GPL
    as it is applied to this software. View the full text of the
    exception in file EXCEPTIONS-CONNECTOR-C++ in the directory of this
    software distribution.This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
    *//* Standard C++ includes */
    #include <stdlib.h>
    #include <iostream>/*
      Include directly the different
      headers from cppconn/ and mysql_driver.h + mysql_util.h
      (and mysql_connection.h). This will reduce your build time!
    */
    #include "mysql_connection.h"#include <cppconn/driver.h>
    #include <cppconn/exception.h>
    #include <cppconn/resultset.h>
    #include <cppconn/statement.h>using namespace std;int main(void)
    {
    cout << endl;
    cout << "Running 'SELECT 'Hello World!' »
       AS _message'..." << endl;try {
      sql::Driver *driver;
      sql::Connection *con;
      sql::Statement *stmt;
      sql::ResultSet *res;  /* Create a connection */
      driver = get_driver_instance();
      con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
      /* Connect to the MySQL test database */
      con->setSchema("test");  stmt = con->createStatement();
      res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
      while (res->next()) {
        cout << "\t... MySQL replies: ";
        /* Access column data by alias or column name */
        cout << res->getString("_message") << endl;
        cout << "\t... MySQL says it again: ";
        /* Access column fata by numeric offset, 1 is the first column */
        cout << res->getString(1) << endl;
      }
      delete res;
      delete stmt;
      delete con;} catch (sql::SQLException &e) {
      cout << "# ERR: SQLException in " << __FILE__;
      cout << "(" << __FUNCTION__ << ") on line " »
         << __LINE__ << endl;
      cout << "# ERR: " << e.what();
      cout << " (MySQL error code: " << e.getErrorCode();
      cout << ", SQLState: " << e.getSQLState() << " )" << endl;
    }cout << endl;return EXIT_SUCCESS;
    }
    
      

  7.   

    21.6.5.6. MySQL Connector/C++ Complete Example 2
    The following code shows a complete example of how to use MySQL Connector/C++: /* Copyright 2008 Sun Microsystems, Inc.This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; version 2 of the License.There are special exceptions to the terms and conditions of the GPL
    as it is applied to this software. View the full text of the
    exception in file EXCEPTIONS-CONNECTOR-C++ in the directory of this
    software distribution.This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
    *//* Standard C++ includes */
    #include <stdlib.h>
    #include <iostream>/*
      Include directly the different
      headers from cppconn/ and mysql_driver.h + mysql_util.h
      (and mysql_connection.h). This will reduce your build time!
    */
    #include "mysql_connection.h"#include <cppconn/driver.h>
    #include <cppconn/exception.h>
    #include <cppconn/resultset.h>
    #include <cppconn/statement.h>
    #include <cppconn/prepared_statement.h>using namespace std;int main(void)
    {
    cout << endl;
    cout << "Let's have MySQL count from 10 to 1..." << endl;try {
      sql::Driver *driver;
      sql::Connection *con;
      sql::Statement *stmt;
      sql::ResultSet *res;
      sql::PreparedStatement *pstmt;  /* Create a connection */
      driver = get_driver_instance();
      con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
      /* Connect to the MySQL test database */
      con->setSchema("test");  stmt = con->createStatement();
      stmt->execute("DROP TABLE IF EXISTS test");
      stmt->execute("CREATE TABLE test(id INT)");
      delete stmt;  /* '?' is the supported placeholder syntax */
      pstmt = con->prepareStatement("INSERT INTO test(id) VALUES (?)");
      for (int i = 1; i <= 10; i++) {
        pstmt->setInt(1, i);
        pstmt->executeUpdate();
      }
      delete pstmt;  /* Select in ascending order */
      pstmt = con->prepareStatement("SELECT id FROM test ORDER BY id ASC");
      res = pstmt->executeQuery();  /* Fetch in reverse = descending order! */
      res->afterLast();
      while (res->previous())
        cout << "\t... MySQL counts: " << res->getInt("id") << endl;
      delete res;  delete pstmt;
      delete con;} catch (sql::SQLException &e) {
      cout << "# ERR: SQLException in " << __FILE__;
      cout << "(" << __FUNCTION__ << ") on line " »
         << __LINE__ << endl;
      cout << "# ERR: " << e.what();
      cout << " (MySQL error code: " << e.getErrorCode();
      cout << ", SQLState: " << e.getSQLState() << »
         " )" << endl;
    }cout << endl;return EXIT_SUCCESS;
    }
    
      

  8.   

    Thanks ,我现在在用mysql++这个库,觉得也不错