Results 1 to 10 of 10

Thread: SQLite Text to Date not working

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2012
    Posts
    440

    SQLite Text to Date not working

    I'm downloading a JSON file and saving it into my SQLite DB. There's a date field in the JSON, 2021-11-25 for example.

    Geting JSON data into List of.
    Code:
    Dim personList = JsonSerializer.Deserialize(Of List(Of Classes.Person))(jsonResponse, serializerOptions)
    Inserting into my DB and once inside as the correct format in a text colum
    Code:
    For Each p As Person In PersonList
        con.Execute("INSERT OR REPLACE INTO Persons(personId, dob) VALUES (@personId, @Dob)", p)
    Next
    When i come to pull the data from the database, I get just the year 2024, 2018 whatever but not the month or day. Why?
    Code:
        listBox1.Enabled = False
        Dim output = con.Query(Of Person)("SELECT * FROM Person", New DynamicParameters())
        listBox1.DataSource = output.ToList()
        listBox1.DisplayMember = "personId"
        listBox1.Enabled = True
    Person Class
    Code:
    Public Class Person
        Public Property personId As Integer
        Public Property Dob As String
    End Class

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2012
    Posts
    440

    Re: SQLite Text to Date not working

    Data in MySQL is showing as 1982-05-21 as one example.

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,712

    Re: SQLite Text to Date not working

    Rather than a text column, use a DateTime column.

    If the time part is not needed, use a DateOnly by converting from DateTime using DateOnly.FromDateTime

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2012
    Posts
    440

    Re: SQLite Text to Date not working

    DateTime is not a valid column on SQLite otherwise I would have

  5. #5
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    769

    Re: SQLite Text to Date not working

    If you are putting a string into the database you should (obviously) get the same string out. Have you checked what is actually going into the database using something like SQLiteStudio? I store dates in SQLite in ISO-8601 format and convert to DateTime objects in vb.net.
    Last edited by paulg4ije; Feb 9th, 2025 at 09:49 AM.

  6. #6
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,348

    Re: SQLite Text to Date not working

    Quote Originally Posted by ubkra View Post
    DateTime is not a valid column on SQLite otherwise I would have
    SQLite has a Date data type. Try that.

    Edit:

    Wait a second. Here you say your using SQLite

    I'm downloading a JSON file and saving it into my SQLite DB
    And here you reference MySql

    Data in MySQL is showing as 1982-05-21 as one example.
    Why??

    Also, need to see the code for displaying "dob".
    Last edited by wes4dbt; Feb 9th, 2025 at 03:55 PM.

  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,975

    Re: SQLite Text to Date not working

    It's called debugging.
    Start with your deserialized data.
    Does the full date even arrive in your "PersonList"?
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  8. #8
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,712

    Re: SQLite Text to Date not working

    Quote Originally Posted by ubkra View Post
    DateTime is not a valid column on SQLite otherwise I would have
    It is a valid type.
    Attached Images Attached Images  

  9. #9
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,975

    Re: SQLite Text to Date not working

    Quote Originally Posted by kareninstructor View Post
    It is a valid type.
    Karen, have to disagree.
    https://www.sqlite.org/lang_datefunc.html

    SQLite does not have a dedicated date/time datatype. Instead, date and time values can stored as any of the following:

    ISO-8601 A text string that is one of the ISO 8601 date/time values shown in items 1 through 10 below. Example: '2025-05-29 14:16:00'
    Julian day number The number of days including fractional days since -4713-11-24 12:00:00 Example: 2460825.09444444
    Unix timestamp The number of seconds including fractional seconds since 1970-01-01 00:00:00 Example: 1748528160
    Though you CAN use Datatypes like Date, DateTime (and others you're used to from other DBMS) during creation of a Table (via SQL CREATE TABLE blablabla), and SQLite will even save it in its Metadata.
    Internally, SQLite will use its own storage class with the closest Affinity
    https://www.sqlite.org/datatype3.html
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  10. #10
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,712

    Re: SQLite Text to Date not working

    All that matters is a developer can work with dates no matter the Metadata.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width