c++ - 将c++程序与mysql连接时出现意外错误

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

我正在使用 Visual C++ 2010 开发一个 C++ 控制台应用程序,它需要连接到 mySQL 数据库。我正在使用 mysql 的 wamp 服务器和 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;
}

最佳答案

您的 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 和 friends 当然是真正的列名。表中的列没有任何明确定义的顺序,因此您应该具体化以避免有趣的错误和维护噩梦。

关于c++ - 将c++程序与mysql连接时出现意外错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10458705/

相关文章:

c++ - 如何获取动态创建内存的数组大小

c++ - npm安装pomelo错误VC++ 2010?

c++ - 未处理的异常 : Access violation reading location 0x015E2348

mysql - 我在 date_add 之间使用基于用户年龄的高级搜索,但取决于月份和年份不起作用

C# .net 使复选框充当单选按钮

c++ - 如何更改任务管理器中的应用程序图标

php - 禁用唯一的 sql 行结果约束

mysql - 子查询中的派生表无法访问主表

c# - WCF - Visual Studio 不会生成 SVC 文件

c++ - 链接到 Visual Studio 2010 中的静态库