本文整理汇总了Golang中database/sql.Open函数的典型用法代码### 示例。如果您正苦于以下问题:Golang Open函数的具体用法?Golang Open怎么用?Golang Open使用的例子?那么恭喜您, 这里精选的函数代码### 示例或许可以为您提供帮助。

在下文中一共展示了Open函数的20个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码### 示例。

示例1: CreateAndConnect

// CreateAndConnect connects and creates the requested DB (dropping
// if exists) then returns a new connection to the created DB.
func CreateAndConnect(pgURL url.URL, name string) (*gosql.DB, error) {
    {
        pgURL.Path = ""
        db, err := gosql.Open("postgres", pgURL.String())
        if err != nil {
            return nil, err
        }
        defer db.Close()

        if _, err := db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", name)); err != nil {
            return nil, err
        }

        if _, err := db.Exec(fmt.Sprintf(`CREATE DATABASE %s`, name)); err != nil {
            return nil, err
        }
    }

    pgURL.Path = name

    db, err := gosql.Open("postgres", pgURL.String())
    if err != nil {
        return nil, err
    }
    return db, nil
}

开发者ID:CubeLite,项目名称:cockroach,代码行数:28,代码来源:setup.go

示例2: GetUser

func GetUser(username string) (u User, err error) {
    db, err := sql.Open("mysql", DBLoginString)
    checkErr(err)
    defer db.Close()
    stmt, err := db.Prepare("SELECT * FROM userinfo WHERE username=?")
    checkErr(err)
    rows, err := stmt.Query(username)
    if !rows.Next() {
        err = errors.New("Not found")
        return
    }
    var disposable string
    err = rows.Scan(&u.Email, &disposable, &u.Created, &u.Username,
        &u.Score)
    if err != nil {
        return
    }
    db.Close()
    scores, uids := GetSuccesfulAttempts(u.Email)
    db, _ = sql.Open("mysql", DBLoginString)
    u.Finished = make(map[string]Challenge_link)
    for i := 0; i < len(uids); i++ {
        stmt, err = db.Prepare("SELECT Title FROM challenges WHERE " +
            "UID=?")
        rows, err = stmt.Query(uids[i])
        checkErr(err)
        rows.Next()
        var title string
        rows.Scan(&title)
        u.Finished[title] = Challenge_link{Title: title, Score: scores[i],
            UID: uids[i]}
    }
    return
}

开发者ID:jenarvaezg,项目名称:ctff,代码行数:34,代码来源:dboperations.go

示例3: NewDB

// NewDB reinitializes the mysql database and returns a connection to the db.
func (c *SQLContainer) NewDB() (*sql.DB, error) {
    // if a connection is open, close it
    if c.conn != nil {
        c.conn.Close()
        c.conn = nil
    }
    db, err := sql.Open("mysql", c.rootStr)
    if err != nil {
        return nil, fmt.Errorf("can't connect to database: %v", err)
    }
    tx, err := db.Begin()
    if err != nil {
        return nil, fmt.Errorf("could not begin tx: %v", err)
    }
    defer tx.Rollback()
    if err = DropTables(tx); err != nil {
        return nil, fmt.Errorf("could not drop tables: %v", err)
    }
    if err = InitTables(tx, "mysql"); err != nil {
        return nil, fmt.Errorf("could not drop tables: %v", err)
    }
    db.Close()
    conn, err := sql.Open("mysql", c.connStr)
    if err != nil {
        return nil, err
    }
    c.conn = conn
    return conn, nil
}

开发者ID:yhat,项目名称:workload-simulator,代码行数:30,代码来源:main_test.go

示例4: makeTestDB

func makeTestDB(t testing.TB, ddls ...string) *DB {
    const dbName = "squalor_test"

    // We need to create an initial database connection for the purpose
    // of creating our testing database.
    tmpDB, err := sql.Open("mysql", makeTestDSN(""))
    if err != nil {
        t.Fatal(err)
    }
    defer tmpDB.Close()
    tmpDB.SetMaxOpenConns(1)

    if _, err := tmpDB.Exec("SET sql_notes=0"); err != nil {
        t.Fatal(err)
    }
    if _, err := tmpDB.Exec("DROP DATABASE IF EXISTS " + dbName); err != nil {
        t.Fatal(err)
    }
    if _, err := tmpDB.Exec("CREATE DATABASE IF NOT EXISTS " + dbName); err != nil {
        t.Fatal(err)
    }

    // Now create our actual database connection.
    db, err := sql.Open("mysql", makeTestDSN(dbName))
    if err != nil {
        t.Fatal(err)
    }
    for _, ddl := range ddls {
        if _, err := db.Exec(ddl); err != nil {
            t.Fatal(err)
        }
    }
    return NewDB(db)
}

开发者ID:pascallouisperez,项目名称:squalor,代码行数:34,代码来源:base_test.go

示例5: openDB

func openDB(db_url_s string) (db *sql.DB) {
    db_url, err := url.Parse(db_url_s)
    if err != nil {
        log.Fatalln("Failed to parse", db_url_s)
    }

    if db_url.Scheme == "sqlite" {
        db, err = sql.Open("sqlite3", "triton-s3.db")
        if err != nil {
            log.Fatalln("Failed to open db", err)
        }

        // sqlite doesn't so much like concurrency
        db.SetMaxOpenConns(1)
        return
    } else if db_url.Scheme == "postgres" {
        db, err = sql.Open("postgres", db_url_s)
        if err != nil {
            log.Fatalln("Failed to open db", err)
        }
        return
    } else {
        log.Fatalln("Unknown db scheme", db_url.Scheme)
        return
    }
}

开发者ID:postmates,项目名称:go-triton,代码行数:26,代码来源:triton.go

最后编辑: kuteng  文档更新时间: 2021-08-23 19:14   作者:kuteng