github.com/dolthub/go-mysql-server@v0.18.0/driver/_example/main.go (about)

     1  // Copyright 2020-2023 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"database/sql"
    19  	"fmt"
    20  	"log"
    21  	"time"
    22  
    23  	"github.com/dolthub/go-mysql-server/driver"
    24  )
    25  
    26  func main() {
    27  	sql.Register("sqle", driver.New(factory{}, nil))
    28  
    29  	db, err := sql.Open("sqle", "")
    30  	must(err)
    31  
    32  	_, err = db.Exec("USE mydb")
    33  	must(err)
    34  
    35  	rows, err := db.Query("SELECT * FROM mytable")
    36  	must(err)
    37  	dump(rows)
    38  }
    39  
    40  func must(err error) {
    41  	if err != nil {
    42  		log.Fatal(err)
    43  	}
    44  }
    45  
    46  func dump(rows *sql.Rows) {
    47  	var name, email string
    48  	var phoneNumbers string
    49  	var created_at time.Time
    50  
    51  	for rows.Next() {
    52  		must(rows.Scan(&name, &email, &phoneNumbers, &created_at))
    53  		fmt.Println(name, email, phoneNumbers, created_at)
    54  	}
    55  }