Skip to content

Latest commit

 

History

History
438 lines (323 loc) · 8.4 KB

API_DOCUMENTATION.md

File metadata and controls

438 lines (323 loc) · 8.4 KB

go-ibm_db API Documentation

Database APIs

APIs for creating and dropping Database using Go application

Database APIs

  1. .Open(drivername,ConnectionString)
  2. .Prepare(sqlquery)
  3. .Query(sqlquery)
  4. .Exec(sqlquery)
  5. .Begin()
  6. .Close()
  7. .Commit()
  8. .Rollback()
  9. .QueryRow(sqlquery)
  10. .Columns()
  11. .Next()
  12. .Scan(options)

1) .Open(drivername,ConnectionString)

open a connection to database

  • connectionString - The connection string for your database.
  • For distributed platforms, the connection string is typically defined as: DATABASE=dbname;HOSTNAME=hostname;PORT=port;PROTOCOL=TCPIP;UID=username;PWD=passwd
var connStr = flag.String("conn", "HOSTNAME=hostname;PORT=port;DATABASE=dbname;UID=uid;PWD=Pass", "connection string")
func dboper() error {
    fmt.Println("connecting to driver");
    db, err := sql.Open("drivername",*connStr);
    if err != nil {
        return err;
    }

    defer db.Close()
}

2) .Prepare(sqlquery)

Prepare a statement for execution

  • sql - SQL string to prepare

Returns a ‘statement’ object

func oper() error {
    fmt.Println("connecting to go-ibm_db");
    db, err:=sql.Open("go-imb_db",*connStr);
    if err != nil {
        return err;
    }
    
    defer db.Close()

    st, err := db.Prepare("select * from ak")
    if err !=nil {
        return err
    }

    rows,err := st.Query()
    if err != nil {
        return err
    }

    defer rows.Close()
}

3) .Query(sqlquery)

Issue a SQL query to the database

If the query is executed then it will return the rows or it will return error

func oper() error {
    fmt.Println("connecting to go-ibm_db");
    db, err := sql.Open("go-ibm_db",*connStr);
    if err != nil {
        return err;
    }

    defer db.Close()
    
    rows,err := db.Query(“select * from ak”)
    if err != nil {
        return err
    }
    
    defer rows.Close()
}

4) .Exec(sqlquery)

Execute a prepared statement.

Only DML commands are performed. No data is returned back.

func oper() error {
    fmt.Println("connecting to go-ibm_db");
    db, err := sql.Open("go-ibm_db",*connStr);
    if err != nil {
        return err;
    }

    defer db.Close()

    _,err = db.Exec("create table ghh(a int, b float, c double,  d char, e varchar(30))")
    if err != nil {
        return err
    }
}

5) .Begin()

Begin a transaction.

func oper() error {
    fmt.Println("connecting to go-ibm_db");
    db, err := sql.Open("go-ibm_db",*connStr);
    if err != nil {
        return err;
    }

    defer db.Close()
    
    bg, err := db.Begin()
    if err !=nil {
        return err
    }

    return nil
}

6) .Close()

Close the currently opened database.

func dboper() error {
    fmt.Println("connecting to go-ibm_db");
    db, err := sql.Open("go-ibm_db",*connStr);
    if err != nil {
        return err;
    }
    
    defer db.Close()
}

7) .Commit()

Commit a transaction.

func oper() error{
    fmt.Println("connecting to go-ibm_db");
    db, err := sql.Open("go-ibm_db",*connStr);
    if err != nil{
        return err;
    }
    

    defer db.Close()

    bg, err := db.Begin()
    if err != nil {
        return err
    }
    
    _,err=bg.Exec("create table ghh(a int,b float,c double,d char,e varchar(30))")
    if err != nil {
        return err
    }
    
    err = bg.Commit()
    if err != nil {
        return err
    }

    return nil
}

8) .Rollback()

Rollback a transaction.

func oper() error{
    fmt.Println("connecting to go-ibm_db");
    db, err := sql.Open("go-ibm_db",*connStr);
    if err != nil {
        return err;
    }

    defer db.Close()
    bg, err := db.Begin()
    if err !=nil {
        return err
    }

    _,err=bg.Exec("create table ghh(a int,b float,c double,d char,e varchar(30))")
    if err != nil {
        return err
    }

    err = bg.Rollback()
    if err != nil {
        return err
    }
    
    return nil
}

