jeudi 9 avril 2015

C# - SQLite update query not working properly

I'll start this question saying that I know this is not the best way of using SQLite statements but it's easier for me when learning. Here's part of my code:



int horas1, minutos1, segundos1, horas2, minutos2, segundos2;
pausaString = "No";
TimeSpan ts = turriStopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds);
MessageBox.Show(elapsedTime, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
string sql = "select tiempoPausado from Registros where pausado = 'SI' and enCurso = 'SI' and operacion = '" + operacionString + "' and operador = '" + operadorString + "'";
command = new SQLiteCommand(sql, conexion);
using (SQLiteDataReader reader2 = command.ExecuteReader())
{
if (reader2.Read())
{
string tiempoGuardado = reader2.GetString(0);

horas1 = Convert.ToInt16(tiempoGuardado.Substring(0, 2));
minutos1 = Convert.ToInt16(tiempoGuardado.Substring(3, 2));
segundos1 = Convert.ToInt16(tiempoGuardado.Substring(6, 2));

horas2 = Convert.ToInt16(elapsedTime.Substring(0, 2));
minutos2 = Convert.ToInt16(elapsedTime.Substring(3, 2));
segundos2 = Convert.ToInt16(elapsedTime.Substring(6, 2));
//MessageBox.Show(horas2 + ";" + minutos2 + ";" + segundos2, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

int sumaHoras = horas1 + horas2;
int sumaMinutos = minutos1 + minutos2;
int sumaSegundos = segundos1 + segundos2;

while (sumaSegundos >= 60)
{
sumaMinutos = sumaMinutos + 1;
sumaSegundos = sumaSegundos - 60;
}
while (sumaMinutos >= 60)
{
sumaHoras = sumaHoras + 1;
sumaMinutos = sumaMinutos - 60;
}

string horarioAGuardar = String.Format("{0:00}:{1:00}:{2:00}", sumaHoras, sumaMinutos, sumaSegundos);

//MessageBox.Show(horarioAGuardar, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

sql = "update Registros set tiempoPausado = '" + horarioAGuardar + "' AND pausado = 'NO' where enCurso = 'SI' AND operador = '" + operadorString + "' AND operacion = '" + operacionString + "'";
command = new SQLiteCommand(sql, conexion);
int rows = command.ExecuteNonQuery();
MessageBox.Show(rows.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

else
{

}

}


The hour calculation is working fine but, let's suppose tiempoAGuardar is "00:00:02", what I get in tiempoPausado from my database is "0" and pausado is not changed to 'NO'. Even though I change tiempoAGuardar with "asd" it stores 0 at the database. Please help, I've been trying to figure this out for days. Thanks


Aucun commentaire:

Enregistrer un commentaire