2017-08-13 27 views
0

我可以正確使用soci連接到mysql並執行sql。但postgresql的相同方式不起作用。這是我的makefile和main.cpp中:當我使用soci連接到postgresql時,爲什麼我沒有足夠的數據庫特權?

all: main.cpp 
    g++ -Wall -g -o main main.cpp -lpq -lsoci_core -lsoci_postgresql -ldl 
clean: 

這是main.cpp中:

#include <soci/soci.h> 
#include <soci/postgresql/soci-postgresql.h> 
#include<iostream> 
#include<istream> 
#include<ostream> 
#include<string> 
#include<exception> 

using namespace std; 
using namespace soci; 

int main() { 
    try { 
     session sql("postgresql://dbname=mydb_0"); 
     int count ; 
     sql<< "select count(*) from student", into(count); 
     cout<<"count="<<count<<endl; 
    } catch (exception const &e) { 
     cerr<<"Error:" <<e.what()<<endl; 
    } 
} 

這是錯誤輸出:

Error:Cannot execute query. Fatal error. 錯誤: 對關係 student 權限不夠 
while executing "select count(*) from student". 

我可以在終端執行sql "select count(*) from student"但C++代碼不起作用,爲什麼?

回答

0

好的,我自己解決了這個問題。與其他示例不同,我在init語句中添加了「host = 127.0.0.1」和「port = 5432」。就像這樣:session sql(postgresql, "host=127.0.0.1 dbname=exampledb user=postgres password=123 port=5432");。我希望這個解決方案能夠幫助他人。

相關問題