You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/ai/guides/tables.md
+82Lines changed: 82 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -129,6 +129,88 @@ To insert multiple records:
129
129
(3, 'LlamaIndex is a Python library for building AI-powered applications', '[0.7, 0.8, 0.9]', '{"category": "rag"}');
130
130
```
131
131
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**
0 commit comments