Skip to content

Commit 1e44b82

Browse files
authored
docs: add save api and insert with dict sections (#169)
1 parent 08e87a0 commit 1e44b82

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

src/ai/guides/tables.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,88 @@ To insert multiple records:
129129
(3, 'LlamaIndex is a Python library for building AI-powered applications', '[0.7, 0.8, 0.9]', '{"category": "rag"}');
130130
```
131131

132+
### With Dict
133+
134+
You can also use `dict` to represent records and insert them into the table. This approach is more flexible and doesn't require to use a `TableModel` to insert data.
135+
136+
To insert a single record:
137+
138+
=== "Python"
139+
140+
Use the `table.insert()` method with a dictionary to insert a single record into the table.
141+
142+
```python
143+
table.insert({
144+
"id": 1,
145+
"content": "TiDB is a distributed SQL database",
146+
"embedding": [0.1, 0.2, 0.3],
147+
"meta": {"category": "database"},
148+
})
149+
```
150+
151+
=== "SQL"
152+
153+
Use the `INSERT INTO` statement to insert a single record into the table.
154+
155+
```sql
156+
INSERT INTO items(id, content, embedding, meta)
157+
VALUES (1, 'TiDB is a distributed SQL database', '[0.1, 0.2, 0.3]', '{"category": "database"}');
158+
```
159+
160+
## Save data to a table
161+
162+
The `save` method provides a convenient way to insert or update a single record. If a record with the specified primary key does not exist, it creates a new record. If the record already exists, it overwrites the entire record.
163+
164+
!!! note
165+
166+
If a record ID already exists, `table.save()` function overwrites the entire record. To change only part of a record, use `table.update()`.
167+
168+
=== "Python"
169+
170+
Use the `table.save()` method to save a single record to the table.
171+
172+
**Example: Save a new record**
173+
174+
```python
175+
saved_record = table.save(
176+
Item(
177+
id=4,
178+
content="Vector databases enable AI applications",
179+
embedding=[1.0, 1.1, 1.2],
180+
meta={"category": "vector-db"},
181+
)
182+
)
183+
```
184+
185+
**Example: Save an existing record (overwrites the entire record)**
186+
187+
```python
188+
# This overwrites the entire record with id=1
189+
updated_record = table.save(
190+
Item(
191+
id=1, # Existing ID
192+
content="Updated content for TiDB",
193+
embedding=[0.2, 0.3, 0.4],
194+
meta={"category": "updated"},
195+
)
196+
)
197+
```
198+
199+
=== "SQL"
200+
201+
Use the `INSERT ... ON DUPLICATE KEY UPDATE` statement to save a record.
202+
203+
**Example: Save a new record or update if it exists**
204+
205+
```sql
206+
INSERT INTO items(id, content, embedding, meta)
207+
VALUES (4, 'Vector databases enable AI applications', '[1.0, 1.1, 1.2]', '{"category": "vector-db"}')
208+
ON DUPLICATE KEY UPDATE
209+
content = VALUES(content),
210+
embedding = VALUES(embedding),
211+
meta = VALUES(meta);
212+
```
213+
132214
## Query data from a table
133215

134216
To fetch records from a table:

0 commit comments

Comments
 (0)