Spanish (SOLUCIONADO) SQLite y campos fecha

Piluso

Member
Licensed User
Buen día.
Estoy trabajando con SQLite y el gestor que uso para crear y gestionar a la BBDD es https://sqlitebrowser.org/dl/ y el inconveniente que tengo es que no me deja crear campos fecha lo que me complica a la hora de programar.
Me podrán decir como creo y/o gestiono SQLite y que pueda crear campos de este tipo?


Saludos
 

musaso

Active Member
Licensed User
Buen día.
Estoy trabajando con SQLite y el gestor que uso para crear y gestionar a la BBDD es https://sqlitebrowser.org/dl/ y el inconveniente que tengo es que no me deja crear campos fecha lo que me complica a la hora de programar.
Me podrán decir como creo y/o gestiono SQLite y que pueda crear campos de este tipo?


Saludos
SQLite no provee un tipo de dato especial para almacenar fechas y horas, por lo que debemos utilizar alguno de los tipos existentes.

Mira AQUI
 

musaso

Active Member
Licensed User
Yo los guardo en formato Long.
Dim hoy As Long = DateTime.Now
hoy = 1655052702360 (domingo 12 Junio 2022 18:51:42)
 

Piluso

Member
Licensed User
Buen dia.

Ahora lo miro
Yo lo almacenaria en un string en formato yyyymmdd
Así lo tengo definido en este momento.
El tema es que siguiendo este ejemplo me muestra bien las fechas ya cargadas y las de alta usando este código.

B4X:
'===========================================================================
    PrefDialog.Initialize(Root, "Redacción de Tareas", 300dip, 300dip)
    PrefDialog.LoadFromJson (File.ReadString(File.DirAssets, "template1.json"))   
'===========================================================================
    EditCol =B4XTable1.AddColumn("Fecha", B4XTable1.COLUMN_TYPE_TEXT)
    EditCol.Width = 25%x
    EditCol =B4XTable1.AddColumn("Tarea", B4XTable1.COLUMN_TYPE_TEXT)
    EditCol.Width = 75%x
    B4XTable1.RowHeight = 70dip
    B4XTable1.MaximumRowsPerPage=7
    B4XTable1.BuildLayoutsCache(B4XTable1.MaximumRowsPerPage)
'===========================================================================
Pero cuando quiero editar algún campo, me tira este error...

B4X:
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
(MyMap) {Fecha=2022-05-16, Tarea=Prueba de injertos en 2 Almendros y 2 cruelos}
2022-05-16
Error occurred on line: 442 (PreferencesDialog)
java.lang.NumberFormatException: For input string: "2022-05-16"
    at java.lang.Long.parseLong(Long.java:594)
    at java.lang.Long.parseLong(Long.java:636)
    at anywheresoftware.b4a.BA.ObjectToLongNumber(BA.java:692)
    at b4a.example.preferencesdialog._filldata(preferencesdialog.java:2451)
    at b4a.example.preferencesdialog$ResumableSub_ShowDialog.resume(preferencesdialog.java:587)
    at b4a.example.preferencesdialog._showdialog(preferencesdialog.java:405)
    at b4a.example.b4xmainpage$ResumableSub_ShowDialog.resume(b4xmainpage.java:220)
    at b4a.example.b4xmainpage._showdialog(b4xmainpage.java:189)
    at b4a.example.b4xmainpage._b4xtable1_celllongclicked(b4xmainpage.java:179)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:1085)
    at anywheresoftware.b4a.keywords.Common.CallSubNew3(Common.java:1048)
    at b4a.example.b4xtable$ResumableSub_CellClicked.resume(b4xtable.java:1501)
    at b4a.example.b4xtable._cellclicked(b4xtable.java:1242)

Ya probé con datetime.dateparse en distintos lugares pero no encontré la solución todavía. Por eso se me ocurrió preguntar, para ver si podía cargar como campo fecha directamente así evito usar "dateparse".

Les paso el proyecto por si alguno quiere darle una mirada, el evento que tira el error es el click extendido sobre algún de los campos detalle o fecha.
 

