Error al actualizar la base de datos MySQL: ENTRADA POR DEFECTO DUPLICADA PARA CLAVE PRIMARIA = '0'

I have a MySQL dsetup called in-out and I have written a vb.NET client program to get information from the table in the database, display it in a data grid view, and then allow to user to edit this information and update it on the server.

Right now I am hosting the server on my Gateway laptop and also connecting to it from the same laptop therefore I'm using localhost as the server name. My problem is that when I go into the program and change the information and click update, nothing happens... The information stays the same yet there is no sign of an error, syntax failure, or program crash.

I've tried running this on another computer in my house and I get the same results. I can access the information without a hitch but updating it is where I run into trouble. If there was a problem with my code it would have displayed some sort of error or asked me to debug my script, which would have made it a lot easier to solve, therefore I am certain that it has something to do with my database.

Before I got to this step, I kept getting an error when retrieving the information that said something like

DUPLICATE DEFAULT ENTRY FOR PRIMARY KEY = '0'

Which means that the columns in the table related to this error cannot have more than one default value of '0', but that's gone now... (Even though I didn't change anything)

Here is the script that will recreate my database layout. Just run it in MySQL WorkBench or MySQL Query Browser (or what ever your using to manage your SQL Database).

Here's my update code: (just in case the problem lies in my program not the database)

Private Sub cmdupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdupdate.Click
   Dim conn As New MySqlConnection
    Dim myCommand As New MySqlCommand

    '#######
    conn.ConnectionString = "server=" & frmLogin.txtserver.Text & ";" _
& "user id=" & frmLogin.txtusername.Text & ";" _
& "password=" & frmLogin.txtpassword.Text & ";" _
& "database=in_out"
    '#######

    myCommand.Connection = conn
    myCommand.CommandText = "INSERT INTO event(user_id, message_id, timestamp, status, creator)" _
     & "VALUES(?UserID, ?MessageID, NOW(), ?Status, ?Creator)"

    myCommand.Parameters.AddWithValue("?UserID", myUserID)
    myCommand.Parameters.AddWithValue("?MessageID", cbomessage.SelectedValue)
    myCommand.Parameters.AddWithValue("?Status", cbostatus.SelectedItem)
    myCommand.Parameters.AddWithValue("?Creator", myUserID)

    Try
        conn.Open()
        myCommand.ExecuteNonQuery()
    Catch myerror As MySqlException
        MsgBox("There was an error updating the database: " & myerror.Message)
    End Try
    refreshStatus(dgvstatus)

End Sub

Detalles adicionales:

  • OS: Windows 7 Professional x64
  • Software: Visual Basic 2010 Express
  • Server Name: 'Localhost'
  • SQL Manager: MySQL Workbench 5.2.34 CE

preguntado el 27 de agosto de 11 a las 16:08

1 Respuestas

Seems that you have some sort of transaction problem going on...

intenta agregar myCommand.Connection.Close(); después de la ExecuteNonQuery()

EDITAR - según el comentario:

Some links to learn SQL:

EDIT 2:

UPDATE event SET
timestamp = NOW(), 
status = ?Status 
WHERE user_id = ?UserID AND message_id = ?MessageID AND creator = ?Creator;

Since there is not enough details about the data model the above UPDATE statement assumes that the columns user_id y message_id y creator together identify a row uniquely... and update the timestamp y status columns accordingly...

Respondido 29 ago 11, 06:08

that didnt help... i think it has something to do with the "ExecuteNonQuery()" function, because that is the only real difference between my update code and my refresh code... could you explain what the Execute non Query function does exactly?? or maybe an alternative update code? - daniel11

the ExecuteNonQuery tells the DB to execute your code... but does not expect some sort of CURSOR like a SELECT would return... thus can be used for INSERT/UPDATE/DELETE and so on... please explain exactly what you want to achieve and what does not work... - Yahia

i want to get the information from my database, display it in a datagridview control, be able to change the information displayed in the datagridview control using combobox selections that i have pre defined, and finally, update the information on the database with the information in the datagridview control. the updating script (found in the original post) isn't working at all. i can change information on the datagridview but it wont change it on the database... - daniel11

in the original Port I don't see an UPDATE script but I see an INSERT script... how much SQL do you know ? - Yahia

i started writing this program (which is my first sql based script) about a month ago... but before i started i read up on the syntax of sql statements. and your right , there is no update script because i assumed that if you INSERT information into the same column you got it from then it would update it by default... - daniel11

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.