Mysql ConnectC/C++使用

來源:互聯網
上載者:User

標籤:

  前一陣子搞那個QT發布使用mysql程式的驅動問題搞得頭大(實際應該是自己太弱了),弄了幾天也沒搞定,就放棄是用那個了,淚崩,捨不得。這兩天看看connectC/C++的使用方法。
   還是老樣子,LINUX+QT。
  建立一個console程式,測試demo依然臉皮厚的寫在了main檔案內。PRO檔案依然需要加入LIBS += -L /usr/lib/mysql -lmysqlclient -lmysqlpp

 下面是ConnectC++ API介面,ConnectC API介面網上的例子好像比較多,標頭檔是使用mysql.h。自己也用內建的demo測試了下。不得不說,這個沒有QT的好用,但是發布程式不用搞driver了,也是蠻爽的。簡單使用,方便以後學習。
主要內容如下:
 #define MYSQLPP_MYSQL_HEADERS_BURIED//這個必須指定否則可能出現mysql_version.h找不到的情況。

#include <QCoreApplication>
#include<iostream>
#include<mysql++/mysql++.h>

using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
   
    const char* server="localhost";
    const char* db="mysql";
    const char* user="root";
    const char* passwd="Aa,,..1234";

    mysqlpp::Connection conn(false);

    if(conn.connect(db,server,user,passwd))
    {
        mysqlpp::Query query=conn.query("select host,user,password from user");
        if(mysqlpp::StoreQueryResult res=query.store())
        {
            cout<<"host"<<‘\t‘<<"user"<<‘\t‘<<"password"<<endl;
            for(size_t i=0;i<res.num_rows();++i)
            {
                for(size_t j=0;j<res.num_fields();++j)
                {
                    cout<<res[i][j]<<‘\t‘;
                    if(j==2)
                        cout<<endl;
                }
            }
        }
        else
        {
            cerr<<"Failed to get Item list:"<<query.error()<<endl;
            return 1;
        }
        return 0;
    }
    else
    {
        cerr<<"DB connection failed:"<<conn.error()<<endl;
        return 1;
    }

    return a.exec();
}

 

Mysql ConnectC/C++使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.