1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| package person
import ( "database/sql" "fmt" "log" "os" "path" "runtime" "strings" "time"
_ "github.com/mattn/go-sqlite3" )
type PersonDBStruct struct { PersonIndex int64 `json:"person_index"` PersonID string `json:"person_id"` PersonName string `json:"person_name"` PersonPicture string `json:"person_picture"` CreateTime time.Time `json:"create_time"` }
func NewPersonDB() map[int64]*PersonDBStruct { var dbFile string arch := runtime.GOOS if arch == "windows" { dbFile = "E:\\SQLiteStudio\\data\\persondb-test\\PersonDB2.db" fmt.Println(arch + " " + dbFile) } else { dbFile = "/home/data/sqlite/PersonDB2.db" fmt.Println(arch + " " + dbFile) }
db, err := sql.Open("sqlite3", dbFile) if err != nil { fmt.Println(err) }
rows, err := db.Query("select PersonIndex, PersonID , PersonName , PersonPicture , CreateTime from PersonDB") if err != nil { fmt.Println("dbQuery:", err) }
var ( personIndex int64 personID string PersonName string personPicture string CreateTime int64 )
objMap := make(map[int64]*PersonDBStruct) for rows.Next() { err = rows.Scan(&personIndex, &personID, &PersonName, &personPicture, &CreateTime) if err != nil { log.Fatal(err) }
coreEpoch := time.Date(1970, time.January, 1, 8, 0, 0, 0, time.UTC) timestamp := coreEpoch.Add(time.Duration(CreateTime * int64(time.Second)))
picturePath, pictureFile := path.Split(personPicture) picturePath = strings.Trim(picturePath, "//") personPicture = path.Join(picturePath, pictureFile) personPicture = "/" + personPicture
objMap[personIndex] = &PersonDBStruct{ PersonIndex: personIndex, PersonID: personID, PersonName: PersonName, PersonPicture: personPicture, CreateTime: timestamp, }
} rows.Close() db.Close()
return objMap }
func DeletePersonInfo(t1 time.Time, objMap map[int64]*PersonDBStruct) { fmt.Printf("删除 %v时间之前的数据\n", t1)
var dbFile string arch := runtime.GOOS if arch == "windows" { dbFile = "E:\\SQLiteStudio\\data\\persondb-test\\PersonDB2.db" fmt.Println(arch + " " + dbFile) } else { dbFile = "/home/data/sqlite/PersonDB2.db" fmt.Println(arch + " " + dbFile) }
db, err := sql.Open("sqlite3", dbFile) if err != nil { fmt.Println(err) } for pid, obj := range objMap { t := obj.CreateTime t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local) if t.Before(t1) { stmt, err := db.Prepare("DELETE FROM PersonDB where PersonID=?") if err != nil { log.Fatal("### Delete:", err) } else { log.Printf("delete %s from PersonDB\n", obj.PersonID) }
if pid == (obj.PersonIndex) { _, err = stmt.Exec(obj.PersonID) if err != nil { log.Fatal("### Exec:", err) } err = os.Remove(obj.PersonPicture) if err != nil { log.Println("### delete img", err) } else { fmt.Printf("remove %s OK\n", obj.PersonPicture) } }
} } }
|