Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7892543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:53:50+00:00 2026-06-03T06:53:50+00:00

I am developing a c++ console application in visual C++ 2010 that is required

  • 0

I am developing a c++ console application in visual C++ 2010 that is required to connect to mySQL database. I am using wamp server for mysql and mySQL C++ connector to connect. Code works fine for reading database but when I try to Insert data, it gives unexpected error. Any one had such an experience?

Here is my full code:
(Output of the code is: “SQL Error: Error Message: Unknown Exception”)

    // 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:53:52+00:00Added an answer on June 3, 2026 at 6:53 am

    You have two errors in your SQL, one that MySQL will let you get away with and one that it won’t. You’re also picking up a bad habit.

    Both bugs are right here:

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

    String literals in SQL use single quotes, not double quotes; MySQL will let you get away with this though. But, you also have a missing closing parenthesis and MySQL doesn’t like that one bit. You should say this:

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

    Not specifying the columns in an SQL INSERT is a bad habit so you should be saying this:

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

    where col1 and friends are, of course, the real column names. Columns in tables don’t have any well defined order so you should be specific to avoid interesting bugs and maintenance nightmares.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im developing a simple console Application using java. The code is given below `
I am developing one console application using perl script. In that I am printing
This is a console application that I am developing using VS2010 in C#. II
I'm developing a huge console application for Unix using C# via Mono. If I
When developing a Win32 Application (non-console application) in Visual Studio 2005, is there any
Im developing a simple console application using java. I want to display currently running
I am developing a c# console application that requires some 3rd party assemblies(DLL). On
I'm using .NET 3.5 and WCF for developing a server-client application. Binding=BasicHttp. I'm working
I am developing a C# Console Application which downloads files from an SFTP Server
What ? I am developing a Console Application that needs to keep running 24/7

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.