呼叫mysql
連結選項:-lmysqlcppconn
獲取查詢結果
使用繫結變數
異常處理
呼叫儲存過程
呼叫
獲取儲存過程的out引數
獲取儲存過程輸出的結果集
一個stmt有多個結果集時用stmt->getMoreResults()檢視是否還有結果集
點選(此處)摺疊或開啟
-
#include <mysql_driver.h>
-
#include <mysql_connection.h>
-
#include <mysql_error.h>
-
-
int main()
-
{
-
sql::mysql::MySQL_Driver* driver;
-
sql::Connection* con;
-
-
driver = sql::mysql::get_mysql_driver_instance();
-
con = driver->connect("tcp://127.0.0.1:1101", "root", "oracle");
-
if(con->isValid()) cout << "ok" << endl;
-
else con->reconnect();
-
delete con;
-
}
-
-
con->isValid() 測試連結
-
con->reconnect() 重新連線
- 必須釋放con物件
獲取查詢結果
點選(此處)摺疊或開啟
-
#include <stdio.h>
-
#include <iostream>
-
#include <string>
-
#include <vector>
-
#include <mysql_driver.h>
-
#include <mysql_connection.h>
-
#include <mysql_error.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()
-
{
-
sql::mysql::MySQL_Driver *driver;
-
sql::Connection* conn;
-
sql::Statement * stmt;
-
sql::ResultSet * rest;
-
-
driver = sql::mysql::get_mysql_driver_instance();
-
conn = driver->connect("tcp://127.0.0.1:1101", "root", "oracle");
-
cout << "connect ok" << endl << endl;
-
-
stmt = conn->createStatement();
-
stmt->execute("use test"); // 也可以先conn->setSchema(xxxx);
-
stmt->execute("drop table t");
-
stmt->execute("create table t(id int, name varchar(10))");
-
stmt->execute("insert into t values (1, 'xxx')");
-
stmt->execute("insert into t values (2, 'yyy')");
-
stmt->execute("insert into t values (3, 'zzz')");
-
stmt->execute("commit");
-
rest = stmt->executeQuery("select * from t");
-
-
sql::ResultSetMetaData* md = rest->getMetaData();
-
for(int i=0; i<md->getColumnCount(); i++) printf("%10s", md->getColumnLabel(i+1).c_str());
-
cout << endl;
-
while (rest->next()) printf("%10d%10s\n", rest->getInt(1), rest->getString(2).c_str());
-
-
delete rest;
-
delete stmt;
-
delete conn;
-
}
-
- 每次執行查詢都要釋放其產生的resultset
使用繫結變數
點選(此處)摺疊或開啟
-
int main()
-
{
-
sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance();
-
sql::Connection* conn = driver->connect("tcp://192.168.1.201:1101", "root", "oracle");
-
sql::Statement * stmt = conn->createStatement();
-
stmt->execute("use test");
-
stmt->execute("drop table t");
-
stmt->execute("create table t(id int, name varchar(10))");
-
delete stmt;
-
-
char name[5];
-
memset(name, '0', 5);
-
sql::PreparedStatement* pst1 = conn->prepareStatement("insert into t values (?, ?)");
-
sql::PreparedStatement* pst2 = conn->prepareStatement("select * from t where id = ?");
-
sql::ResultSet* rest = NULL;
-
for(int i=0; i<100; i++)
-
{
-
sprintf(name, "%d", i);
-
pst1->setInt(1, i);
-
pst1->setString(2, string(name));
-
pst1->executeUpdate();
-
}
-
for(int i=0; i<100; i++)
-
{
-
pst2->setInt(1, i);
-
rest = pst2->executeQuery();
-
while (rest->next()) printf("%10d%10s\n", rest->getInt(1), rest->getString(2).c_str());
-
delete rest;
-
}
-
delete pst1;
-
delete pst2;
-
delete rest;
-
delete conn;
- }
異常處理
點選(此處)摺疊或開啟
-
try {
-
...
-
} catch (sql::SQLException &e) {
-
cout << "# ERR: " << e.what();
-
cout << " (MySQL error code: " << e.getErrorCode();
-
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
- }
呼叫儲存過程
點選(此處)摺疊或開啟
-
DELIMITER //
-
create procedure add_t (in iid int, in iname varchar(10)) begin insert into t values (iid, iname); end;
-
create procedure get_t_name (in iid int, out oname varchar(10)) begin select max(name) into oname from t where id = iid; end;
-
create procedure get_t_id (in iname varchar(10), out oid int) begin select max(id) into oid from t where name = iname; end;
-
create procedure get_t_cnt (out cnt int) begin select count(*) into cnt from t; end;
-
create procedure get_t ()
-
begin
-
select id from t;
-
select name from t;
-
end;
-
//
- DELIMITER ;
呼叫
點選(此處)摺疊或開啟
- stmt->execute("call add_t(1, 'asd')");
獲取儲存過程的out引數
點選(此處)摺疊或開啟
-
stmt->execute("call get_t_name(1, @name)");
-
std::auto_ptr<sql::ResultSet> res(stmt->executeQuery("select @name as _name"));
-
while (res->next()) cout << "name = " << res->getString("_name") << endl;
-
-
stmt->execute("call get_t_id('asd', @id)");
-
res.reset(stmt->executeQuery("select @id as _id"));
-
while (res->next()) cout << "id = " << res->getInt("_id") << endl;
-
-
stmt->execute("call get_t_cnt(@cnt)");
-
res.reset(stmt->executeQuery("select @cnt as _cnt"));
- while (res->next()) cout << "count = " << res->getInt("_cnt") << endl;
獲取儲存過程輸出的結果集
點選(此處)摺疊或開啟
-
stmt->execute("call get_t");
-
std::auto_ptr<sql::ResultSet> res;
-
do {
-
res.reset(stmt->getResultSet());
-
while (res->next()) cout << "id: " << res->getInt("id") << " name: " << res->getString("name") << endl;
- } while (stmt->getMoreResults());
一個stmt有多個結果集時用stmt->getMoreResults()檢視是否還有結果集
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26239116/viewspace-2125604/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL外掛呼叫MySql
- mybatis mysql 呼叫檢視MyBatisMySql
- MySQL:MySQL層比較函式呼叫MySql函式
- mysql如何呼叫儲存過程MySql儲存過程
- Python呼叫MySQL模組初試PythonMySql
- MySQL 儲存函式及呼叫MySql儲存函式
- MySQL入門--如何呼叫命令列命令MySql命令列
- 用PHP呼叫MySQL儲存過程PHPMySql儲存過程
- c / c + + 呼叫mysql儲存過程MySql儲存過程
- MySQL儲存過程語句及呼叫MySql儲存過程
- mysql 儲存過程,以及mybatis如何呼叫MySql儲存過程MyBatis
- PHP呼叫MYSQL儲存過程例項PHPMySql儲存過程
- mysql-定時呼叫儲存過程MySql儲存過程
- 利用Zipkin追蹤Mysql資料庫呼叫鏈MySql資料庫
- mysql多次呼叫儲存過程的問題MySql儲存過程
- mysql儲存過程及c#呼叫標準版MySql儲存過程C#
- php呼叫mysql儲存過程和函式的方法(轉)PHPMySql儲存過程函式
- C語言呼叫mysql資料庫API實現簡單的mysql客戶端的功能C語言MySql資料庫API客戶端
- qt呼叫js,js呼叫qtQTJS
- Swoole+Lumen:同步程式設計風格呼叫MySQL非同步查詢程式設計MySql非同步
- C程式函式呼叫&系統呼叫C程式函式
- 呼叫鏈系列(2):輕呼叫鏈實現
- java呼叫c++動態庫之jni呼叫JavaC++
- EOS JAVA 呼叫Java
- api呼叫方式API
- Java呼叫ZookeeperJava
- java呼叫JavaScriptJavaScript
- Runloop & 方法呼叫OOP
- app 呼叫介面APP
- webservice介面呼叫Web
- Zuul- 呼叫Zuul
- 反射呼叫DLL反射
- Delphi呼叫javasoJava
- java 呼叫javascriptJavaScript
- chatgpt tools呼叫ChatGPT
- 記 Laravel 呼叫 Gin 介面呼叫 formData 上傳檔案LaravelORM
- Dubbo原始碼分析(十)同步呼叫與非同步呼叫原始碼非同步
- 呼叫鏈系列四:呼叫鏈上下文傳遞