bool CSqlCommand::connect(char *host, char *use, char *passwd, char *db, db_uint16 nPort) { if (m_bIsValid) { return true; } if (m_pMySql) { if (mysql_ping(m_pMySql) == 0) { m_bIsValid = true; return true; } close(); } m_pMySql = mysql_init(NULL); if (m_pMySql == NULL) { return false; } //char value = 1; //mysql_options(m_pMySql, MYSQL_OPT_RECONNECT, (char*)&value); // 设置MYSQL_OPT_RECONNECT 为1,开启自动重连 //mysql_options(m_pMySql, MYSQL_SET_CLIENT_IP, "10.221.152.156"); if (mysql_real_connect(m_pMySql, host, use, passwd, db, nPort, NULL, CLIENT_MULTI_STATEMENTS | CLIENT_INTERACTIVE)) { if(mysql_set_character_set(m_pMySql, "utf8") != 0) { MYSQLERROR(m_pMySql); close(); return false; } //if (mysql_query(m_pMySql, "SET sql_mode='NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'") != 0) //{ // MYSQLERROR(m_pMySql); // close(); // return false; //} m_bIsValid = true; return true; } else { MYSQLERROR(m_pMySql); return false; } return false; }
static void run_application(MYSQL &mysql, Ndb_cluster_connection &cluster_connection, const char* table, const char* db) { /******************************************** * Connect to database via mysql-c * ********************************************/ char db_stmt[256]; sprintf(db_stmt, "CREATE DATABASE %s\n", db); mysql_query(&mysql, db_stmt); sprintf(db_stmt, "USE %s", db); if (mysql_query(&mysql, db_stmt) != 0) MYSQLERROR(mysql); create_table(mysql, table); /******************************************** * Connect to database via NdbApi * ********************************************/ // Object representing the database Ndb myNdb( &cluster_connection, db ); if (myNdb.init()) APIERROR(myNdb.getNdbError()); /* * Do different operations on database */ do_insert(myNdb, table); do_update(myNdb, table); do_delete(myNdb, table); do_read(myNdb, table); /* * Drop the table */ mysql_query(&mysql, db_stmt); }
/*********************************** * Drop a table named MYTABLENAME ***********************************/ static void drop_table(MYSQL &mysql) { if (mysql_query(&mysql, "DROP TABLE" " MYTABLENAME")) MYSQLERROR(mysql); }
static void run_application(MYSQL &mysql, Ndb_cluster_connection &cluster_connection) { /******************************************** * Connect to database via mysql-c * ********************************************/ mysql_query(&mysql, "CREATE DATABASE TEST_DB_1"); if (mysql_query(&mysql, "USE TEST_DB_1") != 0) MYSQLERROR(mysql); create_table(mysql); /******************************************** * Connect to database via NdbApi * ********************************************/ // Object representing the database Ndb myNdb( &cluster_connection, "TEST_DB_1" ); if (myNdb.init()) APIERROR(myNdb.getNdbError()); /* * Do different operations on database */ do_insert(myNdb); do_update(myNdb); do_delete(myNdb); do_read(myNdb); drop_table(mysql); mysql_query(&mysql, "DROP DATABASE TEST_DB_1"); }
/*********************************** * Drop a table named api_async1 ***********************************/ static void drop_table(MYSQL &mysql) { if (mysql_query(&mysql, "DROP TABLE" " api_async1")) MYSQLERROR(mysql); }
void create_table(MYSQL &mysql) { if (try_create_table(mysql)) { if (mysql_errno(&mysql) != ER_TABLE_EXISTS_ERROR) MYSQLERROR(mysql); std::cout << "MySQL Cluster already has example table: api_blob_ndbrecord. " << "Dropping it..." << std::endl; /****************** * Recreate table * ******************/ drop_table(mysql); if (try_create_table(mysql)) MYSQLERROR(mysql); } }
/******************************* * Drop a table named by table *******************************/ static void drop_table(MYSQL &mysql, const char* table) { char drop_stmt[256]; sprintf(drop_stmt, "DROP TABLE IF EXISTS %s", table); if (mysql_query(&mysql, drop_stmt)) MYSQLERROR(mysql); }
/********************************************************* * Create a table named MYTABLENAME if it does not exist * *********************************************************/ static void create_table(MYSQL &mysql) { if (mysql_query(&mysql, "CREATE TABLE" " MYTABLENAME" " (ATTR1 INT UNSIGNED NOT NULL PRIMARY KEY," " ATTR2 INT UNSIGNED NOT NULL)" " ENGINE=NDB")) MYSQLERROR(mysql); }
/********************************************************* * Create a table named by table if it does not exist * *********************************************************/ static void create_table(MYSQL &mysql, const char* table) { char create_stmt[256]; sprintf(create_stmt, "CREATE TABLE %s \ (ATTR1 INT UNSIGNED NOT NULL PRIMARY KEY,\ ATTR2 INT UNSIGNED NOT NULL)\ ENGINE=NDB", table); if (mysql_query(&mysql, create_stmt)) MYSQLERROR(mysql); }
int main(int argc, char** argv) { if (argc != 3) { std::cout << "Arguments are <socket mysqld> <connect_string cluster>.\n"; exit(-1); } // ndb_init must be called first ndb_init(); // connect to mysql server and cluster and run application { char * mysqld_sock = argv[1]; const char *connectstring = argv[2]; // Object representing the cluster Ndb_cluster_connection cluster_connection(connectstring); // Connect to cluster management server (ndb_mgmd) if (cluster_connection.connect(4 /* retries */, 5 /* delay between retries */, 1 /* verbose */)) { std::cout << "Cluster management server was not ready within 30 secs.\n"; exit(-1); } // Optionally connect and wait for the storage nodes (ndbd's) if (cluster_connection.wait_until_ready(30,0) < 0) { std::cout << "Cluster was not ready within 30 secs.\n"; exit(-1); } // connect to mysql server MYSQL mysql; if ( !mysql_init(&mysql) ) { std::cout << "mysql_init failed\n"; exit(-1); } if ( !mysql_real_connect(&mysql, "localhost", "root", "", "", 0, mysqld_sock, 0) ) MYSQLERROR(mysql); // run the application code run_application(mysql, cluster_connection); } ndb_end(0); return 0; }
/********************************************************* * Create a table named api_async1 if it does not exist * *********************************************************/ static void create_table(MYSQL &mysql) { while(mysql_query(&mysql, "CREATE TABLE api_async1" " (ATTR1 INT UNSIGNED NOT NULL PRIMARY KEY," " ATTR2 INT UNSIGNED NOT NULL)" " ENGINE=NDB")) { if (mysql_errno(&mysql) == ER_TABLE_EXISTS_ERROR) { std::cout << "MySQL Cluster already has example table: api_scan. " << "Dropping it..." << std::endl; drop_table(mysql); } else MYSQLERROR(mysql); } }
/** * Function to create table */ void create_table(MYSQL &mysql) { while (mysql_query(&mysql, "CREATE TABLE" " api_scan" " (REG_NO INT UNSIGNED NOT NULL," " BRAND CHAR(20) NOT NULL," " COLOR CHAR(20) NOT NULL," " PRIMARY KEY USING HASH (REG_NO))" " ENGINE=NDB")) { if (mysql_errno(&mysql) != ER_TABLE_EXISTS_ERROR) MYSQLERROR(mysql); std::cout << "MySQL Cluster already has example table: api_scan. " << "Dropping it..." << std::endl; drop_table(mysql); } }
int main(int argc, char** argv) { if (argc != 3) { std::cout << "Arguments are <socket mysqld> <connect_string cluster>.\n"; exit(-1); } char * mysqld_sock = argv[1]; const char *connectstring = argv[2]; ndb_init(); MYSQL mysql; /************************************************************** * Connect to mysql server and create table * **************************************************************/ { if ( !mysql_init(&mysql) ) { std::cout << "mysql_init failed\n"; exit(-1); } if ( !mysql_real_connect(&mysql, "localhost", "root", "", "", 0, mysqld_sock, 0) ) MYSQLERROR(mysql); mysql_query(&mysql, "CREATE DATABASE ndb_examples"); if (mysql_query(&mysql, "USE ndb_examples") != 0) MYSQLERROR(mysql); create_table(mysql); } /************************************************************** * Connect to ndb cluster * **************************************************************/ Ndb_cluster_connection cluster_connection(connectstring); if (cluster_connection.connect(4, 5, 1)) { std::cout << "Unable to connect to cluster within 30 secs." << std::endl; exit(-1); } // Optionally connect and wait for the storage nodes (ndbd's) if (cluster_connection.wait_until_ready(30,0) < 0) { std::cout << "Cluster was not ready within 30 secs.\n"; exit(-1); } Ndb* myNdb = new Ndb( &cluster_connection, "ndb_examples" ); // Object representing the database if (myNdb->init(1024) == -1) { // Set max 1024 parallel transactions APIERROR(myNdb->getNdbError()); } /** * Initialise transaction array */ for(int i = 0 ; i < 10 ; i++) { transaction[i].used = 0; transaction[i].conn = 0; } int i=0; /** * Do 10 insert transactions. */ while(i < 10) { while(populate(myNdb,i,0)<0) // <0, no space on free list. Sleep and try again. milliSleep(10); i++; } std::cout << "Number of temporary errors: " << tempErrors << std::endl; delete myNdb; }
/** * Drop table GARAGE */ void drop_table(MYSQL &mysql) { if (mysql_query(&mysql, "DROP TABLE GARAGE")) MYSQLERROR(mysql); }
int main(int argc, char** argv) { if (argc != 3) { std::cout << "Arguments are <socket mysqld> <connect_string cluster>.\n"; exit(-1); } char * mysqld_sock = argv[1]; const char *connectstring = argv[2]; ndb_init(); Ndb_cluster_connection *cluster_connection= new Ndb_cluster_connection(connectstring); // Object representing the cluster int r= cluster_connection->connect(5 /* retries */, 3 /* delay between retries */, 1 /* verbose */); if (r > 0) { std::cout << "Cluster connect failed, possibly resolved with more retries.\n"; exit(-1); } else if (r < 0) { std::cout << "Cluster connect failed.\n"; exit(-1); } if (cluster_connection->wait_until_ready(30,30)) { std::cout << "Cluster was not ready within 30 secs." << std::endl; exit(-1); } // connect to mysql server MYSQL mysql; if ( !mysql_init(&mysql) ) { std::cout << "mysql_init failed\n"; exit(-1); } if ( !mysql_real_connect(&mysql, "localhost", "root", "", "", 0, mysqld_sock, 0) ) MYSQLERROR(mysql); /******************************************** * Connect to database via mysql-c * ********************************************/ mysql_query(&mysql, "CREATE DATABASE ndb_examples"); if (mysql_query(&mysql, "USE ndb_examples") != 0) MYSQLERROR(mysql); create_table(mysql); Ndb* myNdb= new Ndb( cluster_connection, "ndb_examples" ); // Object representing the database if (myNdb->init() == -1) { APIERROR(myNdb->getNdbError()); exit(-1); } const NdbDictionary::Dictionary* myDict= myNdb->getDictionary(); const NdbDictionary::Table *myTable= myDict->getTable("api_retries"); if (myTable == NULL) { APIERROR(myDict->getNdbError()); return -1; } /************************************ * Execute some insert transactions * ************************************/ std::cout << "Ready to insert rows. You will see notices for temporary " "errors, permenant errors, and retries. \n"; for (int i = 10000; i < 20000; i++) { executeInsertTransaction(i, myNdb, myTable); } std::cout << "Done.\n"; delete myNdb; delete cluster_connection; ndb_end(0); return 0; }
int main(int argc, char**argv) { if (argc != 3) { std::cout << "Arguments are <socket mysqld> <connect_string cluster>.\n"; exit(-1); } char *mysqld_sock = argv[1]; const char *connectstring = argv[2]; ndb_init(); MYSQL mysql; /* Connect to mysql server and create table. */ { if ( !mysql_init(&mysql) ) { std::cout << "mysql_init failed.\n"; exit(-1); } if ( !mysql_real_connect(&mysql, "localhost", "root", "", "", 0, mysqld_sock, 0) ) MYSQLERROR(mysql); mysql_query(&mysql, "CREATE DATABASE ndb_examples"); if (mysql_query(&mysql, "USE ndb_examples") != 0) MYSQLERROR(mysql); create_table(mysql); } /* Connect to ndb cluster. */ Ndb_cluster_connection cluster_connection(connectstring); if (cluster_connection.connect(4, 5, 1)) { std::cout << "Unable to connect to cluster within 30 secs." << std::endl; exit(-1); } /* Optionally connect and wait for the storage nodes (ndbd's). */ if (cluster_connection.wait_until_ready(30,0) < 0) { std::cout << "Cluster was not ready within 30 secs.\n"; exit(-1); } Ndb myNdb(&cluster_connection,"ndb_examples"); if (myNdb.init(1024) == -1) { // Set max 1024 parallel transactions APIERROR(myNdb.getNdbError()); exit(-1); } setup_records(&myNdb); if(populate(&myNdb) > 0) std::cout << "populate: Success!" << std::endl; if(update_key(&myNdb) > 0) std::cout << "update_key: Success!" << std::endl; if(update_scan(&myNdb) > 0) std::cout << "update_scan: Success!" << std::endl; if(fetch_key(&myNdb) > 0) std::cout << "fetch_key: Success!" << std::endl; if(update2_key(&myNdb) > 0) std::cout << "update2_key: Success!" << std::endl; if(delete_key(&myNdb) > 0) std::cout << "delete_key: Success!" << std::endl; return 0; }
int main(int argc, char** argv) { if (argc != 3) { std::cout << "Arguments are <socket mysqld> <connect_string cluster>.\n"; exit(-1); } char * mysqld_sock = argv[1]; const char *connectstring = argv[2]; ndb_init(); MYSQL mysql; /************************************************************** * Connect to mysql server and create table * **************************************************************/ { if ( !mysql_init(&mysql) ) { std::cout << "mysql_init failed\n"; exit(-1); } if ( !mysql_real_connect(&mysql, "localhost", "root", "", "", 0, mysqld_sock, 0) ) MYSQLERROR(mysql); mysql_query(&mysql, "CREATE DATABASE ndb_examples"); if (mysql_query(&mysql, "USE ndb_examples") != 0) MYSQLERROR(mysql); create_table(mysql); } /************************************************************** * Connect to ndb cluster * **************************************************************/ Ndb_cluster_connection cluster_connection(connectstring); if (cluster_connection.connect(4, 5, 1)) { std::cout << "Unable to connect to cluster within 30 secs." << std::endl; exit(-1); } // Optionally connect and wait for the storage nodes (ndbd's) if (cluster_connection.wait_until_ready(30,0) < 0) { std::cout << "Cluster was not ready within 30 secs.\n"; exit(-1); } Ndb myNdb(&cluster_connection,"ndb_examples"); if (myNdb.init(1024) == -1) { // Set max 1024 parallel transactions APIERROR(myNdb.getNdbError()); exit(-1); } /******************************************* * Check table definition * *******************************************/ int column_color; { const NdbDictionary::Dictionary* myDict= myNdb.getDictionary(); const NdbDictionary::Table *t= myDict->getTable("api_scan"); if(t == NULL) { std::cout << "Dictionary::getTable() failed."; exit(-1); } Car car; if (t->getColumn("COLOR")->getLength() != sizeof(car.color) || t->getColumn("BRAND")->getLength() != sizeof(car.brand)) { std::cout << "Wrong table definition" << std::endl; exit(-1); } column_color= t->getColumn("COLOR")->getColumnNo(); } if(populate(&myNdb) > 0) std::cout << "populate: Success!" << std::endl; if(scan_print(&myNdb) > 0) std::cout << "scan_print: Success!" << std::endl << std::endl; std::cout << "Going to delete all pink cars!" << std::endl; { /** * Note! color needs to be of exact the same size as column defined */ Car tmp; sprintf(tmp.color, "Pink"); if(scan_delete(&myNdb, column_color, tmp.color) > 0) std::cout << "scan_delete: Success!" << std::endl << std::endl; } if(scan_print(&myNdb) > 0) std::cout << "scan_print: Success!" << std::endl << std::endl; { /** * Note! color1 & 2 need to be of exact the same size as column defined */ Car tmp1, tmp2; sprintf(tmp1.color, "Blue"); sprintf(tmp2.color, "Black"); std::cout << "Going to update all " << tmp1.color << " cars to " << tmp2.color << " cars!" << std::endl; if(scan_update(&myNdb, column_color, tmp1.color, tmp2.color) > 0) std::cout << "scan_update: Success!" << std::endl << std::endl; } if(scan_print(&myNdb) > 0) std::cout << "scan_print: Success!" << std::endl << std::endl; /** * Drop table */ drop_table(mysql); return 0; }
int main(int argc, char** argv) { if (argc != 5) { std::cout << "Arguments are <socket mysqld1> <connect_string cluster 1> <socket mysqld2> <connect_string cluster 2>.\n"; exit(-1); } // ndb_init must be called first ndb_init(); { char * mysqld1_sock = argv[1]; const char *connectstring1 = argv[2]; char * mysqld2_sock = argv[3]; const char *connectstring2 = argv[4]; // Object representing the cluster 1 Ndb_cluster_connection cluster1_connection(connectstring1); MYSQL mysql1; // Object representing the cluster 2 Ndb_cluster_connection cluster2_connection(connectstring2); MYSQL mysql2; // connect to mysql server and cluster 1 and run application // Connect to cluster 1 management server (ndb_mgmd) if (cluster1_connection.connect(4 /* retries */, 5 /* delay between retries */, 1 /* verbose */)) { std::cout << "Cluster 1 management server was not ready within 30 secs.\n"; exit(-1); } // Optionally connect and wait for the storage nodes (ndbd's) if (cluster1_connection.wait_until_ready(30,0) < 0) { std::cout << "Cluster 1 was not ready within 30 secs.\n"; exit(-1); } // connect to mysql server in cluster 1 if ( !mysql_init(&mysql1) ) { std::cout << "mysql_init failed\n"; exit(-1); } if ( !mysql_real_connect(&mysql1, "localhost", "root", "", "", 0, mysqld1_sock, 0) ) MYSQLERROR(mysql1); // connect to mysql server and cluster 2 and run application // Connect to cluster management server (ndb_mgmd) if (cluster2_connection.connect(4 /* retries */, 5 /* delay between retries */, 1 /* verbose */)) { std::cout << "Cluster 2 management server was not ready within 30 secs.\n"; exit(-1); } // Optionally connect and wait for the storage nodes (ndbd's) if (cluster2_connection.wait_until_ready(30,0) < 0) { std::cout << "Cluster 2 was not ready within 30 secs.\n"; exit(-1); } // connect to mysql server in cluster 2 if ( !mysql_init(&mysql2) ) { std::cout << "mysql_init failed\n"; exit(-1); } if ( !mysql_real_connect(&mysql2, "localhost", "root", "", "", 0, mysqld2_sock, 0) ) MYSQLERROR(mysql2); // run the application code run_application(mysql1, cluster1_connection, "MYTABLENAME1", "TEST_DB_1"); run_application(mysql2, cluster2_connection, "MYTABLENAME2", "TEST_DB_2"); } // Note: all connections must have been destroyed before calling ndb_end() ndb_end(0); return 0; }
/** * Function to drop table */ void drop_table(MYSQL &mysql) { if (mysql_query(&mysql, "DROP TABLE IF EXISTS api_scan")) MYSQLERROR(mysql); }
int main(int argc, char** argv) { if (argc != 3) { std::cout << "Arguments are <socket mysqld> <connect_string cluster>.\n"; exit(-1); } char * mysqld_sock = argv[1]; const char *connectstring = argv[2]; ndb_init(); Ndb_cluster_connection *cluster_connection= new Ndb_cluster_connection(connectstring); // Object representing the cluster int r= cluster_connection->connect(5 /* retries */, 3 /* delay between retries */, 1 /* verbose */); if (r > 0) { std::cout << "Cluster connect failed, possibly resolved with more retries.\n"; exit(-1); } else if (r < 0) { std::cout << "Cluster connect failed.\n"; exit(-1); } if (cluster_connection->wait_until_ready(30,0) < 0) { std::cout << "Cluster was not ready within 30 secs." << std::endl; exit(-1); } // connect to mysql server MYSQL mysql; if ( !mysql_init(&mysql) ) { std::cout << "mysql_init failed\n"; exit(-1); } if ( !mysql_real_connect(&mysql, "localhost", "root", "", "", 0, mysqld_sock, 0) ) MYSQLERROR(mysql); /******************************************** * Connect to database via mysql-c * ********************************************/ mysql_query(&mysql, "CREATE DATABASE TEST_DB_1"); if (mysql_query(&mysql, "USE TEST_DB_1") != 0) MYSQLERROR(mysql); create_table(mysql); Ndb* myNdb = new Ndb( cluster_connection, "TEST_DB_1" ); // Object representing the database NdbTransaction* myNdbTransaction[2]; // For transactions NdbOperation* myNdbOperation; // For operations if (myNdb->init(2) == -1) { // Want two parallel insert transactions APIERROR(myNdb->getNdbError()); exit(-1); } /****************************************************** * Insert (we do two insert transactions in parallel) * ******************************************************/ const NdbDictionary::Dictionary* myDict= myNdb->getDictionary(); const NdbDictionary::Table *myTable= myDict->getTable("MYTABLENAME"); if (myTable == NULL) APIERROR(myDict->getNdbError()); for (int i = 0; i < 2; i++) { myNdbTransaction[i] = myNdb->startTransaction(); if (myNdbTransaction[i] == NULL) APIERROR(myNdb->getNdbError()); myNdbOperation = myNdbTransaction[i]->getNdbOperation(myTable); if (myNdbOperation == NULL) APIERROR(myNdbTransaction[i]->getNdbError()); myNdbOperation->insertTuple(); myNdbOperation->equal("ATTR1", 20 + i); myNdbOperation->setValue("ATTR2", 20 + i); // Prepare transaction (the transaction is NOT yet sent to NDB) myNdbTransaction[i]->executeAsynchPrepare(NdbTransaction::Commit, &callback, NULL); } // Send all transactions to NDB myNdb->sendPreparedTransactions(0); // Poll all transactions myNdb->pollNdb(3000, 2); // Close all transactions for (int i = 0; i < 2; i++) myNdb->closeTransaction(myNdbTransaction[i]); delete myNdb; delete cluster_connection; drop_table(mysql); ndb_end(0); return 0; }
/* Function to drop table. */ void drop_table(MYSQL &mysql) { if (mysql_query(&mysql, "DROP TABLE api_blob_ndbrecord")) MYSQLERROR(mysql); }
int main(int argc, char** argv) { if (argc != 3) { std::cout << "Arguments are <socket mysqld> <connect_string cluster>.\n"; exit(-1); } char * mysqld_sock = argv[1]; const char *connectstring = argv[2]; ndb_init(); MYSQL mysql; /************************************************************** * Connect to mysql server and create table * **************************************************************/ { if ( !mysql_init(&mysql) ) { std::cout << "mysql_init failed\n"; exit(-1); } if ( !mysql_real_connect(&mysql, "localhost", "root", "", "", 0, mysqld_sock, 0) ) MYSQLERROR(mysql); mysql_query(&mysql, "CREATE DATABASE ndb_examples_1"); if (mysql_query(&mysql, "USE ndb_examples") != 0) MYSQLERROR(mysql); while (mysql_query(&mysql, "CREATE TABLE" " api_simple_index" " (ATTR1 INT UNSIGNED," " ATTR2 INT UNSIGNED NOT NULL," " PRIMARY KEY USING HASH (ATTR1)," " UNIQUE MYINDEXNAME USING HASH (ATTR2))" " ENGINE=NDB")) { if (mysql_errno(&mysql) == ER_TABLE_EXISTS_ERROR) { std::cout << "MySQL Cluster already has example table: api_scan. " << "Dropping it..." << std::endl; mysql_query(&mysql, "DROP TABLE api_simple_index"); } else MYSQLERROR(mysql); } } /************************************************************** * Connect to ndb cluster * **************************************************************/ Ndb_cluster_connection *cluster_connection= new Ndb_cluster_connection(connectstring); // Object representing the cluster if (cluster_connection->connect(5,3,1)) { std::cout << "Connect to cluster management server failed.\n"; exit(-1); } if (cluster_connection->wait_until_ready(30,30)) { std::cout << "Cluster was not ready within 30 secs.\n"; exit(-1); } Ndb* myNdb = new Ndb( cluster_connection, "ndb_examples" ); // Object representing the database if (myNdb->init() == -1) { APIERROR(myNdb->getNdbError()); exit(-1); } const NdbDictionary::Dictionary* myDict= myNdb->getDictionary(); const NdbDictionary::Table *myTable= myDict->getTable("api_simple_index"); if (myTable == NULL) APIERROR(myDict->getNdbError()); const NdbDictionary::Index *myIndex= myDict->getIndex("MYINDEXNAME$unique","api_simple_index"); if (myIndex == NULL) APIERROR(myDict->getNdbError()); /************************************************************************** * Using 5 transactions, insert 10 tuples in table: (0,0),(1,1),...,(9,9) * **************************************************************************/ for (int i = 0; i < 5; i++) { NdbTransaction *myTransaction= myNdb->startTransaction(); if (myTransaction == NULL) APIERROR(myNdb->getNdbError()); NdbOperation *myOperation= myTransaction->getNdbOperation(myTable); if (myOperation == NULL) APIERROR(myTransaction->getNdbError()); myOperation->insertTuple(); myOperation->equal("ATTR1", i); myOperation->setValue("ATTR2", i); myOperation = myTransaction->getNdbOperation(myTable); if (myOperation == NULL) APIERROR(myTransaction->getNdbError()); myOperation->insertTuple(); myOperation->equal("ATTR1", i+5); myOperation->setValue("ATTR2", i+5); if (myTransaction->execute( NdbTransaction::Commit ) == -1) APIERROR(myTransaction->getNdbError()); myNdb->closeTransaction(myTransaction); } /***************************************** * Read and print all tuples using index * *****************************************/ std::cout << "ATTR1 ATTR2" << std::endl; for (int i = 0; i < 10; i++) { NdbTransaction *myTransaction= myNdb->startTransaction(); if (myTransaction == NULL) APIERROR(myNdb->getNdbError()); NdbIndexOperation *myIndexOperation= myTransaction->getNdbIndexOperation(myIndex); if (myIndexOperation == NULL) APIERROR(myTransaction->getNdbError()); myIndexOperation->readTuple(NdbOperation::LM_Read); myIndexOperation->equal("ATTR2", i); NdbRecAttr *myRecAttr= myIndexOperation->getValue("ATTR1", NULL); if (myRecAttr == NULL) APIERROR(myTransaction->getNdbError()); if(myTransaction->execute( NdbTransaction::Commit, NdbOperation::AbortOnError ) != -1) printf(" %2d %2d\n", myRecAttr->u_32_value(), i); myNdb->closeTransaction(myTransaction); } /***************************************************************** * Update the second attribute in half of the tuples (adding 10) * *****************************************************************/ for (int i = 0; i < 10; i+=2) { NdbTransaction *myTransaction= myNdb->startTransaction(); if (myTransaction == NULL) APIERROR(myNdb->getNdbError()); NdbIndexOperation *myIndexOperation= myTransaction->getNdbIndexOperation(myIndex); if (myIndexOperation == NULL) APIERROR(myTransaction->getNdbError()); myIndexOperation->updateTuple(); myIndexOperation->equal( "ATTR2", i ); myIndexOperation->setValue( "ATTR2", i+10); if( myTransaction->execute( NdbTransaction::Commit ) == -1 ) APIERROR(myTransaction->getNdbError()); myNdb->closeTransaction(myTransaction); } /************************************************* * Delete one tuple (the one with primary key 3) * *************************************************/ { NdbTransaction *myTransaction= myNdb->startTransaction(); if (myTransaction == NULL) APIERROR(myNdb->getNdbError()); NdbIndexOperation *myIndexOperation= myTransaction->getNdbIndexOperation(myIndex); if (myIndexOperation == NULL) APIERROR(myTransaction->getNdbError()); myIndexOperation->deleteTuple(); myIndexOperation->equal( "ATTR2", 3 ); if (myTransaction->execute(NdbTransaction::Commit) == -1) APIERROR(myTransaction->getNdbError()); myNdb->closeTransaction(myTransaction); } /***************************** * Read and print all tuples * *****************************/ { std::cout << "ATTR1 ATTR2" << std::endl; for (int i = 0; i < 10; i++) { NdbTransaction *myTransaction= myNdb->startTransaction(); if (myTransaction == NULL) APIERROR(myNdb->getNdbError()); NdbOperation *myOperation= myTransaction->getNdbOperation(myTable); if (myOperation == NULL) APIERROR(myTransaction->getNdbError()); myOperation->readTuple(NdbOperation::LM_Read); myOperation->equal("ATTR1", i); NdbRecAttr *myRecAttr= myOperation->getValue("ATTR2", NULL); if (myRecAttr == NULL) APIERROR(myTransaction->getNdbError()); if(myTransaction->execute( NdbTransaction::Commit, NdbOperation::AbortOnError ) == -1) if (i == 3) { std::cout << "Detected that deleted tuple doesn't exist!\n"; } else { APIERROR(myTransaction->getNdbError()); } if (i != 3) { printf(" %2d %2d\n", i, myRecAttr->u_32_value()); } myNdb->closeTransaction(myTransaction); } } delete myNdb; delete cluster_connection; ndb_end(0); return 0; }
db_int64 CSqlCommand::executeBinary(/*(*/char *sqlstr) //这儿编译不过....改了一下——by:mm { db_int32 _nBindLen = m_nBindLen; m_nBindLen = 0; if (sqlstr == NULL) { PRINTFERROR(); return -1; } if (m_pMySql == NULL) { PRINTFERROR(); m_bIsValid = false; return -1; } MYSQL_STMT *m_pStmt = mysql_stmt_init(m_pMySql); if (m_pStmt == NULL) { MYSQLERROR(m_pMySql); m_bIsValid = false; return -1; } if (mysql_stmt_prepare(m_pStmt, sqlstr, (unsigned long)strlen(sqlstr)) != 0) { MYSQLSTMTERROR_SQL(m_pStmt, sqlstr); mysql_stmt_close(m_pStmt); m_pStmt = NULL; m_bIsValid = false; return -1; } if (mysql_stmt_bind_param(m_pStmt, m_bind) != 0) { MYSQLSTMTERROR_SQL(m_pStmt, sqlstr); mysql_stmt_close(m_pStmt); m_pStmt = NULL; m_bIsValid = false; return -1; } if (mysql_stmt_param_count(m_pStmt) != _nBindLen) { MYSQLSTMTERROR_SQL(m_pStmt, sqlstr); mysql_stmt_close(m_pStmt); m_pStmt = NULL; m_bIsValid = false; return -1; } int err = -1; if ((err = mysql_stmt_execute(m_pStmt)) != 0) { MYSQLSTMTERROR_SQL(m_pStmt, sqlstr); mysql_stmt_close(m_pStmt); m_pStmt = NULL; if(err == 1) // sql执行失败 { if (mysql_ping(m_pMySql) != 0) { m_bIsValid = false; } } // err > 1 需要重新连接 else { m_bIsValid = false; } return -1; } m_nValue = (db_int32)mysql_stmt_affected_rows(m_pStmt); mysql_stmt_close(m_pStmt); m_pStmt = NULL; return m_nValue; }