From 6c3c32763021dda7b99a75cee9875710bec18373 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sat, 21 Aug 2021 07:51:26 +0200 Subject: [PATCH] First commit. Draft. --- .gitignore | 2 + go.mod | 5 +++ grid.db | Bin 0 -> 8192 bytes main.go | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 grid.db create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3325d94 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.*~ +go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0ac1ed5 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module examples.com/zakgrid_go + +go 1.16 + +require github.com/mattn/go-sqlite3 v1.14.8 diff --git a/grid.db b/grid.db new file mode 100644 index 0000000000000000000000000000000000000000..96ad34c2cb74a390945791380fdbc076169f9edc GIT binary patch literal 8192 zcmeI#JxT*X6bJBmn_WaOjIb;zwt3j7AX<0yzp z39sM{oIqBvvbFd>=J7sw)vufJZEb?)d*>G_@UhqyMB;>52$981Vs=5XNxLD|)%;gv z;_&*lAFoKhiTDr@fB*y_009U<00Izz00bZa0SNpEw6rlOOM2N2>Ta&5bE9oou1j(@ zs)mz_C&Saa;`NgEOvA%s\n", v.title) + fmt.Println(i, v) + } + + header += "\n" + + return header +} + +func (g *grid) getRowFromDb (m map[string]interface{}) string { + ret := "\n" + + for _, c := range g.cols { + ret += fmt.Sprintf("\t%v\n", m[c.name]) + } + + ret += "\n" + + return ret +} + +func main() { + var g grid + + c := column{name: "id", title: "ID"} + g.addColumn(c) + + c = column{name: "name", title: "Name"} + g.addColumn(c) + + c = column{name: "surname", title: "Surname"} + g.addColumn(c) + + c = column{name: "age", title: "Age"} + g.addColumn(c) + + fmt.Println (g) + + header := g.getHeader() + + fmt.Println(header) + + db, err := sql.Open("sqlite3", "./grid.db") + if err != nil { + // handle err + log.Fatal(err) + } + + rows, err := db.Query("SELECT * FROM clients") + if err != nil { + // handle err + log.Fatal(err) + } + cols, err := rows.Columns() + if err != nil { + // handle err + log.Fatal(err) + } + + fmt.Println(cols) + + columns := make([]interface{}, len(cols)) + columnPointers := make([]interface{}, len(cols)) + for i, _ := range columns { + columnPointers[i] = &columns[i] + } + + for rows.Next() { + err = rows.Scan(columnPointers...) + fmt.Println(columnPointers) + + m := make(map[string]interface{}) + for i, colName := range cols { + val := columnPointers[i].(*interface{}) + m[colName] = *val + } + + fmt.Println(m) + + fmt.Println(g.getRowFromDb(m)) + } +} -- 2.49.0