Attachments

  • 5a_B4xtable.zip
    12.2 KB · Views: 89

musaso

Active Member
Licensed User
Buen dia.


Ahora lo miro

Así lo tengo definido en este momento.
El tema es que siguiendo este ejemplo me muestra bien las fechas ya cargadas y las de alta usando este código.

B4X:
'===========================================================================
    PrefDialog.Initialize(Root, "Redacción de Tareas", 300dip, 300dip)
    PrefDialog.LoadFromJson (File.ReadString(File.DirAssets, "template1.json"))  
'===========================================================================
    EditCol =B4XTable1.AddColumn("Fecha", B4XTable1.COLUMN_TYPE_TEXT)
    EditCol.Width = 25%x
    EditCol =B4XTable1.AddColumn("Tarea", B4XTable1.COLUMN_TYPE_TEXT)
    EditCol.Width = 75%x
    B4XTable1.RowHeight = 70dip
    B4XTable1.MaximumRowsPerPage=7
    B4XTable1.BuildLayoutsCache(B4XTable1.MaximumRowsPerPage)
'===========================================================================
Pero cuando quiero editar algún campo, me tira este error...

B4X:
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
(MyMap) {Fecha=2022-05-16, Tarea=Prueba de injertos en 2 Almendros y 2 cruelos}
2022-05-16
Error occurred on line: 442 (PreferencesDialog)
java.lang.NumberFormatException: For input string: "2022-05-16"
    at java.lang.Long.parseLong(Long.java:594)
    at java.lang.Long.parseLong(Long.java:636)
    at anywheresoftware.b4a.BA.ObjectToLongNumber(BA.java:692)
    at b4a.example.preferencesdialog._filldata(preferencesdialog.java:2451)
    at b4a.example.preferencesdialog$ResumableSub_ShowDialog.resume(preferencesdialog.java:587)
    at b4a.example.preferencesdialog._showdialog(preferencesdialog.java:405)
    at b4a.example.b4xmainpage$ResumableSub_ShowDialog.resume(b4xmainpage.java:220)
    at b4a.example.b4xmainpage._showdialog(b4xmainpage.java:189)
    at b4a.example.b4xmainpage._b4xtable1_celllongclicked(b4xmainpage.java:179)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:1085)
    at anywheresoftware.b4a.keywords.Common.CallSubNew3(Common.java:1048)
    at b4a.example.b4xtable$ResumableSub_CellClicked.resume(b4xtable.java:1501)
    at b4a.example.b4xtable._cellclicked(b4xtable.java:1242)

Ya probé con datetime.dateparse en distintos lugares pero no encontré la solución todavía. Por eso se me ocurrió preguntar, para ver si podía cargar como campo fecha directamente así evito usar "dateparse".

Les paso el proyecto por si alguno quiere darle una mirada, el evento que tira el error es el click extendido sobre algún de los campos detalle o fecha.

Creo que para guardar la fecha debes hacer :
B4X:
 DateTime.DateFormat ="yyyy-MM-dd"'el formato que quieras
 Dim fecha As Long = DateTime.DateParse(Item.Get("Fecha"))'la fecha que sea la guardas en base de datos como Long
parametros.AddAll(Array( fecha), Item.Get("Tarea"))) 'keys based on the template json file


'para recuperarla en el formato
Dim fechatexto As String = DateTime.Date(Item.GetValueAt(0))'GetValueAt no deberias usarlo
Item.Put("Fecha",fechatexto)
 

Piluso

Member
Licensed User
Otro inconveniente corregido gracias a la ayuda de uds.
Tal cual recomendó @musaso guardé primero la fecha convertida en una variable y después corregí el mapa que lleva los datos al ShowDialog.

B4X:
Private Sub B4XTable1_CellLongClicked (ColumnId As String, RowId As Long)
    Dim MiFecha As Long= DateTime.DateParse(B4XTable1.GetRow(RowId).GetValueAt(0))
    Dim item As Map = B4XTable1.GetRow(RowId)
    item.Put ("Fecha", MiFecha)
    ShowDialog(item, RowId)
End Sub

Muchas gracias por ahora.
 
Top