1 dbaccess概述
dbaccess 是 GBase 8s 数据库的一个命令行客户端工具,用来与 GBase 8s 数据库服务器进行交互。
dbaccess 是 GBase 8s 数据库自带的工具,只要安装了 GBase 8s 数据库,就可以在命令行启动该工具。
dbaccess 识别用户输入,将用户输入的 SQL 语句打包发送给 GBase 8s 数据库服务器执行,并接收服务器的执行结果,并按用户的要求将执行结果展示给用户。
2 dbaccess工作模式
—2.1 交互模式
——2.1.1 菜单交互模式
dbaccess提供了一个基于控制台的菜单,用户可以使用方向键或快捷键,选择和执行相应的功能。
直接运行dbaccess命令,即可进入菜单的交互模式。
选择/连接数据库实例 编辑/执行SQL代码 选择/执行文件中的SQL代码 注: 可选择和执行的文件,在启动dbaccess目录中,且扩展名为.sql。
——2.1.2 指令交互模式
dbaccess提供了一个类似mysql或sqlplus的客户端交互模式,用户输入要执行的指令并回车,dbaccess执行用户输入的指令,并返回执行结果。
可以通过为dbaccess提供两个参数,进入指令交互模式。
dbaccess <param1> <param2>
param1:提供数据库名称或-,当该参数为-时,表示未选择默认的数据库,后续可在dbaccess中,使用database <db_name>指定当前数据库。
param2:固定为-,表示dbaccess的输入为标准输入STDIN。
[gbasedbt@train ~]$ dbaccess - - Your evaluation license will expire on 2022-06-09 00:00:00 > database gbasedb; Database selected. > drop table if exists t_user; Table dropped. > create table t_user(f_userid int, f_username varchar(50)); Table created. > insert into t_user values(1, 'gbasedbt'); 1 row(s) inserted. > select * from t_user; f_userid f_username 1 gbasedbt 1 row(s) retrieved. >
—2.2 非交互模式
——2.2.1 管道模式
dbaccess可以接收STDIN中的内容,做为dbaccess需要执行的指令。
echo "sql_code" | dbaccess <db_name>
[gbasedbt@train ~]$ echo "select * from t_user;" | dbaccess gbasedb Your evaluation license will expire on 2022-06-09 00:00:00 Database selected. f_userid f_username 1 gbasedbt 1 row(s) retrieved. Database closed.
——2.2.2 脚本模式
可以将SQL代码保存到文件中,将文件做为dbaccess的第二个参数,来执行文件中的SQL代码。
dbaccess <db_name> <sql_file>
[gbasedbt@train ~]$ cat demo.sql database gbasedb; drop table if exists t_user; create table t_user(f_userid int, f_username varchar(50)); insert into t_user values(1, 'sql_in_file'); select * from t_user; [gbasedbt@train ~]$ dbaccess - demo.sql Your evaluation license will expire on 2022-06-09 00:00:00 Database selected. Table dropped. Table created. 1 row(s) inserted. f_userid f_username 1 sql_in_file 1 row(s) retrieved. Database closed.
可以将SQL代码直接写在脚本文件中,采用如下方式,执行SQL代码。
dbaccess <db_name> <<EOF
<sql_code>
EOF
[gbasedbt@train ~]$ cat demo.sh #!/bin/bash dbaccess <<EOF database gbasedb; drop table if exists t_user; create table t_user(f_userid int, f_username varchar(50)); insert into t_user values(1, 'shell'); select * from t_user; EOF [gbasedbt@train ~]$ sh demo.sh Your evaluation license will expire on 2022-06-09 00:00:00 Database selected. Table dropped. Table created. 1 row(s) inserted. f_userid f_username 1 shell 1 row(s) retrieved. Database closed.
3 dbaccess环境变量
—3.1 DBFLTMASK
默认情况下,dbaccess会尽可能的显示浮点数的小数位数。通过设置该环境变量,控制dbaccess在输出小数信息时,尽量只显示指定位数的小数。
export DBFLTMASK=3
[gbasedbt@train ~]$ cat float.sql database gbasedb; drop table if exists t_float; create table t_float(f_num float); insert into t_float values(1234567890.123456); insert into t_float values(12345.6789); [gbasedbt@train ~]$ unset DBFLTMASK [gbasedbt@train ~]$ echo $DBFLTMASK [gbasedbt@gbase01 ~]$ dbaccess - float.sql [gbasedbt@train ~]$ echo "select * from t_float" | dbaccess gbasedb Your evaluation license will expire on 2022-06-09 00:00:00 Database selected. f_num 1234567890.123 12345.67890000 2 row(s) retrieved. Database closed. [gbasedbt@train ~]$ export DBFLTMASK=3 [gbasedbt@train ~]$ echo "select * from t_float" | dbaccess gbasedb Your evaluation license will expire on 2022-06-09 00:00:00 Database selected. f_num 1234567890.123 12345.679 2 row(s) retrieved. Database closed.
—3.2 DBEDIT
设置dbaccess中使用的操作系统文本编辑器,默认为vi。 export DBEDIT=vi
—3.3 DBMONEY
GBase 8s中的money数据类型的显示。默认情况下,会根据操作系统的本地化设置,显示货币信息。可以通过该环境变量,设置需要显示的信息。
export DBMONEY="[front][.|,][back]"
[gbasedbt@train ~]$ cat money.sql database gbasedb; drop table if exists t_money; create table t_money(f_id int, f_num money); insert into t_money values(1, 123.456); insert into t_money values(2, 123456.789); insert into t_money values(3, 1234567890.123456789); [gbasedbt@train ~]$ dbaccess gbasedb money.sql Your evaluation license will expire on 2022-06-09 00:00:00 Database selected. Database closed. Database selected. Elapsed time: 0.001 sec Table dropped. Elapsed time: 0.004 sec Table created. Elapsed time: 0.001 sec 1 row(s) inserted. Elapsed time: 0.001 sec 1 row(s) inserted. Elapsed time: 0.001 sec 1 row(s) inserted. Elapsed time: 0.001 sec Database closed. [gbasedbt@train ~]$ echo "select * from t_money" | dbaccess gbasedb Your evaluation license will expire on 2022-06-09 00:00:00 Database selected. f_id f_num 1 RMB 123.46 2 RMB 123456.79 3 RMB 1234567890.12 3 row(s) retrieved. Elapsed time: 0.002 sec Database closed. [gbasedbt@train ~]$ echo $LANG en_US.UTF-8 [gbasedbt@train ~]$ export DBMONEY="$" [gbasedbt@train ~]$ echo "select * from t_money" | dbaccess gbasedb Your evaluation license will expire on 2022-06-09 00:00:00 Database selected. f_id f_num 1 $123.46 2 $123456.79 3 $1234567890.12 3 row(s) retrieved. Elapsed time: 0.005 sec Database closed. [gbasedbt@train ~]$ export DBMONEY=".$" [gbasedbt@train ~]$ echo "select * from t_money" | dbaccess gbasedb Your evaluation license will expire on 2022-06-09 00:00:00 Database selected. f_id f_num 1 123.46$ 2 123456.79$ 3 1234567890.12$ 3 row(s) retrieved. Elapsed time: 0.002 sec Database closed.
—3.4 GL_DATE
GBase 8s中的date数据类型,默认格式为月/日/年(英文),可以设置GL_DATE环境变量,改变DATE数据的显示格式。
export GL_DATE="%iY-%m-%d"
[gbasedbt@train ~]$ cat date.sql database gbasedb; drop table if exists t_date; create table t_date(f_dt date); insert into t_date values('06/18/2021'); select * from t_date; [gbasedbt@train ~]$ unset GL_DATE [gbasedbt@train ~]$ echo $GL_DATE [gbasedbt@train ~]$ dbaccess gbasedb date.sql Your evaluation license will expire on 2022-06-09 00:00:00 Database selected. Database closed. Database selected. Elapsed time: 0.001 sec Table dropped. Elapsed time: 0.001 sec Table created. Elapsed time: 0.004 sec 1205: Invalid month in date Error in line 7 Near character position 38 f_dt No rows found. Elapsed time: 0.001 sec Database closed. [gbasedbt@train ~]$ echo $LANG en_US.UTF-8 [gbasedbt@train ~]$ export GL_DATE="%m/%d/%iY" [gbasedbt@train ~]$ echo "insert into t_date values('06/18/2021');" | dbaccess gbasedb Your evaluation license will expire on 2022-06-09 00:00:00 Database selected. 1 row(s) inserted. Elapsed time: 0.004 sec Database closed. [gbasedbt@train ~]$ echo "select * from t_date" | dbaccess gbasedb Your evaluation license will expire on 2022-06-09 00:00:00 Database selected. f_dt 06/18/2021 1 row(s) retrieved. Elapsed time: 0.005 sec Database closed. [gbasedbt@train ~]$ export GL_DATE="%iY-%m-%d" [gbasedbt@train ~]$ echo $GL_DATE %iY-%m-%d [gbasedbt@train ~]$ echo "select * from t_date" | dbaccess gbasedb Your evaluation license will expire on 2022-06-09 00:00:00 Database selected. f_dt 2021-06-18 1 row(s) retrieved. Elapsed time: 0.002 sec Database closed. [gbasedbt@train ~]$
—3.5 DBACCESS_SHOW_TIME
控制dbaccess在执行SQL时,是否显示SQL的执行时间。
export DBACCESS_SHOW_TIME=1
[gbasedbt@train ~]$ unset DBACCESS_SHOW_TIME [gbasedbt@train ~]$ echo $DBACCESS_SHOW_TIME [gbasedbt@train ~]$ echo "select * from t_float" | dbaccess gbasedb Your evaluation license will expire on 2022-06-09 00:00:00 Database selected. f_num 1234567890.123 12345.679 2 row(s) retrieved. Database closed. [gbasedbt@train ~]$ export DBACCESS_SHOW_TIME=1 [gbasedbt@train ~]$ echo $DBACCESS_SHOW_TIME 1 [gbasedbt@train ~]$ echo "select * from t_float" | dbaccess gbasedb Your evaluation license will expire on 2022-06-09 00:00:00 Database selected. f_num 1234567890.123 12345.679 2 row(s) retrieved. Elapsed time: 0.001 sec Database closed. [gbasedbt@train ~]$
4 日期格式化参数
格式化符 说明 %a 星期的短格式表示 %A 星期的完整格式表示 %b 月份的短格式表示 %B 月份的完整格式表示 %C 世纪的表示,通常是年值除以100后的整数部分 %d 天(01-31) %D 相当于格式:%m/%d/%y %e 天(1-31) %h 和%b相同 %H 小时(00-24) %iy 两位年(00-99) %iY 四位年(0000-9999) %m 月(01-12) %M 分钟(00-59) %n 换行符 %S 秒(00-59) %t 制表符 %w 星期的数字表示(0-6) %y 两位年(00-99) %Y 四位年(0000-9999)