Connect to db (not working yet)
This commit is contained in:
parent
9d3cec7cf6
commit
95c7bfe933
2
makefile
2
makefile
@ -40,7 +40,7 @@ api: api-start
|
||||
api-init:
|
||||
cd src/api; go build -o ../../bin/engine_x_mgmt
|
||||
api-start:
|
||||
./bin/engine_x_mgmt &
|
||||
cd bin; ./engine_x_mgmt
|
||||
api-stop:
|
||||
|
||||
# Setup UI
|
||||
|
40
src/api/helper/11_QueryDB.go
Normal file
40
src/api/helper/11_QueryDB.go
Normal file
@ -0,0 +1,40 @@
|
||||
package helper
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
func QueryString(queryStr string) []byte {
|
||||
db_string := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", Db_User, Db_Pass, Db_Host, Db_Port, Db_Default_Db)
|
||||
conn, err := sql.Open("mysql", db_string)
|
||||
if err != nil {
|
||||
LogErrorWarning(err)
|
||||
return []byte("Error while connecting to database")
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
qdata, err := conn.Query(queryStr)
|
||||
LogErrorWarning(err)
|
||||
defer qdata.Close()
|
||||
|
||||
kvp := make(map[string]string)
|
||||
|
||||
for qdata.Next() {
|
||||
var key string
|
||||
var value string
|
||||
|
||||
err = qdata.Scan(&key, &value)
|
||||
LogErrorWarning(err)
|
||||
kvp[key] = value
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(kvp)
|
||||
if err != nil {
|
||||
return []byte("Error while generating json")
|
||||
}
|
||||
return jsonData
|
||||
}
|
Loading…
Reference in New Issue
Block a user