In a windows application I have a query that im trying to insert the current date on my mysql database
query below:
SqlVentaCasas1 = "INSERT INTO VentaCasasHistory (ID, Direccion, Estatus, Precio, " & _
"NumeroDias, FechaHoy, Agente, Compania, Unidad, Ciudad ) VALUES ('" & _
AddIDCasas2 & "','" & AddDireccionCasas2 & "','" & AddEstatusCasas2 & "'," & AddPrecioCasas2 & ", 0, CURDATE(), '" & AgenteNameCasas2 & "','" & AgenteCompaniaCasas2 & "', '" & AddUnidadCasas2 & "', '" & AddCiudadCasas2 & "' );"
CommandVentaCasas1 = New MySqlCommand(SqlVentaCasas1, con)
CommandVentaCasas1.CommandType = CommandType.Text
CommandVentaCasas1.ExecuteNonQuery()
when i input the values that i want to insert into mysql everything runs smoothly except that when i check the database for the date entered i find myself with the 0000-00-00
on the other hand, when i run the same query from PHPmyAdmin
INSERT INTO `VentaCasasHistory`(`ID`, `Direccion`, `Estatus`, `Precio`, `NumeroDias`, `FechaHoy`, `Agente`, `Compania`, `Unidad`, `Ciudad`) VALUES ([String1],[String2],[String3],[Integer1],[Integer2],CURDATE(),[String4],[String5],[String6],[String7])
where FechaHoy is CURDATE() is does insert correctly the date that should be.
I cant seem to find what is the problem, As i understand it will only insert 0000-00-00 if there is something wrong with the date when been entered.
the reason i kept both queries "identical" was to understand what im doing wrong and I know that they can be subject to SQL injection.
your help would be very appreciated.
Leo P.
this is the code i have for the last piece that you told me.
Dim StringDate As String
StringDate = Date.Today.ToString("yyyy-MM-dd")
SqlVentaCasas1 = "INSERT INTO VentaCasasHistory (ID, Direccion, Estatus, Precio, " & _
"NumeroDias, FechaHoy, Agente, Compania, Unidad, Ciudad ) VALUES ('" & _
AddIDCasas2 & "','" & AddDireccionCasas2 & "','" & AddEstatusCasas2 & "'," & AddPrecioCasas2 & ", 0, STR_TO_DATE('" & StringDate & "', GET_FORMAT(DATE,'ISO')), '" & AgenteNameCasas2 & "','" & AgenteCompaniaCasas2 & "', '" & AddUnidadCasas2 & "', '" & AddCiudadCasas2 & "' );"
unfortunately it does not work and i still get the 0000-00-00.
CURDATE()
should really be specified as default value used in CREATE TABLE
. In other words, you don't need to put it in as part of a query as when you insert a new record the field with default value set to CURDATE()
will have the date automatically inserted.
F.ex:
CREATE TABLE MyTable
(
ID int NOT NULL,
MyName varchar(30) NOT NULL,
MyDate datetime NOT NULL DEFAULT CURDATE(),
PRIMARY KEY (ID)
)
INSERT INTO MyTable (MyName) VALUES ('Epistemex')
Will set the MyDate
field to CURDATE()
.
Hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.