Closed asmwarrior closed 1 year ago
OK, I try to add a C++ try and catch block to see what is the error message:
#include <soci/soci.h>
#include <soci/mysql/soci-mysql.h>
#include <iostream>
int main()
{
try
{
soci::session sql(soci::mysql, "dbname=testdb user=root password=123456");
int count;
sql << "SELECT COUNT(*) FROM testtable", soci::into(count);
std::cout << "There are " << count << " rows in testtable" << std::endl;
soci::rowset<soci::row> rows = (sql.prepare << "SELECT * FROM testtable");
for(soci::row& row : rows)
{
std::cout << "id = " << row.get<int>("id")
<< ", name = " << row.get<std::string>("name")
<< ", age = " << row.get<int>("age")
<< std::endl;
}
}
catch(const soci::soci_error& e)
{
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}
And the result is:
There are 2 rows in testtable
Error: Unknown data type while executing "SELECT * FROM testtable".
I looks like this may caused by the reason I have a “json" column type. I will try test again.
OK, it looks like the soci library does not know the "json" column in my database, I just change that column type to "binary", and now it works!
Another question: How to use the "json" colum? Maybe it just like a binary type?
JSON fields in MySQL are handled as just strings currently.
I don't think there is any bug here, an exception is not a crash and you do need to handle them when using SOCI, so I'm closing this.
OK, thanks.
Hi, I just installed this library by the command
pacman -S mingw-w64-x86_64-soci
under msys2.I created a very simple sample code and a test database(I'm running Mysql 8.0.33 under Windows).
Here is the sample code:
The code builds OK under my msys2(Code::Blocks + MinGW64 compiler).
When I run the app, I see that it does correctly print the count of the rows as:
But after that, it just crashes in the line
soci::rowset<soci::row> rows = (sql.prepare << "SELECT * FROM testtable");
Any ideas?
My table are something like below: (I just export the content of the database from HeidiSQL)
Also, are there any document or sample code for a beginner to learn this library? Thanks. I don't see much document/samples, for example, I see here: Simple SQL statements