-
Notifications
You must be signed in to change notification settings - Fork 0
Tables EN
简体中文 | English
Define the table in the script that creates the connection. The plugin uses this definition to build statements and name the keys in query results.
register a database table "users":
id: bigint, primary key, auto increment, not null
name: string(64), not null
age: int, nullable
The body is one column per line, in the exact form
name: type[(size)][, primary key][, auto increment][, not null]
-
The name may contain letters, digits, marks and underscores, and may not start with a digit. The
same rule holds for the table name after
register a database table, and it is checked when the section runs:"my table"or"2fa_codes"is refused withInvalid table name '…'inlast database error, quoted or not. A column name is checked while the script is parsed, so it fails earlier. Names are used as written, so keep one spelling: on a MySQL server running on Linux, table names are case-sensitive. -
The type is one of the names in Types. An unknown one is refused when the section runs,
with a message saying the connected database does not support it; it lands in
last database errorright after the section. -
The size in brackets is for
string, and must be greater than zero. Leaving it off gives that type its default of 255. Writing one for another type is refused by PostgreSQL, whereuuidandlocationareBYTEAand take no size, and accepted by MySQL and"JDBC", where it becomes the width of the column:location(16)is smaller than a serialized location, so every write to it fails with a data-too-long error.uuidis 16 bytes andlocation2048 whatever the brackets say, so leave them tostring. -
The modifiers are separated from the type and from each other by commas. They are
primary key,auto increment,not nullandnullable. A column is nullable unless it saysnot null, and saying both is an error.
Columns are direct lines. A nested block is refused, and so are duplicate column names, more than one
primary key, and auto increment on a column that is not one.
primary key marks the column the by id operations use, and pagination needs one:
select entity from table "users" by id {_id} and store the result in {_user::*}
update one entity in table "users" by id {_id} and wait
delete one entity from table "users" by id {_id} and wait
select page 2 with size 20 from table "users" and store the results in {_page::*}
auto increment lets the database assign the value. The plugin does not hand the generated id back to
the script, so a script that needs to know it should write its own value and use upsert, or find the
row again by another column; see Cookbook.
On SQL backends, registration runs CREATE TABLE IF NOT EXISTS and waits for completion.
It does not drop, alter, or inspect existing table structures.
Existing tables are left unchanged. Adding a column to the script and reloading updates the plugin's
definition, not the database schema. Operations that reference the missing column fail at runtime;
other operations continue to work. Registration produces no warning because it has succeeded.
To change the schema, run ALTER TABLE yourself. In a development database, you can also drop the
table and let the plugin recreate it.
Registrations belong to a connection. Registering "users" again on the same connection fails with
Table 'users' is already registered.. This can happen when two scripts register the same table on a
shared connection, or when a script is reloaded without recreating its connection.
The following example avoids duplicate registration by creating a fresh connection each time:
on load:
create a connection to database "MySQL" with properties:
url: "jdbc:mysql://localhost:3306/mydb"
username: "root"
password: "123456"
register a database table "users":
id: bigint, primary key, auto increment, not null
name: string(64), not null
create a connection always creates a fresh connection with no registered tables. A script that
connects before registering can therefore be reloaded: it replaces the connection rather than
registering on the old one. Only duplicate registration on the same connection is rejected.
See Connections.
register a database table has no and wait: it always waits, and a failure is exposed as
last database error right after it:
register a database table "users":
id: bigint, primary key, auto increment, not null
name: string(64), not null
if last database error is set:
send "Table registration failed: %last database error%" to console
An NBT column is refused here on a server without SkBee, rather than failing later on the first row that touches it; see Types.
skript-orm
参考
实用指南
skript-orm (English)
Reference
- Connections
- Tables
- Writing rows
- Reading rows
- Updating and deleting
- Affected rows
- Errors and waiting
- Transactions
- Types
Practical guides
Wiki 由仓库中的 README 和 docs/ 自动生成。修改文档请到仓库提交,直接编辑 Wiki 的内容会在下次同步时被覆盖。
This wiki is generated from the README files and docs/ in the repository. Please submit changes there; direct wiki edits are overwritten on the next sync.