I am setting up a reminder
feature in a personal assistant application. The application accepts the details of the reminder as a html form which also include date
and time
input fields.
I am using golang for creating my server and created a sqlite database using it. But the value of date and time from the form doesn't get saved in the DB while other fields like title, description, etc gets saved. What datatype should I use for my time and date fields in the sqlite DB?
I have tried using TEXT
as the datatype for date and time but it doesn't work.
Input form:
...
<div class="form-group">
<label for="date">Date</label>
<input type="date" class="form-control" id="date" data-ng-model="formData.remDate" required>
</div>
<div class="form-group">
<label for="time">Time</label>
<input type="time" class="form-control" id="time" data-ng-model="formData.remTime" required>
</div>
...
This is the reminder object
that is generated from the form:
{title: "learn golang", description: "How do I save date and time in database?", date: Mon Apr 08 2019 00:00:00 GMT+0530 (India Standard Time), time: Thu Jan 01 1970 20:00:00 GMT+0530 (India Standard Time)}
Golang file for creating and storing in DB:
package controllers
import (
"net/http"
"database/sql"
"fmt"
"strconv"
_ "github.com/mattn/go-sqlite3"
)
type reminder struct {
id int
title string
description string
date string
time string
}
func ReminderController(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
r.ParseForm()
request := reminder{
title: r.FormValue("title"),
description: r.FormValue("description"),
date: r.FormValue("date"),
time: r.FormValue("time"),
}
fmt.Println(request)
AddReminder(request, w)
}
func prepareDB() {
database, err := sql.Open("sqlite3", "./jarvis.db")
checkErr(err)
statement, err := database.Prepare("CREATE TABLE IF NOT EXISTS reminder (id INTEGER PRIMARY KEY, title TEXT, description TEXT, date TEXT, time TEXT")
checkErr(err)
statement.Exec()
}
func AddReminder(reminderObject reminder, res http.ResponseWriter) {
prepareDB()
database, err := sql.Open("sqlite3", "./jarvis.db")
checkErr(err)
statement, err := database.Prepare("INSERT INTO reminder (title, description, date, time) VALUES (?,?,?,?) ")
checkErr(err)
fmt.Println(reminderObject.title)
statement.Exec(reminderObject.title, reminderObject.description, reminderObject.date, reminderObject.time)
ShowReminder()
res.Write([]byte(`{"status": "success", "message": "Reminder has been set !"}`))
}
func ShowReminder() {
prepareDB()
database, err := sql.Open("sqlite3", "./jarvis.db")
checkErr(err)
rows, err := database.Query("SELECT id, title, description, date, time FROM reminder")
checkErr(err)
var id int
var title string
var description string
var date string
var time string
for rows.Next() {
rows.Scan(&id, &title, &description, &date, &time)
fmt.Println(strconv.Itoa(id) + ": " + title + " " + description + "
End Time : " + date + ":" + time)
}
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
Output generated in go console :
3: learn golang How do I save date and time in database?
End Time : :
Use Integer type in sqlite table and your time property in reminder struct should change to time.Time
type reminder struct {
id int
title string
description string
date string
time time.Time
}
In order to convert your form date and time strings into a time.Time format you should parse your strings like so :
layout := "2006-01-02T15:04:05.000Z"
str := fmt.Sprintf("%sT%sZ", r.FormValue("date") , r.FormValue("time"))
t, err := time.Parse(layout, str)