Add Record in MySQL
-
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 whycon->setSchema(....);
is producing exception. 2- How do I get all database names and table names using prepared statements. Thanks for any pointer. -
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 whycon->setSchema(....);
is producing exception. 2- How do I get all database names and table names using prepared statements. Thanks for any pointer.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.
-
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 whycon->setSchema(....);
is producing exception. 2- How do I get all database names and table names using prepared statements. Thanks for any pointer.Put the code into a
try
block and catchsql::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 becausesetSchema
fails to open the specified database. BTW: You should useGetString
instead ofGetBuffer
when a const string has to be passed.