go-sqlcipher/_example/encrypto/encrypto.go

55 lines
1.0 KiB
Go
Raw Normal View History

2015-04-01 17:48:04 +03:00
/*
* @Author: xeodou
* @Date: 2015
* @Last Modified by: xeodou
*/
package main
import (
"database/sql"
"fmt"
2018-05-23 18:43:09 +03:00
2015-04-01 17:48:04 +03:00
_ "github.com/xeodou/go-sqlcipher"
)
func main() {
db, err := sql.Open("sqlite3", "users.db?key=123456")
2015-04-01 17:48:04 +03:00
if err != nil {
fmt.Println(err)
}
defer db.Close()
p := "PRAGMA key = '123456';"
_, err = db.Exec(p)
if err != nil {
fmt.Println(err)
}
2018-05-23 18:43:09 +03:00
2015-04-01 17:48:04 +03:00
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()
}