banner
biuaxia

biuaxia

"万物皆有裂痕,那是光进来的地方。"
github
bilibili
tg_channel

Developing DM database applications with Golang

title: Developing Dm Database Applications with Golang
date: 2022-02-21 14:23:13
toc: true
index_img: https://b3logfile.com/file/2022/02/23a5526744174569ada00a90ca3167f3.png
category:

  • Golang
    tags:
  • Golang
  • Development
  • Dm
  • Database
  • Applications

Go Database Interface#

Go Language Standard Library database/sql provides a set of standard interfaces for database operations. The DM database provides a Go language interface for DM database operations by implementing the interfaces of the database/sql package based on GO 1.13 version.

Development Environment Setup#

SoftwareVersion
DM DatabaseDM 8.0 and above
GOGo 1.13 Windows-amd 64
LiteIDELiteIDE x20.1, Go language IDE
GITGit-2.29.2-64-bit

Installing DM Database#

Please refer to the DM Database Quick Start Guide.

During the database installation process, please select "BOOKSHOP" and "DMHR" sample databases as the database simulation environment, as shown in the following image:

DMHR Sample Database

Installing Go Environment#

You can download the Go 1.13 Windows-amd 64 installer from the official website, or choose the unzip version. The following image shows the installer package:

Installer Image

Installing LiteIDE Go Language IDE#

LiteIDE is a lightweight Go language IDE. You can download the LiteIDE installer package.

Downloading and Installing Git#

Please download and install Git to obtain the driver program. The installation process of Git is simple. Just click "Next" continuously. After the installation is complete, right-click on the desktop and select the "Git Bash Here" button. If the following image appears, it means that the installation is successful.

Git Installation Successful

Configuring DM Driver Package#

  1. Installing DM Driver Package

    Place the provided DM driver package in the src directory of GOPATH. The driver package is located at dmdbms/drivers/go/dm-go-driver.zip. Extract it to the src directory of the GOPATH installation path, as shown in the following image:

    Installing DM Driver Package

  2. Installing Dependency Packages

    There are two required Go dependency packages: text and snappy. Clone the dependency packages from Git to your local machine. Right-click on the desktop and select "Git Bash Here" to open the Git command line window. Download the text and snappy dependencies one by one.

    git clone https://github.com/golang/text.git  D:/Go/src/golang.org/x/text
    git clone https://github.com/golang/snappy  D:/Go/src/github.com/golang/snappy
    

    Note: The example installation path is D:/Go. Please replace it with your own installation path.

    Cloning Dependency Packages

    Cloning Dependency Packages

Binding Variables and Large Field Operation Examples#

Open the LiteIDE editor, create a new directory, and create a TestDemo.go file in the directory.

/* This example implements basic operations such as inserting data, modifying data, deleting data, and querying data. */
package main

// Import relevant packages
import (
	"database/sql"
	"dm"
	"fmt"
	"io/ioutil"
	"time"
)

var db *sql.DB
var err error

func main() {
	driverName := "dm"
	dataSourceName := "dm://SYSDBA:SYSDBA@localhost:5236"
	if db, err = connect(driverName, dataSourceName); err != nil {
		fmt.Println(err)
		return
	}
	if err = insertTable(); err != nil {
		fmt.Println(err)
		return
	}
	if err = updateTable(); err != nil {
		fmt.Println(err)
		return
	}
	if err = queryTable(); err != nil {
		fmt.Println(err)
		return
	}
	if err = deleteTable(); err != nil {
		fmt.Println(err)
		return
	}
	if err = disconnect(); err != nil {
		fmt.Println(err)
		return
	}
}

/* Create a database connection */
func connect(driverName string, dataSourceName string) (*sql.DB, error) {
	var db *sql.DB
	var err error
	if db, err = sql.Open(driverName, dataSourceName); err != nil {
		return nil, err
	}
	if err = db.Ping(); err != nil {
		return nil, err
	}
	fmt.Printf("connect to \"%s\" succeed.\n", dataSourceName)
	return db, nil
}

/* Insert data into the product information table */
func insertTable() error {
	var inFileName = "D:\\三国演义.jpg"
	var sql = `INSERT INTO production.product(name,author,publisher,publishtime,
product_subcategoryid,productno,satetystocklevel,originalprice,nowprice,discount,
description,photo,type,papertotal,wordtotal,sellstarttime,sellendtime)
VALUES(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12,:13,:14,:15,:16,:17);`
	data, err := ioutil.ReadFile(inFileName)
	if err != nil {
		return err
	}
	t1, _ := time.Parse("2006-Jan-02", "2005-Apr-01")
	t2, _ := time.Parse("2006-Jan-02", "2006-Mar-20")
	t3, _ := time.Parse("2006-Jan-02", "1900-Jan-01")
	_, err = db.Exec(sql, "三国演义", "罗贯中", "中华书局", t1, 4, "9787101046126", 10, 19.0000, 15.2000,
		8.0,
		"《三国演义》是中国第一部长篇章回体小说,中国小说由短篇发展至长篇的原因与说书有关。",
		data, "25", 943, 93000, t2, t3)
	if err != nil {
		return err
	}
	fmt.Println("insertTable succeed")
	return nil
}

/* Modify data in the product information table */
func updateTable() error {
	var sql = "UPDATE production.product SET name = :name WHERE productid = 11;"
	if _, err := db.Exec(sql, "三国演义(上)"); err != nil {
		return err
	}
	fmt.Println("updateTable succeed")
	return nil
}

/* Query the product information table */
func queryTable() error {
	var productid int
	var name string
	var author string
	var description dm.DmClob
	var photo dm.DmBlob
	var sql = "SELECT productid,name,author,description,photo FROM production.product WHERE productid=11"
	rows, err := db.Query(sql)
	if err != nil {
		return err
	}
	defer rows.Close()
	fmt.Println("queryTable results:")
	for rows.Next() {
		if err = rows.Scan(&productid, &name, &author, &description, &photo); err != nil {
			return err
		}
		blobLen, _ := photo.GetLength()
		fmt.Printf("%v %v %v %v %v\n", productid, name, author, description, blobLen)
	}
	return nil
}

/* Delete data from the product information table */
func deleteTable() error {
	var sql = "DELETE FROM production.product WHERE productid = 12;"
	if _, err := db.Exec(sql); err != nil {
		return err
	}
	fmt.Println("deleteTable succeed")
	return nil
}

/* Close the database connection */
func disconnect() error {
	if err := db.Close(); err != nil {
		fmt.Printf("db close failed: %s.\n", err)
		return err
	}
	fmt.Println("disconnect succeed")
	return nil
}

Note: Prepare the large field image to be inserted in D:\\三国演义.jpg in advance.

Click the "build and run" button to run the code. The screenshot after running is as follows:

Screenshot after Running

The database screenshot after running is shown in the following image:

Database Screenshot after Running

References#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.