出大乱子了,比较郁闷 看不出哪里有错,请教高手

小时候很凶_tusiji 2010-09-08 10:19:24
程序里首先执行一个 select * from aaa where ... 的sql语句
然后执行了一个删除语句 delete aaa where 的sql语句
其次 又一个查询操作 select * from aaa where ... 的操作
然后问题来了。。 我查询出来的数据在数据库中不存在,然后进行插入操作, (注意上面的查询是关系到下面的insert的)
由于 没有查询到值 所以 insert into aaa value 居然会 违反了 PRIMARY KEY 约束
这是什么原因呢 跟奇怪的是 我把程序里的sql打印出来 在数据库里 居然能执行 而且不报错?
跪求
...全文
115 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
OK 问题解决了 结贴了
hao1hao2hao3 2010-09-08
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 dupeng_accp 的回复:]
好人一生平安啊
[/Quote]

把插入语句的ProtectIDarea这一列去掉看看。
还有你的ProtectIDarea是否设置了自增?

另外,如果你能调试话话,那就调试一下看看
sql3="insert into DealerProductTemp(ProductID,ProtectIDarea,DealerID,ProductName,DealerName,CreateTime,SellTime,State,ProtectNum," +
"ChangeTime,Changer,ProvinceID,CityID,Address,Phone,LinkMan)" +
" values('"+dt.getProductID()+"','"+al.get(j)+"','"+dt.getDealerID()+"','"+dt.getProductName()+"','"+dt.getDealerName()+"','"+dt.getCreateTime()+"','"+dt.getSellTime()+"','"+dt.getState()+"','"+((Long.parseLong(al.get(j).substring(14, 26))-Long.parseLong(al.get(j).substring(0, 12)))/10+1)+"'," +
"'"+dt.getChangeTime()+"','"+dt.getChanger()+"','"+dt.getProvinceID()+"','"+dt.getCityID()+"','"+dt.getAddress()+"','"+dt.getPhone()+"','"+dt.getLinkMan()+"')";


sql3的结果是什么,然后在查询分析器里面执行看看是否有问题。

good luck!
  • 打赏
  • 举报
回复
好人一生平安啊
  • 打赏
  • 举报
回复
继续上图 希望好心人帮帮忙

Mr_Nice 2010-09-08
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 dupeng_accp 的回复:]
有图有真相
[/Quote]

看不见图,真相不明...
Mr_Nice 2010-09-08
  • 打赏
  • 举报
回复
[Quote=引用楼主 dupeng_accp 的回复:]
程序里首先执行一个 select * from aaa where ... 的sql语句
然后执行了一个删除语句 delete aaa where 的sql语句
其次 又一个查询操作 select * from aaa where ... 的操作
然后问题来了。。 我查询出来的数据在数据库中不存在,然后进行插入操作, (注意上面的查询是关系到下面的insert的)
由于 没有查询到……
[/Quote]

语句可以在SSMS里面执行...!!!代码里却报错.... 是个问题!!!
  • 打赏
  • 举报
回复


有图有真相
Andy__Huang 2010-09-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 chenqqabcdchenqqabcd 的回复:]
有一种可能。就是你的ID。或者什么的是自增的 。不让你手动插入。所以引起了主键约束!还有一种可能就是你的aaa数据库可能关联到了其他的数据库。导致这个错误!
[/Quote]

