2010年4月15日星期四

使用mysql++对mysql数据库表进行增删改查操作

和mysql connector/c++相比,只要在开始时设置好字符集,之后不需要进行转码操作。
conn.set_option(new mysqlpp::SetCharsetNameOption("gbk"));

//查询方法1
vector<mysqlpp::Row> v;
        query.storein(v);

        cout.setf(ios::left);
        cout<<setw(10)<<"username"<<setw(10)<<"password"<<endl;

        for (vector<mysqlpp::Row>::iterator it = v.begin(); it != v.end(); ++it) {
            cout<<setw(10)<<it->at(0)<<setw(10)<<it->at(1)<< endl;
        }

//查询方法2
mysqlpp::StoreQueryResult res = query.store();
        if(res){
            cout.setf(ios::left);
            cout<<setw(10)<<"username"<<setw(10)<<"password"<<endl;
            for (size_t i = 0; i < res.num_rows(); ++i)
                cout << setw(10) << res[i]["username"] << setw(10) << res[i]["password"] << endl;
        }else {
            cerr << "Failed to get stock table: " << query.error() << endl;
            return 1;
        }

//修改记录
query<<"update users set password='hello' where username='沈健'";
query.execute();

//插入
query<<"insert into users(username,password) values('飞翔','123456')";
query.execute();

//删除记录
query<<"delete from users where username='遨游'";
query.execute();

2010年4月13日星期二

使用mysql connector/c++对mysql进行增删改查操作

 

//查询
result = stmt->executeQuery("select * from users"); 
while(result->next()) 
{ 
    string username = result->getString("username"); 
    string password = result->getString("password"); 
    cout << utf2ansi(username) << " : " << password << endl;
} 

//插入
string sql="insert into users(username,password) values('" + ansi2utf8(string("飞翔"))+ "','asdfgh')";
stmt->executeUpdate(sql);

//删除
sql="delete from users where id=4";
stmt->executeUpdate(sql);

//修改
sql="update users set password='hello' where username='" + ansi2utf8(string("沈健")) +"'";;
stmt->executeUpdate(sql);

查出来的数据需要转换成ANSI编码,传给mysql服务器的数据需要转换为unicode编码(mysql数据库中所建的数据库、表和字段采用utf-8编码)

下面是相应的转换函数

//将标准string(unicode)转换为string(ansi)
string utf2ansi(const string &src){
    int nLen = MultiByteToWideChar( CP_UTF8, 0, src.data(), -1, NULL, NULL );
    LPWSTR lpwsz = new WCHAR[nLen];
    MultiByteToWideChar( CP_UTF8, 0, src.data(), -1, lpwsz, nLen );

    int nLen1 = WideCharToMultiByte( CP_ACP, 0, lpwsz, nLen, NULL, NULL, NULL, NULL );
    LPSTR lpsz = new CHAR[nLen1];
    WideCharToMultiByte( CP_ACP, 0, lpwsz, nLen, lpsz, nLen1, NULL, NULL );
    string str(lpsz);
    delete []lpsz;
    return str;
}

//将标准string(ansi)转换为string(unicode)
string ansi2utf8(const string &src){
    int nLen = MultiByteToWideChar( CP_ACP, 0, src.data(), -1, NULL, NULL );
    LPWSTR lpwsz = new WCHAR[nLen];
    MultiByteToWideChar( CP_ACP, 0, src.data(), -1, lpwsz, nLen );

    int nLen1 = WideCharToMultiByte( CP_UTF8, 0, lpwsz, nLen, NULL, NULL, NULL, NULL );
    LPSTR lpsz = new CHAR[nLen1];
    WideCharToMultiByte( CP_UTF8, 0, lpwsz, nLen, lpsz, nLen1, NULL, NULL );
    string str(lpsz);
    delete []lpsz;
    return str;
}

2010年4月12日星期一

使用mysql connector/C++进行数据库编程的几个注意点

 

1、环境准备

下载mysql connector/c++并解压

2、在项目设置中

(1)项目属性,配置属性,C/C++,常规,附加包含目录:将第一步解压出来的目录中的include和include/cppconn添加进去

(2)项目属性,配置属性,链接器,输入,附加依赖项:mysqlcppconn.lib mysqlcppconn-static.lib libmysql.lib

(3)项目属性,配置属性,链接器,常规,附加库目录:将第一步解压出来的目录中的lib目录添加进去

(4)编译项目

(5)运行时,需要将mysqlcppconn.dll libmysql.dll复制到生成可执行文件的目录下

