使用IBPP在C++中操作FireBird/Interbase数据库
- 在程序目录下查找fbembed.dll
- 在程序目录下查找fbclient.dll
- 依据DefaultInstance注册表键值查找fbclient.dll
- 在系统定义位置查找fbclient.dll
- 在系统定义位置查找gds32.dll
例一,基本应用
- #include
<iostream> - #include
<ibpp.h> - using
namespace std; -
- //数据库名,根据你的情况更改。
- const
char* g_szDBName "d:\\test.fdb";= - //服务器名,针对服务器版本。对于嵌入式,它应该是""
- const
char* g_szServerName "";= -
- //这里的用户名和密码是FireBird默认值,对于服务器版,用你自己的密码
- //对于嵌入式,就是这个(FireBird嵌入式版没有加密功能)。
- const
char* g_szUserName = - const
char* g_szPassword "masterkey";= -
- int
main() - {
-
IBPP::Database db = IBPP::DatabaseFactory(g_szServerName, -
g_szDBName, -
g_szUserName, -
g_szPassword); -
//建立数据库 -
db->Create(3); -
//连接数据库 -
db->Connect(); -
-
IBPP::Transaction tr = IBPP::TransactionFactory(db); -
tr->Start(); -
try{ -
IBPP::Statement st = IBPP::StatementFactory(db,tr); -
//建立数据表 -
st->Execute( -
"CREATE TABLE TESTTABLE(" -
" ID INTEGER NOT NULL PRIMARY KEY," -
" RNO VARCHAR(10) NOT NULL UNIQUE," -
" SHIFT VARCHAR(5) NOT NULL" -
" CHECK(SHIFT IN('A','B','C','D'))," -
" LINE CHAR(20) NOT NULL," -
" SL COMPUTED BY(SHIFT||'.'||LINE)," -
" EMP CHAR(20)" -
" )" -
); -
tr->CommitRetain(); -
//插入数据,华安和小强 -
st->Execute( -
"INSERT INTO TESTTABLE(ID,RNO,SHIFT,LINE,EMP)" -
"VALUES(1,'B9527','B','DAZHA','Hua,An')" -
); -
st->Execute( -
"INSERT INTO TESTTABLE(ID,RNO,SHIFT,LINE,EMP)" -
"VALUES(2,'B9528','B','ZHUANGSI','Xiao,Qiang')" -
); -
-
tr->CommitRetain(); -
//插入数据,石榴和祝枝山 -
st->Execute( -
"INSERT INTO TESTTABLE(ID,RNO,SHIFT,LINE,EMP)" -
"VALUES(3,'B9525','A','DAZHA','Shi,Liu')" -
); -
//SHIFT只能是ABCD,看看写X会有什么情况发生 -
st->Execute( -
"INSERT INTO TESTTABLE(ID,RNO,SHIFT,LINE,EMP)" -
"VALUES(4,'B9526','X','DAZHA','Zhu,ZiShang')" -
); -
tr->Commit(); -
} -
catch(IBPP::SQLException &e){ -
cerr << e.what() << endl; -
tr->Rollback(); -
} -
-
tr->Start(); -
try{ -
IBPP::Statement st = IBPP::StatementFactory(db,tr); -
st->Execute("SELECT RNO, );EMP FROM TESTTABLE" -
//显示SELECT得到的数据 -
while(st->Fetch()) -
{ -
string rno, emp; -
st->Get("RNO",rno); -
st->Get("EMP",emp); -
cout << "RNO:" << "rno << EMP:" << emp << endl; -
} -
tr->Commit(); -
} -
catch(IBPP::SQLException &e){ -
cerr << e.what() << endl; -
tr->Rollback(); -
} - }
相关文章