9) .QueryRow(sqlquery)

QueryRow executes a query that is expected to return at most one row. If there are more rows then it will scan first and discards the rest.

func oper()  error {
    id := 123
    var username string
    err := db.QueryRow("SELECT name FROM ak WHERE id=?", id).Scan(&username)
    if err != nil {
        return err
    }

    fmt.Printf("Username is %s\n", username)
    return nil
}

10) .Columns()

Returns the column names.

Returns error if the rows are closed.

func oper() error {
    fmt.Println("connecting to databse");
    db, err := sql.Open("go-ibm_db",*connStr);
    if err != nil {
        return err;
    }

    defer db.Close()

    st, err := db.Prepare("select * from ak")
    if err !=nil {
    return err
    }
    
    rows,err :=st.Query()
    if err != nil {
        return err
    }
    
    defer rows.Close()
    name11 := make([]string,1)
    name11, err = rows.Columns()
    fmt.Printf("%v",name11);
    return nil
}

11) .Next()

Prepares the next result row for reading with the scan api.

func oper() error {
    fmt.Println("connecting to database");
    db, err:=sql.Open("go-ibm_db",*connStr);
    if err != nil {
        return err;
    }

    defer db.Close()
    rows,err := db.Query()
    if err != nil {
        return err
    }
    
    defer rows.Close()
    for rows.Next() {
        var t string
        var x string
        err = rows.Scan(&t, &x)
        if err != nil {
            return err
        }

        fmt.Printf("%v %v\n",t,x)
    }
    
    return nil
}

12) .Scan(options)

copies the columns in the current row into the values pointed.

func oper() error {
    fmt.Println("connecting to database");
    db, err := sql.Open("go-ibm_db", *connStr);
    if err != nil {
        return err;
    }

    defer db.Close()

    rows,err := db.Query()
    if err != nil {
        return err
    }

    defer rows.Close()
    for rows.Next() {
        var t string
        var x string
        err = rows.Scan(&t, &x)
        if err != nil {
            return err
        }

        fmt.Printf("%v %v\n",t,x)
    }
    return nil
}

Create and Drop Database APIs

.CreateDb(dbName, connectionString, options...)

To create a database (dbName) through Go application.

  • dbName - The database name.
  • connectionString - The connection string for your database instance.
  • options - OPTIONAL - string type
    • codeset - Database code set information.
    • mode - Database logging mode (applicable only to "IDS data servers").
package main

import(
    "github.com/ibmdb/go_ibm_db"
    "database/sql"
    "fmt"
)

func main(){
    var conStr = "HOSTNAME=hostname;PORT=port;PROTOCOL=TCPIP;UID=username;PWD=password";
    var dbName = "Goo";
    res, err := go_ibm_db.CreateDb(dbName, conStr)
    // CreateDb with options
    //go_ibm_db.CreateDb(dbName, conStr, "codeset=UTF-8","mode=value")
	if err != nil {
		fmt.Println("Error while creating database ", err)
    }
    if res {
        fmt.Println("Database created successfully.")
        conStr = conStr + ";" + "DATABASE=" + dbName
        db,err := sql.Open("go_ibm_db",conStr)
        if err = db.Ping(); err != nil {
		    fmt.Println("Ping Error: ", err)
	    }
	    defer db.Close()
	    fmt.Println("Connected: ok")
    }
}

.DropDb(dbName, connectionString)

To drop a database (dbName) through Go application.

  • dbName - The database name.
  • connectionString - The connection string for your database instance.
package main

import(
    "github.com/ibmdb/go_ibm_db"
    "fmt"
)

func main(){
    var conStr = "HOSTNAME=hostname;PORT=port;PROTOCOL=TCPIP;UID=username;PWD=password";
    var dbName = "Goo";
	res, err := go_ibm_db.DropDb(dbName, conStr)
	if err != nil {
		fmt.Println("Error while dropping database ",err)
    }
    if res {
        fmt.Println("Database dropped successfully.")
    }
}