Soci 的另一个问题......我想连接我刚刚创建的 testDB,但下面的代码显示了致命错误。
我这样做了:
在 PostgreSQL 上:
1) CREATE USER robert WITH ENCRYPTED PASSWORD 'pass';
2) CREATE DATABASE testDB;
3) GRANT ALL PRIVILEGES ON DATABASE testDB TO robert;
我的 C++ 代码:
#include <iostream>
#include <soci.h>
#include <string>
using std::string;
using std::cout;
#include <postgresql/soci-postgresql.h>
bool connectToDatabase(string databaseName, string user, string password)
{
try
{
soci::session sql(soci::postgresql, "dbname=" +databaseName + " user="+user + " password="+password);
}
catch (soci::postgresql_soci_error const & e)
{
std::cerr << "PostgreSQL error: " << e.sqlstate() << " " << e.what() << std::endl;
return false;
}
catch (std::exception const & e)
{
std::cerr << "Some other error: " << e.what() << std::endl;
return false;
}
return true;
}
int main(int argc, char **argv)
{
cout << connectToDatabase("testDB", "robert", "pass");
return 0;
}
编译后我得到了这个:
Some other error: Cannot establish connection to the database.
FATAL: database "testDB" does not exist
怎么了?