🧚 주목! Beekeeper Studio는 빠르고 현대적이며 오픈 소스 데이터베이스 GUI입니다 다운로드
January 25, 2023 작성자: Matthew Rathbone

JSON, or JavaScript Object Notation, is a format for storing and exchanging data. It is a lightweight, text-based, and human-readable format that is easy for machines to parse and generate. In SQLite, JSON can be stored and queried in a few different ways.

Using the JSON1 Extension

One way to store JSON in SQLite is to use the JSON1 extension, which provides functions for encoding and decoding JSON. To use the JSON1 extension, you first need to enable it by running the following command:

SELECT load_extension('/path/to/json1/extension');

Once the extension is enabled, you can create a table that has a column of type JSON using the following SQL statement:

CREATE TABLE users (
  id INTEGER PRIMARY KEY,
  data JSON
);

Inserting JSON Data

To insert a JSON value into this table, you can use the json_encode function provided by the JSON1 extension, like this:

INSERT INTO users (id, data) VALUES (1, json_encode({
  "name": "Alice",
  "age": 25,
  "email": "alice@example.com"
}));

Querying JSON Data

To query this table, you can use the json_extract function, which allows you to extract values from the JSON column by specifying a JSON path. For example, to get the email address of the user with id 1, you can use the following query:

SELECT json_extract(data, '$.email') AS email
FROM users
WHERE id = 1;

Store JSON in BLOB

Another way to store and query JSON in SQLite is to use the BLOB type. In this case, you can create a table with a BLOB column like this:

CREATE TABLE users (
  id INTEGER PRIMARY KEY,
  data BLOB
);

Inserting JSON into a BLOB Column

To insert a JSON value into this table, you can use the json_encode function to encode the value as a string, and then use the hex function to convert it to a BLOB, like this:

INSERT INTO users (id, data) VALUES (1, hex(json_encode({
  "name": "Alice",
  "age": 25,
  "email": "alice@example.com"
})));

Querying JSON in a BLOB Column

To query this table, you can use the JSON_EXTRACT function provided by the JSON1 extension, but you need to convert the BLOB value back to a string using the unhex function, like this:

SELECT json_extract(unhex(data), '$.email') AS email
FROM users
WHERE id = 1;

Summary

In conclusion, SQLite provides two ways to store and query JSON values: using the JSON1 extension and using the BLOB type. Both approaches have their own advantages and disadvantages, and which one you choose will depend on your specific needs.

If you need to convert JSON data to SQL INSERT statements, check out our free JSON to SQL Converter tool.

Beekeeper Studio는 무료 & 오픈 소스 데이터베이스 GUI입니다

제가 사용해 본 최고의 SQL 쿼리 & 편집기 도구입니다. 데이터베이스 관리에 필요한 모든 것을 제공합니다. - ⭐⭐⭐⭐⭐ Mit

Beekeeper Studio는 빠르고 직관적이며 사용하기 쉽습니다. Beekeeper는 많은 데이터베이스를 지원하며 Windows, Mac, Linux에서 훌륭하게 작동합니다.

Beekeeper의 Linux 버전은 100% 완전한 기능을 갖추고 있으며, 기능 타협이 없습니다.

사용자들이 Beekeeper Studio에 대해 말하는 것

★★★★★
"Beekeeper Studio는 제 예전 SQL 워크플로를 완전히 대체했습니다. 빠르고 직관적이며 데이터베이스 작업을 다시 즐겁게 만들어 줍니다."
— Alex K., 데이터베이스 개발자
★★★★★
"많은 데이터베이스 GUI를 사용해 봤지만, Beekeeper는 기능과 단순함 사이의 완벽한 균형을 찾았습니다. 그냥 작동합니다."
— Sarah M., 풀스택 엔지니어

SQL 워크플로를 개선할 준비가 되셨나요?

download 무료 다운로드