mysql++学习一

表结构如下   :  

mysql>   describe   user_1;
+-------+--------------+------+-----+---------+-------+
|   Field   |   Type                   |   Null   |   Key   |   Default   |   Extra   |
+-------+--------------+------+-----+---------+-------+
|   name     |   varchar(20)     |   YES     |           |   NULL         |               |
|   sex       |   char(1)             |   YES     |           |   NULL         |               |
|   age       |   int(11)             |   YES     |           |   NULL         |               |
|   memo     |   varchar(100)   |   YES     |           |   NULL         |               |

表的内容     :  
  mysql>   select   *   from   user_1;
+---------+------+------+-------------------------------------+
|   name         |   sex     |   age     |   memo                                                                 |
+---------+------+------+-------------------------------------+
|   yaya         |   F         |       20   |   hello   world!                                                 |
|   haojing   |   F         |       20   |   hello   world!   i '   m   yaya                             |
|   tufei       |   T         |       26   |   hello   world!   i '   m   tufei!                         |
|   zhutou     |   T         |       26   |   hello   world!   i '   m   zhutou!                       |
|   xiaobai   |   T         |       23   |   hello   world!   i '   m   xiaobai!                     |
|   laohu       |   T         |       31   |   hello   world!!!!!!!!!!!!!   i   'm   laohu   |
+---------+------+------+-------------------------------------+
6   rows   in   set   (0.01   sec)

  bool   tmpbool   =   conn.connect( "yaya ");
                if   (!tmpbool)
                {
                                cerr < < "connnect   database   faile   ! " < <endl;
                                return   1;
                }
                mysqlpp::Query     query   =   conn.query();

                query   < <   sqlstr;
                mysqlpp::Result   res=   query.store();

                if   (res)
                {
                                mysqlpp::Row   row;
                                mysqlpp::Row::size_type   i;
                                int   sum   =   0   ;
                                int   tmp   =   0;
                                for   (i   =   0   ;   row   =   res.at(i)   ;   i++   ){
                                                tmp   =   row[ "age "];
                                                sum   +=   tmp   ;
                                }
                                cout < < "sum   :     "   < <   sum   < <   endl;

                }
                else
                {
                                cerr < < "Failed   to   get   item   list   : " < <query.error() < <endl;
                                return   1;
                }
这个方法可以求得表里所有age的和!
请问   :  
1     还有别的方法不??
      mysqlpp::ColData   这个的用法可以求age的和不?
2     :   mysqlpp::Row   row;
                                mysqlpp::Row::size_type   i;
                                int   sum   =   0   ;
                                int   tmp   =   0;
                                string   userstr( "xiaobai ");
                                string   outputstr   ;
                                for   (i   =   0   ;   row   =   res.at(i)   ;   i++   ){
                                                tmp   =   row[ "age "];
                                                sum   +=   tmp   ;
                                                if   (userstr   ==   row[ "name "])
                                                {
                                                                //这个地方我是想把memo字段的值给outputstr呢!
                                                                outputstr   =   row[ "memo "];
                                                }
                                }
                                cout < < "sum   :     "   < <   sum   < <   endl;

这个地方编译不能通过呀!!!
[root@localhost   1]#   make
/usr/bin/g++   -c   -g   -I/usr/include/mysql   -I/usr/local/include/mysql++           select_user.cpp   -o   select_user.o
select_user.cpp:   In   function   ‘int   main()’:
select_user.cpp:55:   错误:no   match   为   ‘operator==’   在   ‘userstr   ==   mysqlpp::Row::operator[](const   char*)   const(((const   char*) "name "))’   中
/usr/local/include/mysql++/type_info.h:355:   附注:备选为:   bool   mysqlpp::operator==(const   mysqlpp::mysql_type_info&,   const   std::type_info&)
/usr/local/include/mysql++/type_info.h:341:   附注:                   bool   mysqlpp::operator==(const   std::type_info&,   const   mysqlpp::mysql_type_info&)
/usr/local/include/mysql++/type_info.h:328:   附注:                   bool   mysqlpp::operator==(const   mysqlpp::mysql_type_info&,   const   mysqlpp::mysql_type_info&)
/usr/local/include/mysql++/const_string.h:188:   附注:                   bool   mysqlpp::operator==(mysqlpp::const_string&,   mysqlpp::const_string&)
select_user.cpp:58:   错误:ambiguous   overload   为   ‘operator=’   在   ‘outputstr   =   mysqlpp::Row::operator[](const   char*)   const(((const   char*) "memo "))’   中
/usr/lib/gcc/i386-redhat-linux/4.1.0/../../../../include/c++/4.1.0/bits/basic_string.h:493:   附注:备选为:   std::basic_string <_CharT,   _Traits,   _Alloc> &   std::basic_string <_CharT,   _Traits,   _Alloc> ::operator=(const   _CharT*)   [with   _CharT   =   char,   _Traits   =   std::char_traits <char> ,   _Alloc   =   std::allocator <char> ]
/usr/lib/gcc/i386-redhat-linux/4.1.0/../../../../include/c++/4.1.0/bits/basic_string.h:504:   附注:                   std::basic_string <_CharT,   _Traits,   _Alloc> &   std::basic_string <_CharT,   _Traits,   _Alloc> ::operator=(_CharT)   [with   _CharT   =   char,   _Traits   =   std::char_traits <char> ,   _Alloc   =   std::allocator <char> ]
make:   ***   [select_user.o]   错误   1
[root@localhost   1]#

就这两个问题哦!!

作者: jj_yaya_19880601   发布时间: 2007-07-12


http://www.vckbase.com/code/listcode.asp?mclsid=11&sclsid=1121

作者: hzhxxx   发布时间: 2007-07-12

啊   阿!   你这个不是官方的mysql++的用法阿

作者: jj_yaya_19880601   发布时间: 2007-07-12

mysql++
呵呵。。。
初次接触。。。

作者: jufeng2309   发布时间: 2007-07-12

没用过,只用过mysql   C版的

作者: akirya   发布时间: 2007-07-12

Specialized   SQL   Structures

作者: lovecocohuan   发布时间: 2007-07-12

关注

作者: rockefeller8   发布时间: 2007-07-12

你这个帖子有点老了, 我刚刚也遇到你这个问题中的第二个, 具体就是这个:

错误:ambiguous overload 为 ‘operator=’ 在 ‘okey = row.mysqlpp::Row::operator[](((const char*)"olkey"))’ 中
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:493: 附注:备选为: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:504: 附注: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]

错误分析,应该是mysqlpp程序对linux测试不充分? 我也很菜,搞不清楚, 这里是一个 = 符号的不不确定操作引起的, 出现的情况是当数据库中的字段是string的时候, 一用 = 就会出错.

修复方法: 
比如原来的代码是这样:
str = row["str"]; //服务器ip 
修改为以下:
  mysqlpp::String tmp = row["str"]; //因为是同类型,不会出错
  tmp.to_string(str); //然后使用它mysqlpp的string类的方法来得到字符串

作者: wawehi   发布时间: 2011-05-04