// testmysqlapi.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" //#include "stdlib.h" //#include #include #include "mysql_driver.h" #include "mysql_connection.h" #include #include #include //Creating tables, inserting rows (simple) #include //Creating tables, inserting rows (prepared) using namespace std; void testExample1(); void testExample2(); void testInsert(); int _tmain(int argc, _TCHAR* argv[]) { // testExample1() ; // testExample2() ; testInsert() ; return 0; } void testExample1() { cout << endl; cout << "testExample1() begin" << 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", "jiaoge"); /* 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 << "testExample1() end" << endl; cout << endl; } void testExample2() { cout << endl; cout << "testExample2() begin" << 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", "jiaoge"); /* 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 << "testExample2() end" << endl; cout << endl; } void testInsert() { cout << endl; cout << "testInsert() begin" << endl; try { //加上编译选项 /GX 或者是 /EHsc sql::Driver *driver; sql::Connection *con; sql::Statement *stmt; sql::PreparedStatement *pstmt; sql::ResultSet *res; /* Create a connection */ driver = get_driver_instance(); con = driver->connect("tcp://127.0.0.1:3306", "root", "jiaoge"); //建立数据库连接 端口 用户 密码 stmt = con->createStatement(); res = stmt->executeQuery("SHOW DATABASES LIKE '%test%'"); //判断数据库是否存在 if(res->rowsCount() == 0) { stmt->execute("CREATE DATABASE test"); //创建数据库 } delete res; char cc; /* Connect to the MySQL test database */ con->setSchema("test"); //连接到数据库 res = stmt->executeQuery("SHOW TABLES LIKE '%test_insert%'"); //判断表是否存在 if(res->rowsCount() > 0) { delete res; res = stmt->executeQuery("SELECT id FROM test_insert LIMIT 1");//判断表中是否有数据 if(res->rowsCount() > 0) { cout << "删除旧数据? y/n" << endl; cin >> cc; if(cc == 'y' || cc == 'Y') { stmt->execute("DELETE FROM test_insert"); //删除表中的数据 cout << "已删除原始记录" << endl; } else { cout << "保留原始记录" << endl; } } } else { cout << "创建表test_insert" << endl; stmt->execute("DROP TABLE IF EXISTS test_insert"); //删除表,如果表存在 stmt->execute("CREATE TABLE test_insert(\ id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,\ tagname CHAR(128) NOT NULL,\ tagtype SMALLINT,\ tagvalueinteger INT,\ tagvaluefloat DOUBLE,\ tagvaluestring varchar(64),\ tagtimestamp DATETIME,\ tagtimemsel SMALLINT)"); //创建表 } delete res; int nCount = 0; cout << "输入要插入的记录条数 (单位:万)" << endl; cin >> nCount; DWORD dwBeginTime = 0; DWORD dwEndTime = 0; if(nCount > 0) { std::string str = "testname"; std::string strValue = "0.00"; char szTm[64]; SYSTEMTIME sys; int nValue = 0; double dbValue = 0.0f; dwBeginTime = GetTickCount(); /* '?' is the supported placeholder syntax */ pstmt = con->prepareStatement("INSERT INTO test_insert(\ id,tagname,tagtype,tagvalueinteger,tagvaluefloat,tagvaluestring,tagtimestamp,tagtimemsel) \ VALUES (?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),\ (?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),\ (?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),\ (?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),\ (?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),\ (?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),\ (?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),\ (?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?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?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?)"); for(int i=0; isetInt(nRow*8+1,0); pstmt->setString(nRow*8+2,str); pstmt->setInt(nRow*8+3,1); pstmt->setInt(nRow*8+4,nValue); pstmt->setDouble(nRow*8+5,dbValue); pstmt->setString(nRow*8+6,strValue); GetLocalTime( &sys ); sprintf_s(szTm,"%d-%02d-%02d %02d:%02d:%02d", sys.wYear,sys.wMonth,sys.wDay, sys.wHour,sys.wMinute,sys.wSecond); pstmt->setDateTime(nRow*8+7,szTm); pstmt->setInt(nRow*8+8,sys.wMilliseconds); nValue++; dbValue = dbValue+1.0f; } pstmt->executeUpdate(); } } delete pstmt; dwEndTime = GetTickCount(); printf("插入操作费时%d毫秒", (dwEndTime-dwBeginTime)); cout << endl; } cout << "是否进行检索 y/n" << endl; cin >> cc; if(cc == 'y' || cc == 'Y') { cout << "检索点名=testname 按时间段和最大记录条数查询" << endl; char szTimeBegin[24] = {0}; char szTimeEnd[24] = {0}; int nMaxCount = 0; for(int ii=0; ii<100; ii++) { cout << "输入起始时间段 如:20100415090000" << endl; cin >> szTimeBegin; if(strlen(szTimeBegin) < 14) { cout << "输入时间格式非法,请重新输入..." << endl; memset(szTimeBegin, 0, sizeof(szTimeBegin)); } else { break; } } for(int jj=0; jj<100; jj++) { cout << "输入结束时间段 如:20100415110000" << endl; cin >> szTimeEnd; if(strlen(szTimeEnd) < 14) { cout << "输入时间格式非法,请重新输入..." << endl; memset(szTimeEnd, 0, sizeof(szTimeEnd)); } else { break; } } for(int kk=0; kk<100; kk++) { cout << "输入最大查询条数 如:1000" << endl; cin >> nMaxCount; if(nMaxCount <= 0) { cout << "请重新输入最大查询条数..." << endl; } else { break; } } dwBeginTime = GetTickCount(); char szSqlQueryCmd[256] = {0}; sprintf_s(szSqlQueryCmd, "SELECT * FROM test_insert WHERE tagname = 'testname' AND tagtimestamp >= '%s' \ AND tagtimestamp <= '%s' LIMIT %d", szTimeBegin, szTimeEnd, nMaxCount); pstmt = con->prepareStatement(szSqlQueryCmd); res = pstmt->executeQuery(); printf("检索到%d条有效数据", res->rowsCount()); cout << endl; dwEndTime = GetTickCount(); printf("检索操作费时%d毫秒", (dwEndTime-dwBeginTime)); cout << endl; if(res->rowsCount() > 0) { cout << "是否输出检索记录 y/n" << endl; cin >> cc; if(cc == 'y' || cc == 'Y') { res->beforeFirst(); while (res->next()) { cout << "\t点名: " << (res->getString(2)).data(); cout << "\t值: " << res->getDouble(5) << " "; cout << res->getInt64(7) << " "; printf("ddd %s \x0", (res->getString(7)).data()); // cout << "\t时标: " << (res->getString(7)).data() << " "; cout << res->getInt(8); cout << endl; } } } delete pstmt; 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 << "testInsert() end" << endl; cout << "press any key to exit" <> e; }