APIs for creating and dropping Database using Go application
Database APIs
- .Open(drivername,ConnectionString)
- .Prepare(sqlquery)
- .Query(sqlquery)
- .Exec(sqlquery)
- .Begin()
- .Close()
- .Commit()
- .Rollback()
- .QueryRow(sqlquery)
- .Columns()
- .Next()
- .Scan(options)
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()
}
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()
}
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()
}
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
}
}
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
}
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()
}
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
}
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
}
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
}
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
}
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
}
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
}
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")
}
}
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.")
}
}