started rewrite into golang
This commit is contained in:
90
main.go
Normal file
90
main.go
Normal file
@ -0,0 +1,90 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/ncruces/go-sqlite3"
|
||||
_ "github.com/ncruces/go-sqlite3/embed"
|
||||
)
|
||||
|
||||
const memory = ":memory:"
|
||||
|
||||
func main() {
|
||||
db, err := sqlite3.Open(memory)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
/* Not necessary because I am currently saving the db in memory
|
||||
sqlstmt := `
|
||||
DROP TABLE IF EXISTS artists;
|
||||
DROP TABLE IF EXISTS websites;
|
||||
DROP TABLE IF EXISTS links;
|
||||
`
|
||||
err = db.Exec(sqlstmt)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
*/
|
||||
|
||||
sqlstmt := `
|
||||
CREATE TABLE IF NOT EXISTS artists (
|
||||
id integer NOT NULL CONSTRAINT artists_pk PRIMARY KEY AUTOINCREMENT,
|
||||
name text NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS websites (
|
||||
id integer NOT NULL CONSTRAINT websites_pk PRIMARY KEY AUTOINCREMENT,
|
||||
name text NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS links (
|
||||
id integer NOT NULL CONSTRAINT links_pk PRIMARY KEY AUTOINCREMENT,
|
||||
url text NOT NULL,
|
||||
websites_id integer NOT NULL,
|
||||
artists_id integer NOT NULL,
|
||||
CONSTRAINT links_artists FOREIGN KEY (artists_id)
|
||||
REFERENCES artists (id),
|
||||
CONSTRAINT links_websites FOREIGN KEY (websites_id)
|
||||
REFERENCES websites (id)
|
||||
);
|
||||
`
|
||||
err = db.Exec(sqlstmt)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
/*
|
||||
err = db.Exec(`INSERT INTO users (id, name) VALUES (0, 'go'), (1, 'zig'), (2, 'whatever')`)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
*/
|
||||
|
||||
sqlstmt = `
|
||||
SELECT name
|
||||
FROM sqlite_schema
|
||||
WHERE type ='table' AND name NOT LIKE 'sqlite_%';
|
||||
`
|
||||
|
||||
stmt, _, err := db.Prepare(sqlstmt)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
for stmt.Step() {
|
||||
log.Println("created table:", stmt.ColumnText(0))
|
||||
}
|
||||
if err := stmt.Err(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = stmt.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = db.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user