科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道Linux应用集成MySQL数据库访问技巧

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

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

本文描述了MySQL,一种利用第三方数据库开发电子贸易和其它复杂、动态网站的有效工具。MySQL 是一种快速、多线程和全功能的 SQL服务器。

来源:中国IT实验室 2008年5月19日

关键字: 技巧 数据库 MySQL

  • 评论
  • 分享微博
  • 分享邮件
以下代码是使用免费获得的Tcl Generic Database Interface以Tcl实现的。这样一种接口的好处在于Tcl是解释型的,可以对代码进行迅速开发和快速修改。
  
  Tcl示例
  
  #This code prints out all products in the database
  # that are below a specified price (assumed to have been determined
  # beforehand, and stored in the variable targetPrice)
  # The output is in HTML table format, appropriate for CGI output
  
  #load the SQL shared object library. the Tcl interpreter could also
  #have been compiled with the library, making this line unnecessary
  load /home/aroetter/tcl-sql/sql.so
  
  #these are well defined beforehand, or they could
  #be passed into the script
  set DBNAME "clientWebSite";
  set TBLNAME "products";
  set DBHOST "backend.company.com"
  set DBUSER "mysqluser"
  set DBPASSWD "abigsecret"
  
  set targetPrice 200;
  
  #connect to the database
  set handle [sql connect $DBHOST $DBUSER $DBPASSWD]
  sql selectdb $handle $DBNAME ;# get test database
  
  #run a query using the specified sql code
  sql query $handle "select * from $TBLNAME where price <= $targetPrice"
  
  
  #print out html table header
  puts "<table border=4>"
  puts "<th>Product Id <th width=200>Description <th>Price (\$)"
  
  #output table rows - each fetchrow retrieves one result
  #from the sql query
  while {[set row [sql fetchrow $handle]] != ""} {
    set prodid [lindex $row 0]
    set descrip [lindex $row 1]
    set price [lindex $row 2]
    puts "<tr><td>$prodid <td align=center>$descrip <td>$price"
  }
  
  puts "</table>"
  
  #empty the query result buffer - should already be empty in this case
  sql endquery $handle
  #close the db connection - in practice this same connection
  #is used for multiple queries
  sql disconnect $handle
  
  下面的代码是使用正式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 << "<table border=4>\n";
    cout << "<th>Product Id <th width=200>Description"
   << "<th>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 << "<tr><td align=center>" << row[0]
        << "<td>" << row[1]
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章