@ -20,12 +20,12 @@ func initializeDatabase() {
}
func writeToDB ( post BlogPost ) {
stmt , _ := db . Prepare ( "INSERT INTO blogposts(author, title, content, time) VALUES (?, ?, ?, ?)")
stmt . Exec ( post . Author , post . Title, post . Content, time . Now ( ) . Unix ( ) )
stmt , _ := db . Prepare ( "INSERT INTO blogposts(author, content, time) VALUES (?, ?, ?)")
stmt . Exec ( post . Author , post . Content, time . Now ( ) . Unix ( ) )
}
func readBlogpostsFromDB ( ) [ ] BlogPost {
res , err := db . Query ( "SELECT content, author, title, time from blogposts")
res , err := db . Query ( "SELECT content, title, time from blogposts limit 50 ")
if err != nil {
log . Println ( "Error reading blogposts" )
return make ( [ ] BlogPost , 0 )
@ -38,10 +38,10 @@ func resultToBlogposts(res *sql.Rows) []BlogPost {
defer res . Close ( )
entries := make ( [ ] BlogPost , 0 )
for res . Next ( ) {
var content , author , title string
var content , author string
var timeInt int64
res . Scan ( & content , & author , & ti tle, & ti meInt)
post := BlogPost { Content : content , Author : author , Title: title , FTime: time . Unix ( timeInt , 0 ) . Format ( "02.01.2006 - 15:04:05" ) }
res . Scan ( & content , & author , & ti meInt)
post := BlogPost { Content : content , Author : author , FTime: time . Unix ( timeInt , 0 ) . Format ( "02.01.2006 - 15:04:05" ) }
entries = append ( entries , post )
}
return entries