mysql中varchar的长度问题

cube_chen 2009-06-02 04:09:02
VARCHAR列中的值为可变长字符串。长度可以指定为0到65,535之间的值。(VARCHAR的最大有效长度由最大行大小和使用的字符集确定。整体最大长度是65,532字节)。

参考手册上是这么说的,但是我定义的varchar(1000) str,当我传入的str的长度超过255的时候,就报错,各位大哥给解决下,谢谢!
...全文
592 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
cube_chen 2009-06-02
  • 打赏
  • 举报
回复
我把str的类型改为text就可以了!
但是这个varchar(n),n的大小问题还是没搞清.
cube_chen 2009-06-02
  • 打赏
  • 举报
回复
呵呵,还真没注意过java中String对象的长度,网上搜了下,说是2^31-1字节,约等于2G.所以应该不是java的限制!
ACMAIN_CHM 2009-06-02
  • 打赏
  • 举报
回复

MySQL应该没有这种限制。应该是你 jdbc上的设置,检查一下。

wwwwb 2009-06-02
  • 打赏
  • 举报
回复
这应该是JAVA的限制吧?在JAVA中,字符串最长可以达到1000?
cube_chen 2009-06-02
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 liangCK 的回复:]
concat('SELECT vq.*,bicc.calling_number,bicc.called_number FROM ',tbl_name1,' ....

前面一大串.都一样的.就是后面的不一样..写了3次..不累呀..
在前面..赋好值..然后在后面判断一下..再连接字符串.
[/Quote]

呵呵,谢谢,我继续改!
cube_chen 2009-06-02
  • 打赏
  • 举报
回复
在java程序中调用的,当传入的str的长度太长的时候(超过255)的时候程序报下面的错误:
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data truncated for column 'str' at row 1
liangCK 2009-06-02
  • 打赏
  • 举报
回复
concat('SELECT vq.*,bicc.calling_number,bicc.called_number FROM ',tbl_name1,' ....

前面一大串.都一样的.就是后面的不一样..写了3次..不累呀..
在前面..赋好值..然后在后面判断一下..再连接字符串.
WWWWA 2009-06-02
  • 打赏
  • 举报
回复
怎么调用的?
cube_chen 2009-06-02
  • 打赏
  • 举报
回复

CREATE DEFINER=`root`@`%` PROCEDURE `phonesp`(
starttime varchar(255),
endtime varchar(255),
str varchar(1000),
startPage int,
maxPerPage int
)
BEGIN
DECLARE cnt int;
declare h int default 0;
declare tbl_name varchar(20);
declare tbl_name1 varchar(20);
declare dt varchar(20);
declare ho varchar(2);
declare flag int default -1;
declare readCount int default 0;
declare cur_row_count int default 0;
declare row_count int default 0;
declare startRowNum int;
declare findStartRow boolean default false;

set startRowNum = (startPage - 1) * maxPerPage + 1;
set cnt=timestampdiff(hour,starttime,endtime);
while h <= cnt do
if readCount < maxPerPage then
set dt = date_format(starttime,'%Y%m%d%H');
set tbl_name=concat('vq',dt,'0000');
set tbl_name1=concat('bicc',dt,'0000');
set @test = concat("select count(`TABLE_NAME`) into @flag from `INFORMATION_SCHEMA`.`TABLES` where `TABLE_SCHEMA`='VQM' and `TABLE_NAME`='",tbl_name1,"';");
prepare s0 from @test;
execute s0;
set flag = @flag;
deallocate prepare s0;
if flag>0 then
set @x = concat('SELECT count(*) into @cur_row_count FROM ',tbl_name1,' bicc,',tbl_name,' vq where bicc.local_sdp_ip=vq.src_ip or bicc.local_sdp_ip=vq.dst_ip and bicc.remote_sdp_ip=vq.dst_ip or bicc.remote_sdp_ip=vq.src_ip and bicc.local_sdp_port=vq.src_port or bicc.local_sdp_port=vq.dst_port and bicc.remote_sdp_port=vq.dst_port or bicc.remote_sdp_ip=vq.src_port',str);
prepare s1 from @x;
execute s1;
deallocate prepare s1;
set cur_row_count = @cur_row_count;
set row_count = row_count + cur_row_count;
if row_count - startRowNum >= 0 then
if !findStartRow then
if row_count - startRowNum >= maxPerPage then
set @x = concat('SELECT vq.*,bicc.calling_number,bicc.called_number FROM ',tbl_name1,' bicc,',tbl_name,' vq where bicc.local_sdp_ip=vq.src_ip or bicc.local_sdp_ip=vq.dst_ip and bicc.remote_sdp_ip=vq.dst_ip or bicc.remote_sdp_ip=vq.src_ip and bicc.local_sdp_port=vq.src_port or bicc.local_sdp_port=vq.dst_port and bicc.remote_sdp_port=vq.dst_port or bicc.remote_sdp_ip=vq.src_port',str,' limit ',cur_row_count - (row_count - startRowNum) - 1,',',maxPerPage);
prepare s1 from @x;
execute s1;
deallocate prepare s1;
set readCount = maxPerPage;
else
set @x = concat('SELECT vq.*,bicc.calling_number,bicc.called_number FROM ',tbl_name1,' bicc,',tbl_name,' vq where bicc.local_sdp_ip=vq.src_ip or bicc.local_sdp_ip=vq.dst_ip and bicc.remote_sdp_ip=vq.dst_ip or bicc.remote_sdp_ip=vq.src_ip and bicc.local_sdp_port=vq.src_port or bicc.local_sdp_port=vq.dst_port and bicc.remote_sdp_port=vq.dst_port or bicc.remote_sdp_ip=vq.src_port',str,' limit ',(cur_row_count - (row_count - startRowNum) - 1),',',row_count - startRowNum);
end if;
set findStartRow = true;
else
set @x = concat('SELECT vq.*,bicc.calling_number,bicc.called_number FROM ',tbl_name1,' bicc,',tbl_name,' vq where bicc.local_sdp_ip=vq.src_ip or bicc.local_sdp_ip=vq.dst_ip and bicc.remote_sdp_ip=vq.dst_ip or bicc.remote_sdp_ip=vq.src_ip and bicc.local_sdp_port=vq.src_port or bicc.local_sdp_port=vq.dst_port and bicc.remote_sdp_port=vq.dst_port or bicc.remote_sdp_ip=vq.src_port',str,' limit ',0,',',maxPerPage - readCount);
prepare s1 from @x;
execute s1;
deallocate prepare s1;
set readCount = readCount + (cur_row_count - (maxPerPage - readCount - cur_row_count));
end if;
end if;
end if;
end if;
set h=h+1;
set starttime=timestampadd(hour,1,starttime);
end while;
end;
WWWWA 2009-06-02
  • 打赏
  • 举报
回复
测试了一下,可以啊,贴你的SP代码

56,679

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