使用mysql++进行数据库编程时的几个注意点

 

1、mysql++的准备

从http://tangentsoft.net/mysql++/releases/mysql++-3.0.9.tar.gz下载源代码,解压缩后,用VS2008打开VS2008文件夹中的解决方案文件(sln),注意此时要用到mysql的include目录和libmysql.lib,如果你在本机上安装了mysql数据库,则可以从安装目录中找到,将include和libmysql.lib添加到项目的额外的包含文件目录和库文件目录中,并在链接选项的输入中键入 libmysql.lib。如果不想安装mysql服务器,也可以下载mysql connector C driver,其中也包含了相应的文件。

设置好之后编译mysql++,如果无误。双击运行其中的install.hta完成安装过程。

2、使用mysql++的项目的相关设置

(1)添加额外的包含文件目录:

  • mysql++的include目录
  • mysql server的include目录

(2)添加额外的库文件目录,并将 libmysql.lib mysqlpp_d.lib 加入到 链接器|输入|附加依赖项中

(3)编译代码

(4)运行时要将libmysql.dll和mysqlpp_d.dll放到生成的可执行文件的文件夹下

使用mysql++连接mysql数据库进行数据查询

#include <iostream>
#include <iomanip>
#include <mysql++.h>
#include <stdio.h>
#include <Windows.h>

using namespace std;
using namespace mysqlpp;

int main(){

    //在取得连接之前首先设置字符编码
    mysqlpp::Connection conn(false);
    conn.set_option(new mysqlpp::SetCharsetNameOption("GBK"));
    conn.connect("mydb","127.0.0.1","user","password",3306);

    mysqlpp::Query query=conn.query();
    vector<mysqlpp::Row> v;
    query<<"select * from nations";
    query.storein(v);

    cout.setf(ios::left);
    cout<<setw(10)<<"Nation"<<setw(10)<<"Notation"<<endl;
    for (vector<mysqlpp::Row>::iterator it = v.begin(); it != v.end(); ++it) {
        cout<<setw(10)<<it->at(0)<<setw(10)<<it->at(1)<< endl;
    }
    getchar();
    return 0;
}

2010年4月9日星期五

使用mysql connector/C++查询mysql数据库

#include <iomanip>
#include <mysql_connection.h> 
#include <mysql_driver.h>
#include <cppconn/driver.h> 
#include <cppconn/statement.h> 
#include <cppconn/exception.h>
#include <cppconn/resultset.h>

using namespace std;

void RunConnectMySQL()  
{ 
    try{
        //sql::Driver *driver;
        sql::mysql::MySQL_Driver *driver;
        sql::Connection *con;
        sql::Statement *stmt; 
        sql::ResultSet *result; 

        //driver = get_driver_instance();
        driver=sql::mysql::get_mysql_driver_instance();

        con = driver->connect("tcp://127.0.0.1:3306", "user", "password"); 

        stmt = con->createStatement(); 
        stmt->execute("use doclibexp"); 

        result = stmt->executeQuery("select * from nations"); 

        while(result->next()) 
        { 
            string nation = result->getString("Nation"); 
            string notation = result->getString("Notation"); 
            cout << nation << " : " << notation << endl;
        } 
        delete stmt; 
        delete con;

    }catch (sql::SQLException &e) {
        cout << "# ERR: SQLException in " << __FILE__;
        cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
        cout << "# ERR: " << e.what();
        cout << " (MySQL error code: " << e.getErrorCode();
        cout << ", SQLState: " << e.getSQLState() << " )" << endl;
    }

} 

int main(int argc, char **argv) 
{ 
    RunConnectMySQL(); 
    getchar(); 
    return 0; 
} 

C++中的标准string由unicode转换为ansi[windows环境]

#include <windows.h>

string utf2ansi(const string &src){
    int nLen = MultiByteToWideChar( CP_UTF8, 0, src.data(), -1, NULL, NULL );
    //得到UTF8编码的字符串长度
    LPWSTR lpwsz = new WCHAR[nLen];
    MultiByteToWideChar( CP_UTF8, 0, src.data(), -1, lpwsz, nLen ); 

    int nLen1 = WideCharToMultiByte( CP_ACP, 0, lpwsz, nLen, NULL, NULL, NULL, NULL );
    LPSTR lpsz = new CHAR[nLen1];
    WideCharToMultiByte( CP_ACP, 0, lpwsz, nLen, lpsz, nLen1, NULL, NULL );
    string str(lpsz);
    delete []lpsz;
    return str;
}