2011-03-08 15 views
0
#include <iostream> 
#include <mysql++.h> 
using namespace std; 

int main() { 
    // Get database access parameters from command line 
    const char* db = "enet", *server = "192.168.1.108", *user = "root", *pass = 
      "123456"; 
    // Connect to the sample database. 
    mysqlpp::Connection conn(false); 
    conn.set_option(new mysqlpp::MultiStatementsOption(true)); 
    if (conn.connect(db, server, user, pass)) { 
     mysqlpp::Query query = conn.query(); 
     query << "call CreateTable('test1', 'generic', 0, 1, 2, 3,4,5,6,7,8,9,10,NOW());"; 
     query.execute(); 
     query.reset(); 

     query << "call CreateTable('test2', 'generic', 0, 1, 2, 3,4,5,6,7,8,9,10,NOW());"; 
     query.execute(); 
     query.reset(); 

     return 0; 
    } else { 
     cerr << "DB connection failed: " << conn.error() << endl; 
     return 1; 
    } 

    return 0; 
} 

我想用mysql ++查詢來執行過程「CreateTable」多次,並且我重置查詢在最後,但不管怎麼樣,只是第一個查詢有效,最後一個沒有,我的問題是: 如何使所有的查詢工作?如何重新使用MySQL ++查詢對象來調用多個存儲過程?

-- create table -- 
delimiter $$ 
drop procedure if exists CreateTable $$ 
create procedure CreateTable(
    IN tableName VARCHAR(20), 
    IN dbName VARCHAR(20), 
    IN INT_RegDevID INTEGER, 
    IN Dec_Long DECIMAL(24,16), 
    IN Dec_Lat DECIMAL(24,16), 
    IN Dec_Height DECIMAL(10,6), 
    IN Dec_Direction DECIMAL(10,6), 
    IN AverageSpeed DECIMAL(10,6), 
    IN Dec_Base VARCHAR(10), 
    IN MCC INTEGER, 
    IN MNC INTEGER, 
    IN LAC INTEGER, 
    IN CI INTEGER, 
    IN Dec_LocaDate TIMESTAMP) 
-- ------------------------------------------------------------------------------- 
-- ------------------------------------------------------------------------------- 
begin 
    -- the test variable 
    -- Warning: the encoding can result many problem!!! 
    declare varTableName VARCHAR(32) default NULL; 
    set @varTableName = NULL; 
    set @table_prefix = "posinfo_"; 
    set @table_params = "(
      `Int_LocaID` int(11) NOT NULL auto_increment, 
      `INT_RegDevID` int(11) NOT NULL default '0', 
      `Dec_Long` decimal(24,16) NOT NULL default '0.0000000000000000', 
      `Dec_Lat` decimal(24,16) NOT NULL default '0.0000000000000000', 
      `Dec_Height` decimal(10,6) NOT NULL default '0.000000', 
      `Dec_Direction` decimal(10,6) NOT NULL default '0.000000', 
      `Dec_ MaxSpeed` decimal(10,6) NOT NULL default '0.000000', 
      `Dec_ MinSpeed` decimal(10,6) NOT NULL default '0.000000', 
      `AverageSpeed` decimal(10,6) NOT NULL default '0.000000', 
      `Var_PosInfo` varchar(50) character set latin1 NOT NULL default '', 
      `Var_Remark` varchar(200) character set latin1 NOT NULL default '', 
      `Date_LocaDate` timestamp NOT NULL default CURRENT_TIMESTAMP, 
      `Dec_Base` varchar(10) character set latin1 NOT NULL, 
      `MCC` int(11) NOT NULL COMMENT '', 
      `MNC` int(11) NOT NULL COMMENT '', 
      `LAC` int(11) NOT NULL COMMENT '', 
      `CI` int(11) NOT NULL COMMENT '', 
      PRIMARY KEY (`Int_LocaID`) 
     ) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=gbk;"; 
    set @varCreate = CONCAT("create table ", dbName,".",@table_prefix, tableName, @table_params); 
    -- the insert operation 
    set @insertOperation = CONCAT("insert into ", dbName,".",@table_prefix, tableName, 
     "(INT_RegDevID,Dec_Long,Dec_Lat,Dec_Height,Dec_Direction,AverageSpeed, 
     Dec_Base,MCC,MNC,LAC,CI,Date_LocaDate) values(",INT_RegDevID,",",Dec_Long, 
     ",",Dec_Lat,",",Dec_Height,",",Dec_Direction,",",AverageSpeed,",",Dec_Base, 
     ",",MCC,",",MNC,",",LAC,",",CI,",NOW())"); 
    -- find the target table 
    -- Look care about the "' '" ! 
    set @getTargetTable = CONCAT("select TABLE_NAME into @varTableName from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='", 
     dbName, "' and TABLE_NAME='", @table_prefix, tableName,"'"); 

    -- ------------------------------------------------------------------------------- 
    -- ------------------------------------------------------------------------------- 
    PREPARE getTargetTable from @getTargetTable; 
    execute getTargetTable; 
    select @varTableName; 
    set varTableName = @varTableName; 

    if varTableName is NULL then 
     -- create new table 
     PREPARE newTable 
     from @varCreate; 
     execute newTable; 
     -- do insert operation 
     PREPARE insertOperation 
     from @insertOperation; 
     execute insertOperation; 

    else 
     -- do insert operation 
     PREPARE insertOperation 
     from @insertOperation; 
     execute insertOperation; 
    end if; 

end $$ 
delimiter ; 

以上是程序。

+0

這是什麼意思「它不工作」? – 2011-03-08 07:03:19

+0

這意味着,代碼只是創建表'test1',表'test2'丟失,爲什麼? – liunx 2011-03-08 07:49:01

回答

1

有幾種錯誤的位置:

  1. 您關閉了異常(conn(false)),但你也不能爲錯誤代碼檢查返回值。您的第二個​​呼叫失敗,但沒有詢問Query對象爲什麼,您正在失明。儘管如此,我認爲它允許MySQL ++拋出異常(conn())並將整個事件包裝在try塊中,而不是將錯誤檢查添加到所有MySQL ++調用中。

  2. 您不需要MultiStatementsOption就可以按照您當前顯示的方式進行操作。你在這裏有兩個單獨的陳述,而不是一個複合陳述。結合分號可能會讓MySQL感到困惑,這就是爲什麼第二次調用失敗的原因。

    mysql命令行工具需要分號來終止SQL語句,但是當使用數據庫API(如MySQL ++)時,它們只需要分隔多個語句。

    您可以將兩個CREATE語句組合成一個字符串(和一個​​),或者可以刪除分號和MultiStatementsOption

  3. reset()查詢之間的調用自從MySQL ++ 2. x以來沒有必要。該方法仍然可用的唯一原因是,如果要重用已用於模板查詢的Query對象,則有必要;他們是唯一仍然不能自動重置的類型,原因相當明顯。

+0

非常感謝您的提示! – liunx 2011-03-09 01:38:15

相關問題