Linux下C++訪問MySQL – Art_Hero – 51CTO技術部落格

來源:互聯網
上載者:User

Linux下C++訪問MySQL - Art_Hero - 51CTO技術部落格

Linux下C++訪問MySQL

2011-04-01 16:39:16

標籤:C++ Linux 資料庫 MySQL 休閑原創作品,允許轉載,轉載時請務必以超連結形式標明文章 原始出處 、作者資訊和本聲明。否則將追究法律責任。http://curran.blog.51cto.com/2788306/533169

今天給大家示範在linux下如果用C++操作mysql1:安裝MySQL掛載光碟片:mkdir /cdrom
mount /dev/hdc /cdrom
cd /cdrom/Server
rpm -ivh perl-DBI-1.52-2.el5.i386.rpm
rpm -ivh mysql-5.0.45-7.el5.i386.rpm mysql-bench-5.0.45-7.el5.i386.rpm mysql-devel-5.0.45-7.el5.i386.rpm
rpm -ivh perl-DBD-MySQL-3.0007-2.el5.i386.rpm
rpm -ivh mysql-server-5.0.45-7.el5.i386.rpm mysql-test-5.0.45-7.el5.i386.rpm

查看mysql服務狀態:service mysqld status

啟動服務:service mysqld start

串連資料庫:mysql2:安裝gcc工具包rpm -ivh gcc* --force --nodeps 強制安裝3:進入mysqlcreate table t1 (id int,name varchar(30));t1表插入資料:1.cpp
#include <iostream>
#include <mysql/mysql.h>
#include <string>
using namespace std;
main()
{
     MYSQL mysql;
     mysql_init(&mysql);
     mysql_real_connect(&mysql, "localhost", "root", "root", "test", 3306, NULL, 0);
     string sql = "insert into t1 (id, name) values (1, 'java1');";
     mysql_query(&mysql, sql.c_str());
     mysql_close(&mysql);
}
g++ -o 1.out 1.cpp -lmysqlclient -I/usr/include/mysql/ -L/usr/lib/mysql

 更新mysql資料;1.cpp
#include <iostream>
#include <mysql/mysql.h>
#include <string>

using namespace std;

main()
{
     MYSQL mysql;
     mysql_init(&mysql);
     mysql_real_connect(&mysql, "localhost", "root", "root", "test", 3306, NULL, 0);
     string sql = "insert into t1 (id,name) values (2, 'java2'),(3, 'java3');";
     mysql_query(&mysql, sql.c_str());
     sql = "update t1 set name = 'java33' where id = 3;";
     mysql_query(&mysql, sql.c_str());
     mysql_close(&mysql);
}
g++ -o 1.out 1.cpp -lmysqlclient -I/usr/include/mysql/ -L/usr/lib/mysql
./1.out mysql 預存程序:mysql>delimiter //
        >create procedure p01()
        >begin
        >insert into t1 (id,name) values (66, 'java66');
        >end;
        >//

應用預存程序:

mysql>delimiter //
        >create procedure p01()
        >begin
        >insert into t1 (id,name) values (66, 'java66');
        >end;
        >//觸發器:
建立t2表:mysql>delimiter //
     >create trigger tr1 after insert on t1 for each row
     >begin
     >insert into t2 (id,name) values (new.id,new.name);
     >end;
     >//
     >delete from t1 where id = 66;
     >//
     >delimeter ;vi 3.cpp
#include <iostream>
#include <string>
#include <mysql/mysql.h>

using namespace std;

main()
{
    MYSQL mysql;
    mysql_init(&mysql);
    mysql_real_connect(&mysql, "localhost", "root", "root", 3306, NULL, 0);
    string sql = "call p01();";
    mysql_query(&mysql, sql.c_str());
    mysql_close(&mysql);
}

g++ -o 3.out 3.cpp -lmysqlclient -I/usr/include/mysql/ -L/usr/lib/mysql
./3.outmysql>delimeter ;
     >select * from t1;
     >select * from t2;
     >存在資料(66, 'java66')查看錶中資料的總數:vi 4.cpp
