Skip to content

Commit

Permalink
Work-around for mattn/go-sqlite3#39
Browse files Browse the repository at this point in the history
  • Loading branch information
Neal committed Jan 17, 2014
1 parent 44b6199 commit e88336b
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions web/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type DbConnection struct {
}

func InitializeDB() (*DbConnection, error) {
conn, err := sql.Open("sqlite3", "./test.db")
log.Print("Opening database")
conn, err := sql.Open("sqlite3", "file:ipreg.db?cache=shared&mode=rwc")
if err != nil {
return nil, err
}
Expand All @@ -36,31 +37,27 @@ email text, note text);

func (conn *DbConnection) SetRegistration(address string,
record RegistrationRecord) bool {
rows, err := conn.Query("select * from RegisteredUsers where address = ?", address)
rows, err := conn.Query("select * from RegisteredUsers where address = ?;", address)
if err != nil {
return false
}
defer rows.Close()
if rows.Next() {
rows.Close()
return doPreparedStatement(conn,
"update RegisteredUsers set name=?, email=?, note=? where address=?",
"update RegisteredUsers set name=?, email=?, note=? where address=?;",
record.Name, record.Email, record.Note, address)
} else {
rows.Close()
return doPreparedStatement(conn,
"insert into RegisteredUsers values (?, ?, ?, ?)",
"insert into RegisteredUsers values (?, ?, ?, ?);",
address, record.Name, record.Email, record.Note)
}
}

func doPreparedStatement(conn *DbConnection, statement string,
arguments ...interface{}) bool {

stmt, err := conn.Prepare(statement)
if err != nil {
log.Printf("Failed create statement %q", err.Error())
return false
}
_, err = stmt.Exec(arguments...)
_, err := conn.Exec(statement, arguments...)
if err != nil {
log.Printf("Failed execute statement %q", err.Error())
return false
Expand Down

0 comments on commit e88336b

Please sign in to comment.