sql into zip(gob)

package main

import (

    "archive/zip"

    "database/sql"

    "encoding/gob"

    "encoding/json"

    "fmt"

    "io/ioutil"

    "log"

    "os"

    _ "github.com/lib/pq"

)

type Sqlinfo struct {

    Driver        string

    Connectstring string

}

type Pginfo struct {

    Uid int

    Username string

    Department string

    Created string

}

type Pginfolite struct {

    Lite []Pginfo

}

func main() {

    fmt.Println("hello")

    fp, err := ioutil.ReadFile("F:/MyGo/src/sqlinzip/sql.json")

    if err != nil {

        log.Fatal(err)

    }

    var sqlcon Sqlinfo

    if err = json.Unmarshal(fp, &sqlcon); err != nil {

        log.Fatal(err)

    }

    db, err := sql.Open(sqlcon.Driver, sqlcon.Connectstring) //open sql

    if err != nil {

        log.Fatal(err)

    }

    defer db.Close()

    info, err := db.Query("select * from userinfo") //run sql word

    if err != nil {

        log.Fatal(err)

    }

    defer info.Close()

    zipfile, err := os.Create("sql.zip")

    if err != nil {

        log.Fatal(err)

    }

    defer zipfile.Close()

    zipfilewrite := zip.NewWriter(zipfile)

    defer zipfilewrite.Close()

    createzipfile, err := zipfilewrite.Create("sql.gob")

    if err != nil {

        log.Fatal(err)

    }

    cwz := gob.NewEncoder(createzipfile)

    for info.Next() {

        var oneinfo Pginfo

        info.Scan(&oneinfo.Uid, &oneinfo.Username, &oneinfo.Department, &oneinfo.Created)

        //make your sql's infomation into model

        cwz.Encode(oneinfo)

    }

}

你可能感兴趣的:(sql into zip(gob))