我在将int 类型的数据压入一个string的时候该如何写啊? 比如说:
int a = 15;
int b = 16;
string name = "test"
string query = "insert into test values ('"+name+"','"+a+"','"+b+"')";但是我在编译的时候就提示错误,我的分析是我的int型的 a 和 b 在压入string类型的时候出错.  请帮忙看看应该如何写这个string 语句.是不是连接的时候出问题? 

解决方案 »

  1.   

    string query = "insert into test values ('"+name+"',"+a+","+b+")";假设你的TEST表就三个字段 第一个为CHAR,其它两个为数字。
      

  2.   

    (1)如果我把后面的两个int类型的链接干掉,就没有错误.  加上去就错误...不知道是不是我的写法出了问题..
    (2)string query = "insert into test values ('"+name+"',"+a+","+b+")"; 这种写法也报错.
      

  3.   

    数据库中a,b值对应的列是什么类型?如果是int不需要',如果是varchar或者char要加'
      

  4.   

    在数据库中 a 和b 对应的是int类型的,name 是varchar类型的, 我是使用的C++ 语言来编写的代码.
    使用string query = "insert into test values ('"+name+"',"+a+","+b+")"; 这种写法也报错:
    1>------ Build started: Project: TDBServer, Configuration: Debug Win32 ------
    1>Compiling...
    1>DClient.cpp
    1>e:\ricky\work files\filenew\program\gameserver\src\gamecore\object.h(62) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
    1>e:\ricky\work files\filenew\program\gameserver\src\gamecore\object.h(105) : warning C4244: 'return' : conversion from 'uint16' to 'uint8', possible loss of data
    1>e:\ricky\work files\filenew\program\gameserver\src\gamecore\tailmanager.h(27) : warning C4267: 'return' : conversion from 'size_t' to 'uint32', possible loss of data
    1>e:\ricky\work files\filenew\program\gameserver\src\gamecore\buff.h(18) : warning C4355: 'this' : used in base member initializer list
    1>e:\ricky\work files\filenew\program\gameserver\src\gamecore\worldsession.h(22) : warning C4172: returning address of local variable or temporary
    1>e:\ricky\work files\filenew\program\gameserver\src\tdbserver\dclient.cpp(111) : warning C4267: 'argument' : conversion from 'size_t' to '_UInt32', possible loss of data
    1>e:\ricky\work files\filenew\program\gameserver\src\tdbserver\dclient.cpp(132) : warning C4267: 'argument' : conversion from 'size_t' to '_UInt32', possible loss of data
    1>e:\ricky\work files\filenew\program\gameserver\src\tdbserver\dclient.cpp(133) : warning C4267: '+=' : conversion from 'size_t' to 'uint32', possible loss of data
    1>e:\ricky\work files\filenew\program\gameserver\src\tdbserver\dclient.cpp(171) : warning C4101: 'idx' : unreferenced local variable
    1>e:\ricky\work files\filenew\program\gameserver\src\tdbserver\dclient.cpp(200) : error C2782: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : template parameter '_Elem' is ambiguous
    1>        e:\program files\study tools\visual studio 2005\vc\include\string(61) : see declaration of 'std::operator +'
    1>        could be 'uint16'
    1>        or 'char'
    1>e:\ricky\work files\filenew\program\gameserver\src\tdbserver\dclient.cpp(200) : error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const _Elem *' from 'uint16'
    1>        e:\program files\study tools\visual studio 2005\vc\include\string(51) : see declaration of 'std::operator +'
    1>e:\ricky\work files\filenew\program\gameserver\src\tdbserver\dclient.cpp(200) : error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const _Elem,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'uint16'
    1>        e:\program files\study tools\visual studio 2005\vc\include\string(41) : see declaration of 'std::operator +'
    1>e:\ricky\work files\filenew\program\gameserver\src\tdbserver\dclient.cpp(200) : error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'std::basic_string<_Elem,_Traits,_Ax>'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>,
    1>            _Ax=std::allocator<char>
    1>        ]
    1>        e:\program files\study tools\visual studio 2005\vc\include\string(31) : see declaration of 'std::operator +'
    1>e:\ricky\work files\filenew\program\gameserver\src\tdbserver\dclient.cpp(200) : error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'uint16'
    1>        e:\program files\study tools\visual studio 2005\vc\include\string(21) : see declaration of 'std::operator +'
    1>e:\ricky\work files\filenew\program\gameserver\src\tdbserver\dclient.cpp(200) : error C2784: 'std::_Vb_iterator<_MycontTy> std::operator +(_Vb_iterator<_MycontTy>::difference_type,std::_Vb_iterator<_MycontTy>)' : could not deduce template argument for 'std::_Vb_iterator<_MycontTy>' from 'uint16'
    1>        e:\program files\study tools\visual studio 2005\vc\include\vector(1800) : see declaration of 'std::operator +'
    1>e:\ricky\work files\filenew\program\gameserver\src\tdbserver\dclient.cpp(200) : error C2784: 'std::_Vb_const_iterator<_MycontTy> std::operator +(_Vb_const_iterator<_MycontTy>::difference_type,std::_Vb_const_iterator<_MycontTy>)' : could not deduce template argument for 'std::_Vb_const_iterator<_MycontTy>' from 'uint16'
    1>        e:\program files\study tools\visual studio 2005\vc\include\vector(1695) : see declaration of 'std::operator +'
    1>e:\ricky\work files\filenew\program\gameserver\src\tdbserver\dclient.cpp(200) : error C2784: 'std::_Vector_iterator<_Ty,_Alloc> std::operator +(_Vector_iterator<_Ty,_Alloc>::difference_type,std::_Vector_iterator<_Ty,_Alloc>)' : could not deduce template argument for 'std::_Vector_iterator<_Ty,_Alloc>' from 'uint16'
    1>        e:\program files\study tools\visual studio 2005\vc\include\vector(396) : see declaration of 'std::operator +'
    1>e:\ricky\work files\filenew\program\gameserver\src\tdbserver\dclient.cpp(200) : error C2784: 'std::_Vector_const_iterator<_Ty,_Alloc> std::operator +(_Vector_const_iterator<_Ty,_Alloc>::difference_type,std::_Vector_const_iterator<_Ty,_Alloc>)' : could not deduce template argument for 'std::_Vector_const_iterator<_Ty,_Alloc>' from 'uint16'
    1>        e:\program files\study tools\visual studio 2005\vc\include\vector(264) : see declaration of 'std::operator +'
    1>e:\ricky\work files\filenew\program\gameserver\src\tdbserver\dclient.cpp(200) : error C2784: 'std::_String_iterator<_Elem,_Traits,_Alloc> std::operator +(_String_iterator<_Elem,_Traits,_Alloc>::difference_type,std::_String_iterator<_Elem,_Traits,_Alloc>)' : could not deduce template argument for 'std::_String_iterator<_Elem,_Traits,_Alloc>' from 'uint16'
    1>        e:\program files\study tools\visual studio 2005\vc\include\xstring(439) : see declaration of 'std::operator +'
    1>e:\ricky\work files\filenew\program\gameserver\src\tdbserver\dclient.cpp(200) : error C2784: 'std::_String_const_iterator<_Elem,_Traits,_Alloc> std::operator +(_String_const_iterator<_Elem,_Traits,_Alloc>::difference_type,std::_String_const_iterator<_Elem,_Traits,_Alloc>)' : could not deduce template argument for 'std::_String_const_iterator<_Elem,_Traits,_Alloc>' from 'uint16'
    1>        e:\program files\study tools\visual studio 2005\vc\include\xstring(299) : see declaration of 'std::operator +'
    1>e:\ricky\work files\filenew\program\gameserver\src\tdbserver\dclient.cpp(200) : error C2784: 'std::reverse_iterator<_RanIt> std::operator +(_Diff,const std::reverse_iterator<_RanIt> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'uint16'
    1>        e:\program files\study tools\visual studio 2005\vc\include\xutility(1847) : see declaration of 'std::operator +'
    1>e:\ricky\work files\filenew\program\gameserver\src\tdbserver\dclient.cpp(200) : error C2676: binary '+' : 'std::basic_string<_Elem,_Traits,_Ax>' does not define this operator or a conversion to a type acceptable to the predefined operator
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>,
    1>            _Ax=std::allocator<char>
    1>        ]
      

  5.   

    desc test;看一下你的表结构。
      

  6.   

    其实我要执行的语句是:
    std::string name;
    uint16 job;
    uint16 sex;
    uint16 photo;
            cin >> name;
            cin >> job;
            cin >> sex;
            cin >> photo;(1) std::string sqlInsert ="insert into players(Name, Job, Sex,Photo) values('"+name+"',"+job+","+sex+","photo")";     //c++编译错误
    (1) std::string sqlInsert ="insert into players(Name, Job, Sex,Photo) values('"+name+"')";                             //c++编译正确,虽然sql执行会出错但是在
      

  7.   

    "photo+")";
      

  8.   

    std::string sqlInsert ="insert into players(Name, Job, Sex,Photo) values('"+name+"',"+job+","+sex+","+photo+")";  
    C++ 的编译错误似乎不用来问了吧 
      

  9.   

    (1) std::string sqlInsert ="insert into players(Name, Job, Sex,Photo) values('"+name+"',"+job+","+sex+","+photo+")";    //c++编译错误错误提示是什么?另外提供你的 desc players表结构,已经向你要了多次了。可你似乎不愿意提供。
      

  10.   

    create table Players
    (
    Id   int(4) auto_increment primary key,
    Name varchar(20) not null,
    Job  smallint(2) not null default 1,
    Sex  smallint(2) not null default 1,
    Photo  smallint(2) not null default 1,
            Email varchar(20)
    )engine = MYISAM;
    我的表中只有这么几列数据. 错误已经在上面提供了.  首先出现的是C++ 编译错误啊...
      

  11.   

    本帖最后由 ACMAIN_CHM 于 2009-11-19 13:18:44 编辑
      

  12.   

    string query = "insert into test values (''"+name+"'',''"+a+"'',''"+b+"'')"; 
      

  13.   

    也不行啊, 错误为:
    1>e:\ricky\work files\c++\c++primertest\testmysql\src\main.cpp(49) : error C2110: '+' : cannot add two pointers
      

  14.   

    to rickypc  建议到C++的版中去问一下如何做两个string 的相连。string s = '1234adsf" + 123 ;这种语法可能不被C++支持。至少在C中是不支持的。C++中没有映象了。
    现在你的C++语法错误,不是数据库本身的问题。
      

  15.   


    string query = "sting"+ 13;可以相连,不过 输出的结果并不是我想要的"sting13",  看来只能将int转换成string类型之后才能相连了...顺便请教:
    (1)在mysql中使用select 得到的一个结果集中的int类型为什么都是string类型的呢? 不管数据库中存放的是什么数据都是string类型的吗?
    (2)使用C++ 进行数据库操作的时候,为什么执行完一个存储过程之后,以后不管什么操作都不能执行呢?  而且错误编号是 2014.  如何设置? 
      

  16.   


    是的, 返回的列都是 char* 
    应该没有这样问题,这个你需要具体分析你的代码了。