Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Add Record in MySQL

Add Record in MySQL

Scheduled Pinned Locked Moved C / C++ / MFC
databasec++mysqljsonperformance
3 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Django_Untaken
    wrote on last edited by
    #1

    Hello there. I am trying to insert record in mysql database using c++ ("sakila.actor"). Problem is, if I hardcode my table name (using double quotes) then it works fine, otherwise it gives runtime exception. Here is the code

    void MyAppDlg::InsertRecord(CString sHostWithPort, CString sUser, CString sPassword, CString sTableName, int nActorId, CString sFirstName, CString sLastName, CString sLastUpdate)
    {
    sql::Driver *driver;
    sql::Connection *con;
    sql::PreparedStatement *pstmt;

    driver = get\_driver\_instance();
    con = driver->connect(sHostWithPort.GetString(), sUser.GetString(), sPassword.GetString());
    
    con->setSchema(sTableName.GetBuffer()); // UNHANDLED EXCEPTION .... AT MEMORY LOCATION
    
        ///// rest of the code down below. e.g. preparing and executing statment
    

    }

    Two questions: 1- driver->connect(...); function is compiling with no problem but why con->setSchema(....); is producing exception. 2- How do I get all database names and table names using prepared statements. Thanks for any pointer.

    Richard Andrew x64R J 2 Replies Last reply
    0
    • D Django_Untaken

      Hello there. I am trying to insert record in mysql database using c++ ("sakila.actor"). Problem is, if I hardcode my table name (using double quotes) then it works fine, otherwise it gives runtime exception. Here is the code

      void MyAppDlg::InsertRecord(CString sHostWithPort, CString sUser, CString sPassword, CString sTableName, int nActorId, CString sFirstName, CString sLastName, CString sLastUpdate)
      {
      sql::Driver *driver;
      sql::Connection *con;
      sql::PreparedStatement *pstmt;

      driver = get\_driver\_instance();
      con = driver->connect(sHostWithPort.GetString(), sUser.GetString(), sPassword.GetString());
      
      con->setSchema(sTableName.GetBuffer()); // UNHANDLED EXCEPTION .... AT MEMORY LOCATION
      
          ///// rest of the code down below. e.g. preparing and executing statment
      

      }

      Two questions: 1- driver->connect(...); function is compiling with no problem but why con->setSchema(....); is producing exception. 2- How do I get all database names and table names using prepared statements. Thanks for any pointer.

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      OK, but you haven't said what the error is. Try:

      con->setSchema(sTableName);

      The difficult we do right away... ...the impossible takes slightly longer.

      1 Reply Last reply
      0
      • D Django_Untaken

        Hello there. I am trying to insert record in mysql database using c++ ("sakila.actor"). Problem is, if I hardcode my table name (using double quotes) then it works fine, otherwise it gives runtime exception. Here is the code

        void MyAppDlg::InsertRecord(CString sHostWithPort, CString sUser, CString sPassword, CString sTableName, int nActorId, CString sFirstName, CString sLastName, CString sLastUpdate)
        {
        sql::Driver *driver;
        sql::Connection *con;
        sql::PreparedStatement *pstmt;

        driver = get\_driver\_instance();
        con = driver->connect(sHostWithPort.GetString(), sUser.GetString(), sPassword.GetString());
        
        con->setSchema(sTableName.GetBuffer()); // UNHANDLED EXCEPTION .... AT MEMORY LOCATION
        
            ///// rest of the code down below. e.g. preparing and executing statment
        

        }

        Two questions: 1- driver->connect(...); function is compiling with no problem but why con->setSchema(....); is producing exception. 2- How do I get all database names and table names using prepared statements. Thanks for any pointer.

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #3

        Put the code into a try block and catch sql::SQLException to get a detailed error description. See the MySQL example http://dev.mysql.com/doc/connector-cpp/en/connector-cpp-examples-complete-example-1.html[^] I guess that the exception is thrown because setSchema fails to open the specified database. BTW: You should use GetString instead of GetBuffer when a const string has to be passed.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups