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

来源:互联网 发布:360去广告软件 编辑:程序博客网 时间:2024/04/29 03:52
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
.Zwq402{display:none;}


本新闻共3页,当前在第1页 1 2 3 

 


MySQLAPI

MySQLAPI可用于各种语言,包括几乎所有编写网站后端所实际使用的语言。使用这些API,我们可以构建由Web服务器控制的MySQL客户机。

API(用于数据库访问)以基于连接的模式工作。客户机必须做的第一件事是打开与MySQL服务器的连接。这包括适当地使用服务器认识的用户名和口令来对连接进行身份认证。建立了连接后,服务器选择要使用的特定数据库。确定了初始化后,客户机应用程序(就我们来说是服务器方CGI脚本)就能自由地与数据库以两种方式中的一种进行交互:可以运行常规SQL命令,包括添加和删除表,以及向它们添加记录;也可以对返回结果的数据库运行查询。查询生成一组与查询匹配的记录,然后,客户机可以逐一访问记录,直到查看完所有记录,或者客户机取消暂挂的记录检索。一旦脚本完成了对数据库的操作后,与服务器的连接就被关闭。

要构建集成数据库访问的网站,需要编写CGI脚本来根据数据库状态生成动态结果。Web服务器启动CGI脚本,然后将适当格式化的HTML输出到它们的标准输出流中。Web服务器捕捉到HTML后将它发送回客户机,如同请求是对静态HTML页面进行的那样。在生成HTML的过程中,脚本可以修改数据库,也可以查询并将结果合并到它们的输出中。

作为简单解释上述过程的一个示例,下面的代码(以C和Tcl编写)查询一个包含某公司供销售的产品清单的数据库。这绝没有使用两种语言MySQLAPI的所有特性,但提供了快速、简易扩展的示例,可以对数据库内容执行任何SQL命令。在该例中,脚本显示了低于特定价格的所有产品。在实践中,用户可能在Web浏览器中输入该价格,然后将它发给服务器。我们省去了从环境变量中进行读取来确定HTML表单值的细节,因为它与不支持数据库的CGI脚本中执行的情况没有什么差别。为清晰起见,我们假设事先设置了特定一些参数(例如要查询的价格)。

以下代码是使用免费获得的TclGenericDatabaseInterface以Tcl实现的。这样一种接口的好处在于Tcl是解释型的,可以对代码进行迅速开发和快速修改。
Tcl示例

#Thiscodeprintsoutallproductsinthedatabase
#thatarebelowaspecifiedprice(assumedtohavebeendetermined
#beforehand,andstoredinthevariabletargetPrice)
#TheoutputisinHTMLtableformat,appropriateforCGIoutput

#loadtheSQLsharedobjectlibrary.theTclinterpretercouldalso
#havebeencompiledwiththelibrary,makingthislineunnecessary
load/home/aroetter/tcl-sql/sql.so

#thesearewelldefinedbeforehand,ortheycould
#bepassedintothescript
setDBNAME"clientWebSite";
setTBLNAME"products";
setDBHOST"backendpany"
setDBUSER"MySQLuser"
setDBPASSWD"abigsecret"

settargetPrice200;

#connecttothedatabase
sethandle[sqlconnect$DBHOST$DBUSER$DBPASSWD]
sqlselectdb$handle$DBNAME;#gettestdatabase

#runaqueryusingthespecifiedsqlcode
sqlquery$handle"select*from$TBLNAMEwhereprice<=$targetPrice"

#printouthtmltableheader
puts"<tableborder=4>"
puts"<th>ProductId<thwidth=200>Description<th>Price(/$)"

#outputtablerows-eachfetchrowretrievesoneresult
#fromthesqlquery
while{[setrow[sqlfetchrow$handle]]!=""}{
   setprodid[lindex$row0]
   setdescrip[lindex$row1]
   setprice[lindex$row2]
   puts"<tr><td>$prodid<tdalign=center>$descrip<td>$price"
}

puts"</table>"

#emptythequeryresultbuffer-shouldalreadybeemptyinthiscase
sqlendquery$handle
#closethedbconnection-inpracticethissameconnection
#isusedformultiplequeries
sqldisconnect$handle

本新闻共3页,当前在第2页 1 2 3 

 


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

C++示例

#include
#include
#include

constchar*DBNAME="clientWebSite";
constchar*DBTABLE="products";
constchar*DBHOST="backendpany";
constchar*DBUSER="MySQLuser";
constchar*DBPASSWD="abigsecret":

intmain(){
 try{
   //openthedatabaseconnectionandquery
   Connectioncon(DBNAME,DBHOST,DBUSER,DBPASSWD);
   Queryquery=con.query();

   //writevalidsqlcodetothequeryobject
   query<<"select*from"<<DBTABLE;

   //runthequeryandstoretheresults
   Resultres=query.store();

   //writeoutthehtmltableheader
   cout<<"<tableborder=4>/n";
   cout<<"<th>ProductId<thwidth=200>Description"
 <<"<th>Price($)"<<endl;

   Result::iteratorcurResult;
 Rowrow;

   //iterateovereachresultandputitintoanhtmltable
   for(curResult=res.begin();curResult!=res.end();curResult++){
     row=*curResult;
     cout<<"<tr><tdalign=center>"<<row[0]
         <<"<td>"<<row[1]
         <<"<td>"<<row[2]<<endl;

   }
   cout<<"</table>"<<endl;

 }catch(BadQueryer){
   //handleabadquery(usuallycausedbyasqlsyntaxerror)
   cerr<<"Error:"<<er.error<< endl;
   return-1;

 }catch(BadConversioner){
 //handleconversionerrorsoutofthedatabaseas

 

 共2页:上一页1

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>