简体   繁体   English

无法从C ++在MySQL中插入数据

[英]Unable to insert data in MySQL from C++

Am trying to insert data from C++ console application in mysql database table. 我正在尝试从C ++控制台应用程序在mysql数据库表中插入数据。

Am having table: 有桌子:

CREATE TABLE `Emp` (
  `empId` int(11) NOT NULL AUTO_INCREMENT,
  `empName` varchar(50) NOT NULL,
  PRIMARY KEY (`empId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

Now when I try to insert data, I get success message, but when I cross check it in the database, I am not getting any result. 现在,当我尝试插入数据时,会收到成功消息,但是当我在数据库中进行交叉检查时,没有得到任何结果。

Here's what I give the input to insert: 这是我输入的内容:

crazydeveloper@crazydeveloper:~/Desktop$ g++ InsertInMysql.cpp -o InsertInMysql -L /usr/include/mysql -lmysqlclient -I /usr/include/mysql
crazydeveloper@crazydeveloper:~/Desktop$ ./InsertInMysql

Please Enter Your Name: 
Maddy
Connection Established Successfully.......
Connection Established Successfully.......
Your Name is Maddy
INSERT INTO Emp VALUES('Maddy')
Success....

Update: When I insert 2 words for empName , only 1st word gets inserted: 更新:当我为empName插入2个单词时,只会插入第一个单词:

here's the query 这是查询

Please Enter Your Name: 
Brett Lee
Connection Established Successfully.......
Connection Established Successfully.......
Your Name is Brett
INSERT INTO Emp(empName) VALUES('Brett')

Here's my C++ source code: 这是我的C ++源代码:

#include <iostream>
#include <mysql.h>
#include <my_global.h>

#define SERVER "localhost"
#define USERNAME "root"
#define PASSWORD "root"
#define DATABASE "Test"

using namespace std;

int main() {

    string empFirstName, query;
    int res;

    cout << "\nPlease Enter Your Name: " << endl;
    getline(cin, empFirstName);

    MYSQL *connect;
    connect = mysql_init(NULL);

    if (connect){
        cout << "Connection Established Successfully......." << endl;
    }

    connect = mysql_real_connect(connect, SERVER, USERNAME, PASSWORD, DATABASE, 0,NULL,0);

    if (connect){
        cout << "Connection Established Successfully......." << endl;
        cout << "Your Name is " << empFirstName << endl;
    }

    query = "INSERT INTO Emp VALUES('"+empFirstName+"')";

    cout << query << endl;

    if (mysql_query(connect, query.c_str())){
        cout << "Success.... \n" << endl;
    }   

    mysql_close (connect);

    return 0;   
}

Bug Found 发现错误

Just found the error: When I was not giving the column name empName in the query, the success message was printed. 刚发现错误:当我在查询中未给列名empName时,将显示成功消息。 And now when I am giving the column name empName , the success message is not printed. 现在,当我给列名称empName ,不会显示成功消息。

Query to insert: 查询插入:

// prints success message
query = "INSERT INTO Emp VALUES('"+empFirstName+"')";

Query to insert(after updating): 查询要插入(更新后):

// does not prints success message
query = "INSERT INTO Emp(empName) VALUES('"+empFirstName+"')";

Kindly guide me where am I making mistake. 请指导我在哪里犯错。 Thanks. 谢谢。

Your table structure says it has a primary key empId as the first column. 您的表结构说它有一个主键empId作为第一列。

So when you write query as 所以当你写查询为

INSERT INTO Emp VALUES ('first name')

The value will refer to the column empId and the query will fail. 该值将引用列empId ,查询将失败。

You will need to explicitly specify the column names in this situation , something as 在这种情况下,您需要显式指定列名称,例如

INSERT INTO Emp (empName) VALUES ('first name')

Now mysql knows that the value is for the column empName. 现在,mysql知道该值用于empName列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM