使用mysql连接c ++程序时出现意外错误

时间:2012-05-05 03:55:26

标签: c++ mysql visual-studio-2010 connector

我正在开发一个连接到mySQL数据库所需的visual C ++ 2010中的c ++控制台应用程序。我使用wamp服务器为mysql和mySQL C ++连接器连接。代码适用于读取数据库,但是当我尝试插入数据时,它会产生意外错误。有没有人有这样的经历?

这是我的完整代码: (代码输出为:“SQL错误:错误消息:未知异常”)

    // Standad C++ includes
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

// Include the Connector/C++ headers
#include "cppconn/driver.h"
#include "cppconn/exception.h"
#include "cppconn/resultset.h"
#include "cppconn/statement.h"
#include "cppconn/sqlstring.h"

// Link to the Connector/C++ library
#pragma comment(lib, "mysqlcppconn.lib")

// Specify our connection target and credentials
const string server   = "tcp://127.0.0.1:3306";
const string username = "cashif";
const string password = "111222"; // No password - NEVER DO THIS ON A PRODUCTION SERVER!

int main()
{
    sql::Driver     *driver; // Create a pointer to a MySQL driver object
    sql::Connection *dbConn; // Create a pointer to a database connection object
    sql::Statement  *stmt;   // Create a pointer to a Statement object to hold our SQL commands
    sql::ResultSet  *res;    // Create a pointer to a ResultSet object to hold the results of any queries we run

    // Try to get a driver to use to connect to our DBMS
    try
    {
        driver = get_driver_instance();
    }
    catch (sql::SQLException e)
    {
        cout << "Could not get a database driver. Error message: " << e.what() << endl;
        system("pause");
        exit(1);
    }

    // Try to connect to the DBMS server
    try
    {
        dbConn = driver->connect(server, username, password);
    }
    catch (sql::SQLException e)
    {
        cout << "Could not connect to database. Error message: " << e.what() << endl;
        system("pause");
        exit(1);
    }

    stmt = dbConn->createStatement(); // Specify which connection our SQL statement should be executed on

    // Try to query the database
    try
    {
        stmt->execute("use dengue_test");              // Select which database to use. Notice that we use "execute" to perform a command.

        stmt->execute("insert into patients values(3,\"Amina\",\"Iqbal Town\""); // Perform a query and get the results. Notice that we use "executeQuery" to get results back
    }
    catch (sql::SQLException e)
    {
        cout << "SQL error. Error message: " << e.what() << endl;
        system("pause");
        exit(1);
    }

    // While there are still results (i.e. rows/records) in our result set...
/*
    while (res->next())
    {
        // ...get each field we want and output it to the screen
        // Note: The first field/column in our result-set is field 1 (one) and -NOT- field 0 (zero)
        // Also, if we know the name of the field then we can also get it directly by name by using:
        // res->getString("TheNameOfTheField");
        cout << res->getString(1) << " - " << res->getString(2) << " - " << res->getString(3) << endl;                
    }
    */
    // Clean up after ourselves
    //delete res;
    delete stmt;
    delete dbConn;

    system("pause");
    return 0;
}

1 个答案:

答案 0 :(得分:1)

你的SQL中有两个错误,一个是MySQL可以让你逃脱,另一个是不会。你也养成了一个坏习惯。

这两个错误都在这里:

stmt->execute("insert into patients values(3,\"Amina\",\"Iqbal Town\"");

SQL中的字符串文字使用单引号,而不是双引号; MySQL会让你逃脱这一点。但是,你也有一个缺少的右括号,MySQL不喜欢那一点。你应该这样说:

stmt->execute("insert into patients values(3, 'Amina', 'Iqbal Town')");

不指定SQL INSERT中的列是一个坏习惯,所以你应该这样说:

insert into patients (col1, col2, col3) values (...)

其中col1和朋友当然是真正的列名。表中的列没有任何明确定义的顺序,因此您应该具体避免有趣的错误和维护噩梦。