2.5 設定目前的綱要

工作階段的預設綱要可以使用 類似 URI 的連線字串或鍵值對中的 schema 屬性在開啟連線工作階段時指定。Session 類別的 getDefaultSchema() 方法會傳回 Session 的預設綱要。

如果連線時未選取預設綱要,則可以使用 Session 類別的 setCurrentSchema() 函數來設定目前的綱要。

var mysqlx = require('mysqlx');

// Direct connect with no client-side default schema specified
var mySession = mysqlx.getSession('user:password@localhost');
mySession.setCurrentSchema("test");

請注意,setCurrentSchema() 不會變更工作階段的預設綱要,該綱要在整個工作階段中保持不變,或者如果未在連線時設定,則保持 null。可以使用 getCurrentSchema() 方法傳回由 setCurrentSchema() 設定的綱要。

設定目前綱要的另一種方法是使用 Session 類別的 sql() 方法和 USE db_name 陳述式。