你的语句没有错。支持三楼的分析
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 orchidcat 的回复:]
引用楼主 dupeng_accp 的回复:
程序里首先执行一个 select * from aaa where ... 的sql语句
然后执行了一个删除语句 delete aaa where 的sql语句
其次 又一个查询操作 select * from aaa where ... 的操作
然后问题来了。。 我查询出来的数据在数据库中不存在,然后进行插入操作, (注意上面的查询是关系到下面……
[/Quote]
他删除语句 和插入的东西不一样 这个是可以确认的 就算不成功也不影响 主键约束 不过谢谢你的回答
Mr_Nice 2010-09-08
  • 打赏
  • 举报
回复
[Quote=引用楼主 dupeng_accp 的回复:]
程序里首先执行一个 select * from aaa where ... 的sql语句
然后执行了一个删除语句 delete aaa where 的sql语句
其次 又一个查询操作 select * from aaa where ... 的操作
然后问题来了。。 我查询出来的数据在数据库中不存在,然后进行插入操作, (注意上面的查询是关系到下面的insert的)
由于 没有查询到……
[/Quote]


delete aaa where ??/ 没有执行成功吧!

  • 打赏
  • 举报
回复
代码如上 就连接了一个数据库
  • 打赏
  • 举报
回复

try{
con.setAutoCommit(false);
// 标记区间的开始部分
String begin;
//标记区间的结束部分
String end;
// 定义一个用于删除,并添加区间的 st
st=con.createStatement();
String sql2="";
String sql3="";

FanTime ft=new FanTime();
//查询出区间的开头结尾的Lang形式
String selbegin;
String selend;
//原来区间截取后的数组
ArrayList<String> al=new ArrayList<String>();
for(int i=0;i<cit.getInterval().size();i++){

begin= cit.getInterval().get(i).substring(0, 12);
end=cit.getInterval().get(i).substring(14, 26);
sql="select top 1 * from DealerProductTemp d where convert(bigint,'"+end+"')>=(convert(bigint,rtrim(substring(d.ProtectIDarea,0,13)))) and convert(bigint,'"+begin+"')<=(convert(bigint,rtrim(substring(d.ProtectIDarea,15,30)))) and state='A' ";
ps=con.createStatement();
rs=ps.executeQuery(sql);
while(rs.next()){
dt.setAddress(rs.getString("address"));
dt.setChanger(rs.getString("changer"));
dt.setChangeTime(rs.getString("changeTime"));
dt.setCityID(rs.getString("cityID"));
dt.setCreateTime(rs.getString("createTime"));
dt.setDealerID(rs.getString("dealerID"));
dt.setDealerName(rs.getString("dealerName"));
dt.setLinkMan(rs.getString("linkMan"));
dt.setMemo(rs.getString("memo"));
dt.setPhone(rs.getString("phone"));
dt.setProductID(rs.getString("productID"));
dt.setProductName(rs.getString("productName"));
dt.setProtectIDarea(rs.getString("protectIDarea"));
dt.setProtectNum(rs.getString("protectNum"));
dt.setProvinceID(rs.getString("provinceID"));
dt.setSellTime(rs.getString("sellTime"));
dt.setState(rs.getString("state"));
//获取区间的开头

sql2="delete DealerProductTemp where protectIDarea='"+dt.getProtectIDarea()+"'";
st.executeUpdate(sql2);
selbegin=dt.getProtectIDarea().substring(0, 12);
selend=dt.getProtectIDarea().substring(14, 26);
//判断区间。进行处理
// if(Long.parseLong(begin)>Long.parseLong(selbegin)){
//
// al.add(selbegin+"--"+this.calculation(begin, "-"));
//// al.add(String.valueOf(Long.parseLong(end)+1)+"--"+String.valueOf(selend));
//
// }
if(Long.parseLong(selbegin)<Long.parseLong(begin)){
al.add(selbegin+"--"+this.calculation(begin, "-"));
}
if(Long.parseLong(selend)>Long.parseLong(end)){
al.add(this.calculation(end, "+")+"--"+selend);
}

}
//循环将之前的非重合部分再存入到表里面
for(int j=0;j<al.size();j++){
StringBuilder sb = new StringBuilder();
sb.append("select ProtectIDarea from DealerProductTemp where ProtectIDarea ='"+al.get(j)+"'");
rs=ps.executeQuery(sb.toString());
if(rs.getRow()==0){
sql3="insert into DealerProductTemp(ProductID,ProtectIDarea,DealerID,ProductName,DealerName,CreateTime,SellTime,State,ProtectNum," +
"ChangeTime,Changer,ProvinceID,CityID,Address,Phone,LinkMan)" +
" values('"+dt.getProductID()+"','"+al.get(j)+"','"+dt.getDealerID()+"','"+dt.getProductName()+"','"+dt.getDealerName()+"','"+dt.getCreateTime()+"','"+dt.getSellTime()+"','"+dt.getState()+"','"+((Long.parseLong(al.get(j).substring(14, 26))-Long.parseLong(al.get(j).substring(0, 12)))/10+1)+"'," +
"'"+dt.getChangeTime()+"','"+dt.getChanger()+"','"+dt.getProvinceID()+"','"+dt.getCityID()+"','"+dt.getAddress()+"','"+dt.getPhone()+"','"+dt.getLinkMan()+"')";
st.executeUpdate(sql3);
}
}
//将新的记录插入到该表
sql="insert into DealerProductTemp(ProductID,ProtectIDarea,DealerID,ProductName,DealerName,CreateTime,SellTime,State,ProtectNum," +
"ChangeTime,Changer,ProvinceID,CityID,Address,Phone,LinkMan)" +
"values('"+dt.getProductID()+"','"+cit.getInterval().get(i)+"','"+db.getDealerID()+"','"+dt.getProductName()+"','"+db.getDealerName()+"','"+ft.nowDate("yyyy-MM-dd")+"','"+dt.getSellTime()+"','"+state+"','"+((Long.parseLong(end)-Long.parseLong(begin))/10+1)+"'," +
"'"+dt.getChangeTime()+"','"+dt.getChanger()+"','"+db.getProvinceID()+"','"+db.getCityID()+"','"+db.getAddress()+"','"+db.getPhone()+"','"+db.getLinkMan()+"')";
st.executeUpdate(sql);
//将新的记录插入到该表

al.clear();


}
con.setAutoCommit(true);

  • 打赏
  • 举报
回复
有一种可能。就是你的ID。或者什么的是自增的 。不让你手动插入。所以引起了主键约束!还有一种可能就是你的aaa数据库可能关联到了其他的数据库。导致这个错误!
hao1hao2hao3 2010-09-08
  • 打赏
  • 举报
回复
delete aaa where

insert into aaa value
之间的完整操作都贴出来看看。
lxq19851204 2010-09-08
  • 打赏
  • 举报
回复
帖代码....
wcj1018_net 2010-09-08
  • 打赏
  • 举报
回复
插入字段写上对应的列名
例如insert into aaa (productid) values(2)

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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