数据库产品通常会提供一个命令行客户端工具。
数据库厂商 | 命令行客户端 |
---|---|
Oracle | sqlplus |
MySQL | mysql |
Mariadb | mysql |
GBase 8s | dbaccess |
Kingbase ES | ksql |
DM8 | disql |
dbaccess提供了一个基于控制台的菜单,用户可以使用方向键或快捷键,选择和执行相应的功能。
直接运行dbaccess命令,即可进入菜单的交互模式。
DEMO
注: 可选择和执行的文件,在启动dbaccess目录中,且扩展名为.sql。
dbaccess提供了一个类似mysql或sqlplus的客户端交互模式,用户输入要执行的指令并回车,dbaccess执行用户输入的指令,并返回执行结果。
可以通过为dbaccess提供两个参数,进入指令交互模式。
dbaccess
param1:提供数据库名称或-,当该参数为-时,表示未选择默认的数据库,后续可在dbaccess中,使用database 指定当前数据库。
param2:固定为-,表示dbaccess的输入为标准输入STDIN。
DEMO
[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.
>
dbaccess可以接收STDIN中的内容,做为dbaccess需要执行的指令。
echo "sql_code" | dbaccess
DEMO
[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.
[gbasedbt@train ~]$
dbaccess
DEMO
[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.
[gbasedbt@train ~]$
dbaccess <
EOF
DEMO
[gbasedbt@train ~]$ cat demo.sh
#!/bin/bash
dbaccess <
默认情况下,dbaccess会尽可能的显示浮点数的小数位数。通过设置该环境变量,控制dbaccess在输出小数信息时,尽量只显示指定位数的小数。
export DBFLTMASK=3
DEMO
[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@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.
[gbasedbt@train ~]$
设置dbaccess中使用的操作系统文本编辑器,默认为vi。
export DBEDIT=vi
GBase 8s中的money数据类型的显示。默认情况下,会根据操作系统的本地化设置,显示货币信息。可以通过该环境变量,设置需要显示的信息。
export DBMONEY="[front][.|,][back]"
DEMO
[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.
[gbasedbt@train ~]$
GBase 8s中的date数据类型,默认格式为月/日/年(英文),可以设置GL_DATE环境变量,改变DATE数据的显示格式。
export GL_DATE="%iY-%m-%d"
DEMO
[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 ~]$
控制dbaccess在执行SQL时,是否显示SQL的执行时间。
export DBACCESS_SHOW_TIME=1
DEMO
[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 ~]$
格式化符 | 说明 |
---|---|
%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) |