基于pwlib(ptlib)连接MySql

101 篇文章 0 订阅
87 篇文章 1 订阅

    最近在研究ptlib库,因为好多大的开源项目都使用这个用C++开发的跨平台的库,所以比较感兴趣。前一段看了一个连接ACCESS的例子,现在把它改成MySQL的了。说起来简单,但对于我这个新手来说,花了不少时间。对ptlib有研究的朋友可以练习我,毕竟网上关于ptlib的东西不多。代码如下:

 

/*
 * ODBCTest.cxx
#include <ptlib.h>
#include <ptlib/pprocess.h>
#include <ptclib/podbc.h>

class ODBCtest : public PProcess
{
  PCLASSINFO(ODBCtest, PProcess)
  public:
    void Main();
};

PCREATE_PROCESS(ODBCtest)

void ODBCtest::Main()
{
  cout << "ODBC Component for the Pwlib Library Test Program" << endl;
  cout << "=================================================" << endl;
  cout << endl;

  PODBC link;
  PODBC::ConnectData data;       //用来存数据库连接参数的类    ,此类在本例中无用

  if (!link.ConnectDB_mySQL("mytest","root","sa"))   //连接数据库,第一个参数是数据库的名字,后面是用户名和密码
      cout << "MySQL Error Link" << endl;
  else {        
      cout << "Connected MySql DataBase" << endl;
      PINDEX i =0;   /// Table List
      PINDEX j =0;   /// Columns
      PINDEX k =0;  /// Rows
      PINDEX l =0;   /// Fields
     /// Settings
    link.SetPrecision(2);        /// Number of Decimal Places (def = 4) 设置四舍五入的精度  
    link.SetTimeFormat(PTime::ShortDate);/// Set the Default Display Time

/// Enumerate Tables in Database
///+++++++++++++++++++++++++++++
/// You can also use the QUERY keyword to view Queries
     cout << "Tables in Database" << endl;
    PStringArray tables = link.TableList("TABLE");  ///TableList可以获取指定数据库中的所有表名
    cout <<"Tables numberis:"<< tables.GetSize()<< endl;
    for (i= 0; i < tables.GetSize(); i++) {
        cout << tables[i] << endl;   ///打印出数据库所包含的所有表
      }

/// Viewing Database Contents
///++++++++++++++++++++++++++
/// Add Select SQL Statement
           
     for (i= 0; i < tables.GetSize(); i++) {      ///将每个表的字段信息打印出来
         cout << "=================" << endl;
         cout<<"Table Name is: " <<tables[i]<< endl;
         PODBC::Table table(&link,"SELECT * from mytest");  ///在内存中生成一个表,表的内容是SQL语句执行的结果,该函数
                                                                                             ///还有两外一种方法,但我实验时不可行,高手指点一下,就是  
                                                                                            ///把SQL语句变为表名            
         cout << "Query Table: " << tables[i] << endl;
         cout << "Columns: " <<table.Columns() <<  " Rows: " << table.Rows() << endl;           
         cout << "ColumnNames : " << endl;
         PStringArray Names = table.ColumnNames();

         for (j= 0; j < Names.GetSize(); j++)
             cout << Names[j] << " ";
         cout << endl;

    // Create a Field Array. The Fields are Bound to the
    // ODBC Driver so Row Navigation updates the Field Data.
         PArray<PODBC::Field> fields;
         for (j=0; j < table.Columns(); j++)
             fields.Append(&table.Column(j+1));

    // You can also Reference via Record Handler 
   //      PODBC:: Row & row = table.RecordHandler();
  //      PODBC::Field & f1 = row.Column(1);
  //        ...etc...
  // or Access the Field directly via the Recordset 
 //      PODBC::Field f1 = table(row.col) 
     
  // Display Table Contents
        for (k= 0; k < table.Rows(); k++) {
           
            table[k+1];  /// set the Record index to row j
            for (l =0; l < table.Columns(); l++) {
    cout << fields[l].AsString() << " ";
             }
            cout << endl;
          }
      }
      cout << endl;

/// Table Modification Examples 
/// +++++++++++++++++++++++++++
     cout << "Modify Table Calls" << endl;
    PODBC::Table ntable(&link,"SELECT * from mytest");///同上

    cout << endl;

/// Delete a Record (Directly Via RecordSet)
    cout << "Delete the Last Record #" << ntable.Rows() << endl;

     if (ntable.DeleteRow(ntable.Rows()))       ///删除
         cout << "Last Record Deleted.." << endl;
     else
         cout << "Error Deleting Last Record" << endl;

     cout << endl;

/// Update a Field (Using RecordHolder callRef is field 2)     ///更新
     cout << "Add 1 to the callRef field of the First Row" << endl;
        PODBC::Row handle = ntable[1];
          int Num = handle[2].AsString().AsInteger();
        cout << "Old Value " << Num << " ";
          handle[2].SetValue(Num+1);
          handle[2].Post();
        cout << "New Value " << handle[2].AsString() << endl;

        cout << endl;

/// Adding a New Record (Using Column Names)                ///添加
         cout << "Add New Record to Calls Table" << endl;
            ntable.NewRow();
            ntable.Column("ID").SetValue(2);
            ntable.Column("myName").SetValue("hej");
          if (ntable.Post())
                           cout << "New Record Added!" << endl;

       cout << endl;

/// Display the RecordSet Contents thro' the RecordSet. (x,y)
    cout << "Display Table with new Record" << endl;

      for (i=0; i< ntable.Rows(); i++)
      {
          for (j =0; j < ntable.Columns(); j++)
            cout << ntable(i+1,j+1).AsString() << " ";

          cout << endl;
      }        
        cout << "Rows " << ntable.Rows() << endl;
            
  }
  link.Disconnect();

}
// End of netif.cxx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值