#include <iostream>
#include <string>
#include <mysql/mysql.h>

using namespace std;

main()
{
    MYSQL mysql;
    MYSQL_RES *result = NULL;
    mysql_init(&mysql);
    mysql_real_connect(&mysql, "localhost", "root", "root", "test", 3306, NULL, 0);
    string sql = "select id,name from t1;";
    mysql_query(&mysql, sql.c_str());
    result = mysql_store_result(&mysql);
    int rowcount = mysql_num_rows(result);
    cout << rowcount << endl;
    mysql_close(&mysql);
}

g++ -o 4.out 4.cpp -lmysqlclient -I/usr/include/mysql/ -L/usr/lib/mysql
./4.out查處欄位的總數:vi 5.cpp
#include <iostream>
#include <string>
#include <mysql/mysql.h>

using namespace std;

main()
{
    MYSQL mysql;
    MYSQL_RES *result = NULL;
    mysql_init(&mysql);
    mysql_real_connect(&mysql, "localhost", "root", "root", 3306, NULL, 0);
    string str = "select id,name from t1;";
    mysql_query(&mysql, sql.c_str());
    result = mysql_store_result(&mysql);
    int rowcount = mysql_num_rows(result);
    cout << rowcount << endl;
    int fieldcount = mysql_num_fields(result);
    cout << fieldcount << endl;
    mysql_close(&mysql);
}

g++ -o 5.out 5.cpp -lmysqlclient -I/usr/include/mysql/ -L/usr/lib/mysql
./5.out列出具體欄位名:vi 6.cpp
#include <iostream>
#include <string>
#include <mysql/mysql.h>

using namespace std;

main()
{
    MYSQL mysql;
    MYSQL_RES *result = NULL;
    MYSQL_FIELD *field = NULL;
    mysql_init(&mysql);
    mysql_real_connect(&mysql, "localhost", "root", "root", "test", 3306, NULL, 0);
    string str = "select id,name from t1;";
    mysql_query(&mysql, sql.c_str());
    result = mysql_store_result(&mysql);
    int rowcount = mysql_num_rows(result);
    cout << rowcount << endl;
    int fieldcount = mysql_num_fields(result);
    cout << fieldcount << endl;
    for(int i = 0; i < fieldcount; i++)
    {
     field = mysql_fetch_field_direct(result,i);
     cout << field->name << "\t\t";
    }
    cout << endl;
    mysql_close(&mysql);
}

g++ -o 6.out 6.cpp -lmysqlclient -I/usr/include/mysql/ -L/usr/lib/mysql
./6.out顯示表中所有資料:vi 7.cpp
#include <iostream>
#include <string>
#include <mysql/mysql.h>

using namespace std;

main()
{
    MYSQL mysql;
    MYSQL_RES *result = NULL;
    MYSQL_FIELD *field = NULL;
    mysql_init(&mysql);
    mysql_real_connect(&mysql, "localhost", "root", "root", 3306, NULL, 0);
    string str = "select id,name from t1;";
    mysql_query(&mysql, sql.c_str());
    result = mysql_store_result(&mysql);
    int rowcount = mysql_num_rows(result);
    cout << rowcount << endl;
    int fieldcount = mysql_num_fields(result);
    cout << fieldcount << endl;
    for(int i = 0; i < fieldcount; i++)
    {
     field = mysql_fetch_field_direct(result,i);
     cout << field->name << "\t\t";
    }
    cout << endl;
    MYSQL_ROW row = NULL;
    row = mysql_fetch_row(result);
    while(NULL != row)
    {
     for(int i=1; i<fieldcount; i++)
     {
        cout << row[i] << "\t\t";
     }
     cout << endl;
     row = mysql_fetch_row(result);
    }
    mysql_close(&mysql);
}

g++ -o 7.out 7.cpp -lmysqlclient -I/usr/include/mysql/ -L/usr/lib/mysql

./out

本文出自 “Art_Hero” 部落格,請務必保留此出處http://curran.blog.51cto.com/2788306/533169

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.