我有一个奇怪的MySQL问题,我以前从未见过。基本上,我需要在表格中输入相同的条目x次。主键是自动增量,因此不应存在任何重复值问题。但是,它需要最后几列,并且似乎将它们全部聚合为一个值并将其用作关键字。我得到的错误是:
Duplicate entry '80-0--2011-06-16-0-1' for key 'idx_mt'
请注意,没有名为idx_mt的字段。主键字段仅称为ID。并且80-0--2011-06-16-0-1是最后8列左右(你可以看到cols变量中的名字)连接起来,他们显然不应该这样做。此外,第一次循环运行时,条目将通过,但后者与之冲突。我通过将最后8列中的一列更改为基于循环的值来确认这是问题,并且没有任何问题。有什么见解吗?
Dim cols As String = ""
Dim msi As System.Globalization.DateTimeFormatInfo = New System.Globalization.DateTimeFormatInfo()
cols = "TEMPLATE_NAME," & _
"DESCRIPTION," & _
"FORMAT," & _
"SENDER," & _
"`REPLY-TO`," & _
"SUBJECT," & _
"BODYHTML," & _
"BODYTEXT," & _
"CAMPAIGN_ID," & _
"SPLIT_GROUP_ID," & _
"TEMPLATE_IDENTIFIER," & _
"MESSAGE_SEND_DATE," & _
"DAYS_TO_DELAY_SEND," & _
"ACTIVE"
Try
Dim test As Integer = CInt(cmbEffort.Text)
Catch ex As Exception
MsgBox("Please use numerical values from 1 - 6 for the effort #.")
Return
End Try
If (cmbEffort.Text < 1 Or cmbEffort.Text > 6) Then
MsgBox("Please use numerical values from 1 - 6 for the effort #.")
Return
End If
Dim query As String = ""
Dim _date As Date = DateTimePicker1.Value
Dim dateSent As String = ""
Dim html As String = ""
Dim temp As message
If (_date.DayOfWeek <> DayOfWeek.Thursday) Then
MsgBox("Selected date must be a Thursday.")
Return
End If
dateSent = formatDate(_date)
Try
fileStreamIn = New IO.FileStream(txtHTML.Text, IO.FileMode.Open)
streamReader = New IO.StreamReader(fileStreamIn)
Catch ex As Exception
MsgBox("HTML File not found.")
Return
End Try
html = streamReader.ReadToEnd()
html = html.Replace("'", "&apos")
streamReader.Close()
server.query("SELECT * FROM TEMPLATES_TO_COPY WHERE CAMPAIGN_ID = " & cmpgnID)
server.read()
temp = getMessage(cmpgnID.ToString())
temp.bodyText = txtMessage.Text
temp.HTML = html
temp.sendDate = dateSent
temp.description = txtDescription.Text & "_" & cmbEffort.Text
temp.campaignID = cmpgnID
temp.name = temp.name.Replace("MMM", msi.GetMonthName(_date.Month).Substring(0, 3).ToUpper())
temp.name = temp.name.Replace("YY", _date.Year.ToString().Substring(2, 2))
For i As Integer = 1 To cmbEffort.Text
temp.name = temp.name.Replace("X", (i + 1).ToString())
query = "INSERT INTO MESSAGE_TEMPLATES (" & _
cols & ")" & _
"VALUES ('" & temp.name & "','" & temp.description & "','" & temp.format & "','" & temp.sender & "','" & temp.replyTo & "','" & temp.subject & "','" & temp.HTML & "','" & temp.bodyText & "'," & temp.campaignID & "," & 0 & ",'','" & temp.sendDate & "'," & temp.daysToDelay & "," & temp.active & ");"
If (Not server.query(query)) Then
Return
End If
temp.name = temp.name.Replace((i + 1).ToString(), "X")
Next
答案 0 :(得分:2)
错误正是它所说的:
Duplicate entry '80-0--2011-06-16-0-1' for key 'idx_mt'
您定义的键/索引名为idx_mt
。此键在少数几列中设置。正如其他人在评论中指出的那样,所有这些列的值一起与表中已有的行相同,从而导致这种冲突。
将字段连接在一起的重复条目是MySQL如何显示该键的值(甚至可能是商店,我不确定)。它基本上像另一列一样跟踪它。放在那里的值由表中的其他列组成。
当您看到80-0--2011-06-16-0-1
时,表示此特定行上此索引中的字段的值为80,0,null
,2011-06-16,0和1。 INSERT与此相冲突。