From ccb43651560f84e31ba6c49fc06de2ee62c54baa Mon Sep 17 00:00:00 2001 From: xeodou Date: Wed, 1 Apr 2015 22:48:04 +0800 Subject: [PATCH] Add example from encrypted database. --- _example/encrypto/encrypto.go | 52 +++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 _example/encrypto/encrypto.go diff --git a/_example/encrypto/encrypto.go b/_example/encrypto/encrypto.go new file mode 100644 index 0000000..f3a40f0 --- /dev/null +++ b/_example/encrypto/encrypto.go @@ -0,0 +1,52 @@ +/* +* @Author: xeodou +* @Date: 2015 +* @Last Modified by: xeodou + */ + +package main + +import ( + "database/sql" + "fmt" + _ "github.com/xeodou/go-sqlcipher" +) + +func main() { + db, err := sql.Open("sqlite3", "users.db") + if err != nil { + fmt.Println(err) + } + defer db.Close() + + p := "PRAGMA key = '123456';" + _, err = db.Exec(p) + if err != nil { + fmt.Println(err) + } + c := "CREATE TABLE IF NOT EXISTS `users` (`id` INTEGER PRIMARY KEY, `name` char, `password` chart, UNIQUE(`name`));" + _, err = db.Exec(c) + if err != nil { + fmt.Println(err) + return + } + d := "INSERT INTO `users` (name, password) values('xeodou', 123456);" + _, err = db.Exec(d) + if err != nil { + fmt.Println(err) + } + + e := "select name, password from users where name='xeodou';" + rows, err := db.Query(e) + if err != nil { + fmt.Println(err) + } + defer rows.Close() + for rows.Next() { + var name string + var password string + rows.Scan(&name, &password) + fmt.Print("{\"name\":\"" + name + "\", \"password\": \"" + password + "\"}") + } + rows.Close() +}