Skip to content

Instantly share code, notes, and snippets.

View kitz99's full-sized avatar

Bogdan Timofte kitz99

View GitHub Profile
func updateRecord(fieldName string, newVal string, condFieldName string, condFieldValue int64) error {
qryString := fmt.Sprintf("UPDATE posts set %s='%s' WHERE %s=%d", fieldName, newVal, condFieldName, condFieldValue)
fmt.Printf("Query: %v\n", qryString)
_, err := roachConnection.Exec(qryString)
return err
}
func readRecords() []Post {
var result []Post
rows, err := roachConnection.Query("select * FROM posts;")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
func createRecord(post Post) error {
qryString := fmt.Sprintf(
"INSERT INTO posts (name, category, author, created_at, updated_at) VALUES ('%s', '%s', '%s', NOW(), NOW())",
post.Name, post.Category, post.Author)
_, err := roachConnection.Exec(qryString)
return err
}
func init() {
roachConnection, err := sql.Open("postgres", "postgresql://root@localhost:26257/blog_db?sslmode=disable")
if err != nil {
panic("Could not establish connection to CockroachDB")
}
}
$ docker exec -it goroach_master_1 ./cockroach sql --insecure
> create database blog_db;
> set database = blog_db;
> CREATE TABLE "posts" (
"id" SERIAL,
"name" STRING(100),
"category" STRING(50),
"author" STRING(50),
version: '2'
networks:
roachnet:
services:
master:
image: cockroachdb/cockroach:v2.0.3
command: start --insecure
ports:
@kitz99
kitz99 / sh
Created June 11, 2018 06:26
LinuxTestBash
#!/bin/bash
echo "Hello world"