I am currently having internship and my job required me to use C++ to code a program. What I have learned in my college is totally basic. So basically I need to code a program to calculate user's activeness from a MySQL due to the database have 500k+ users and 13million+ rows. What I'm trying to do is to code a program to list down user by ID with loop and cout the month they're active during 2019-2020. I have done this part. Now I can't connect to MySQL which is in the server, I wanna connect it via IP and port. I've tried to find solutions for these errors for 2 days but I can't find a solution so I came here to ask.
#include <iostream>
#include <chrono>
#include <Windows.h>
#include <stdlib.h>
/*
Include directly the different
headers from cppconn/ and mysql_driver.h + mysql_util.h
(and mysql_connection.h). This will reduce your build time!
*/
#include <mysql_connection.h>
//#include <mysql_driver.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
int main()
{
try {
sql::Driver* driver;
sql::Connection* con;
sql::Statement* stmt;
sql::ResultSet* res;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://123.123.12.3:3306", "id", "pass");
/* Connect to the MySQL test database */
con->setSchema("db");
/*
int user_id = 1;
int activity = 0;
int timestamp;
cout << "Months :";
cout << endl;
cout << "USER" << endl;
cout << "------>" << endl;
*/
//int user = 1;
//for (int i = 0; i <= 529834; i++) //user count:529834
//{
stmt = con->createStatement();
res = stmt->executeQuery("SELECT user_id, add_time FROM m_business_record WHERE(user_id = '1' AND type = '9');");
while (res->next()) {
cout << "\t... MySQL replies: ";
/* Access column data by alias or column name */
cout << res->getString("_message") << endl;
cout << "\t... MySQL says it again: ";
/* Access column data by numeric offset, 1 is the first column */
cout << res->getString(1) << endl;
}
}
catch (sql::SQLException & e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
/**
for (int n = 1; n < 2; n++) {
timestamp = 1548950400;
if (timestamp >= 1546272000 && timestamp <= 1548950399) //Jan 2019 GMT +8
{
cout << "1/19\t";
}
else if (timestamp >= 1548950400 && timestamp <= 1551369599) //Feb 2019 GMT +8
{
cout << "2/19\t";
}
else if (timestamp >= 1551369600 && timestamp <= 1554047999) //Mar 2019 GMT +8
{
cout << "3/19\t";
}
else if (timestamp >= 1554048000 && timestamp <= 1556639999) //Apr 2019 GMT +8
{
cout << "4/19";
}
else if (timestamp >= 1556640000 && timestamp <= 1559318399) //May 2019 GMT +8
{
cout << "5/19\t";
}
else if (timestamp >= 1559318400 && timestamp <= 1561939199) //Jun 2019 GMT +8
{
cout << "6/19\t";
}
else if (timestamp >= 1561939200 && timestamp <= 1564617599) //Jul 2019 GMT +8
{
cout << "7/19\t";
}
else if (timestamp >= 1564617600 && timestamp <= 1567295999) //Aug 2019 GMT +8
{
cout << "8/19\t";
}
else if (timestamp >= 1567296000 && timestamp <= 1569887999) //Sep 2019 GMT +8
{
cout << "9/19\t";
}
else if (timestamp >= 1569888000 && timestamp <= 1572566399) //Oct 2019 GMT +8
{
cout << "10/19\t";
}
else if (timestamp >= 1572566400 && timestamp <= 1575158399) //Nov 2019 GMT +8
{
cout << "11/19\t";
}
else if (timestamp >= 1575158400 && timestamp <= 1577836799) //Dec 2019 GMT +8
{
cout << "12/19\t";
}
else if (timestamp >= 1577836800 && timestamp <= 1580515199) //Jan 2020 GMT +8
{
cout << "1/20\t";
}
else if (timestamp >= 1580515199 && timestamp <= 1583020799) //Feb 2020 GMT +8
{
cout << "2/20\t";
}
}
}
cout << endl;
*/
}
Hope someone can point out my mistake and correct me. Thank you.
UPDATE : I fixed my early on error with some help, now left with 4 errors and some weird onE cuz they're in the .obj file but I'm not sure how to open it
1. LNK2019 unresolved external symbol "__declspec(dllimport) public: __cdecl sql::SQLString::~SQLString(void)" (__imp_??1SQLString@sql@@QEAA@XZ) referenced in function main
2. LNK2019 unresolved external symbol "__declspec(dllimport) public: __cdecl sql::SQLString::SQLString(char const * const)" (__imp_??0SQLString@sql@@QEAA@QEBD@Z) referenced in function main
3. LNK2019 unresolved external symbol __imp_get_driver_instance referenced in function main
4. LNK1120 3 unresolved externals
5. identifier "SELECT" is undefined
6. expected a ')' ---- (Line 36, at the query part 'SELECT.....')
ERROR 1 - 6 HAS BEEN FIXED
---------------------------------------------------------------------------------
NEW ERROR :
using try() catch() got an error of
ERR : MySQL_ResultSet::getString: invalid value of 'columnIndex' (MySQL error code 0, SQL state: )
but I didn't use MySQL_ResultSet::getString