Skip to content

Commit

Permalink
Merge pull request #29537 from taosdata/docs/improve-stmt-doc
Browse files Browse the repository at this point in the history
add note for sql using in parameter binding insertion
  • Loading branch information
zitsen authored Jan 13, 2025
2 parents 5087e42 + dc4feb8 commit cfb2919
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
13 changes: 13 additions & 0 deletions docs/en/07-develop/05-stmt.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ When inserting data using parameter binding, it can avoid the resource consumpti

**Tips: It is recommended to use parameter binding for data insertion**

:::note
We only recommend using the following two forms of SQL for parameter binding data insertion:

```sql
a. Subtables already exists:
1. INSERT INTO meters (tbname, ts, current, voltage, phase) VALUES(?, ?, ?, ?, ?)
b. Automatic table creation on insert:
1. INSERT INTO meters (tbname, ts, current, voltage, phase, location, group_id) VALUES(?, ?, ?, ?, ?, ?, ?)
2. INSERT INTO ? USING meters TAGS (?, ?) VALUES (?, ?, ?, ?)
```

:::

Next, we continue to use smart meters as an example to demonstrate the efficient writing functionality of parameter binding with various language connectors:

1. Prepare a parameterized SQL insert statement for inserting data into the supertable `meters`. This statement allows dynamically specifying subtable names, tags, and column values.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package com.taosdata.example;

import com.alibaba.fastjson.JSON;
import com.taosdata.jdbc.AbstractStatement;

import java.sql.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ private void close() {

private void executeQuery(String sql) {
long start = System.currentTimeMillis();
try (Statement statement = connection.createStatement()) {
ResultSet resultSet = statement.executeQuery(sql);
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql)) {

long end = System.currentTimeMillis();
printSql(sql, true, (end - start));
Util.printResult(resultSet);
Expand Down
13 changes: 13 additions & 0 deletions docs/zh/07-develop/05-stmt.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ import TabItem from "@theme/TabItem";

**Tips: 数据写入推荐使用参数绑定方式**

:::note
我们只推荐使用下面两种形式的 SQL 进行参数绑定写入:

```sql
一、确定子表存在:
1. INSERT INTO meters (tbname, ts, current, voltage, phase) VALUES(?, ?, ?, ?, ?)
二、自动建表:
1. INSERT INTO meters (tbname, ts, current, voltage, phase, location, group_id) VALUES(?, ?, ?, ?, ?, ?, ?)
2. INSERT INTO ? USING meters TAGS (?, ?) VALUES (?, ?, ?, ?)
```

:::

下面我们继续以智能电表为例,展示各语言连接器使用参数绑定高效写入的功能:
1. 准备一个参数化的 SQL 插入语句,用于向超级表 `meters` 中插入数据。这个语句允许动态地指定子表名、标签和列值。
2. 循环生成多个子表及其对应的数据行。对于每个子表:
Expand Down

0 comments on commit cfb2919

Please sign in to comment.