MySQL 连接器/C++ 1.1 开发人员指南 / 第 6 章连接器/C++:使用示例 /
6.2 运行简单查询
要运行简单的查询,您可以使用
sql::Statement::execute()
、
sql::Statement::executeQuery()
和
sql::Statement::executeUpdate()
方法。sql::Statement::execute()
如果您的查询不返回结果集或者您的查询返回多个结果集,请使用该方法。有关详细信息,请参阅
examples
目录。
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
driver = 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;
笔记
您必须使用 显式释放sql::Statement
和
sql::Connection
对象
delete
。