WEB开发网
开发学院数据库MySQL Linux应用集成MySQL数据库访问的技巧 阅读

Linux应用集成MySQL数据库访问的技巧

 2007-03-12 10:56:04 来源:WEB开发网   
核心提示: 下面的代码是使用正式MySQL C++ API MySQL++以C++编写的等价脚本,该版本的优势在于它是编译型的,Linux应用集成MySQL数据库访问的技巧(5),因此比解释语言更快,经常用在特定站点的数据库代码应该以C或C++编写,然后由脚本或直接由Web服务器访问,以改进整体运行时

下面的代码是使用正式MySQL C++ API MySQL++以C++编写的等价脚本。该版本的优势在于它是编译型的,因此比解释语言更快。经常用在特定站点的数据库代码应该以C或C++编写,然后由脚本或直接由Web服务器访问,以改进整体运行时间。

C++示例

#include
#include
#include
const char *DBNAME = "clientWebSite";
const char *DBTABLE = "products";
const char *DBHOST = "backend.company.com";
const char *DBUSER = "mysqluser";
const char *DBPASSWD = "abigsecret":
int main() {
try {
//open the database connection and query
Connection con(DBNAME, DBHOST, DBUSER, DBPASSWD);
Query query = con.query();
//write valid sql code to the query object
query "select * from " DBTABLE;
//run the query and store the results
Result res = query.store();
//write out the html table header
cout "
";
cout "Product Id Description"
"Price ($)" endl;
Result::iterator curResult;
Row row;
//iterate over each result and put it into an html table
for (curResult = res.begin(); curResult != res.end(); curResult++) {
row = *curResult;
cout "" row[0]
"" row[1]
"" row[2] endl;
}
cout "" endl;
} catch (BadQuery er) {
// handle a bad query (usually caused by a sql syntax error)
cerr "Error: " er.error endl;
return -1;
} catch (BadConversion er) {
//handle conversion errors out of the database as well
cerr "Error: Can't convert "" er.data "" to a ""
er.type_name ""." endl;
return -1;
}
return 0;
}

上一页  1 2 3 4 5 6  下一页

Tags:Linux 应用 集成

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接