MySQL insert json problem

MySQL insert json problem

MySQL 5.7.8 and later began to support a native JSON type that can efficiently obtain data in JSON text. This type has the following advantages:

  • Automatic verification during storage, error reporting if verification fails
  • Better storage structure. An internal structure is used to store JSON text elements that can be read quickly. The new server reads a JSON text in binary format, rather than reading it as a string and then converting it. This binary format allows the server to retrieve sub-objects (nested arrays) by key or array index in text without having to read the entire value.

In addition, the system has some restrictions on the JSON format:

  • The maximum length of the JSON text depends on the system constant: max_allowed_packet. This value is limited only when the server is storing data. It is allowed to exceed this value when calculating in memory.
  • JSON columns cannot have default values
  • Like other binary columns, JSON columns cannot be indexed. However, you can create indexes on certain table column values ​​from the text stored in the JSON column. The MySQL optimal controller also queries the index created using the JSON representation.

Problems encountered when inserting json data

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '{

Insert code

insert into `players` (`id`,`player_and_games`) values(1,{
  "id":1,
  "name":"aaa",
  "games_played":{
    "Battlefieldld":{
      "weapon":"adsf",
      "level":20
    },
    "Crazy":{
      "weapon":"adsf",
      "level":20
    }
  }
})

There is a problem, json does not use quotes, the correct way is as follows

insert into `players` (`id`,`player_and_games`) values(1,'{
  "id":1,
  "name":"aaa",
  "games_played":{
    "Battlefieldld":{
      "weapon":"adsf",
      "level":20
    },
    "Crazy":{
      "weapon":"adsf",
      "level":20
    }
  }
}')

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • Basic JSON Operation Guide in MySQL 5.7
  • MySQL 5.7 JSON type usage details
  • Instructions for using JSON operation functions in Mysql5.7
  • A brief discussion on MySQL 5.7 JSON format retrieval
  • How to query json in the database in mysql5.6 and below
  • Detailed explanation of JSON series operation functions in Mysql
  • MySQL operations: JSON data type operations
  • Detailed example of MySQL (5.6 and below) parsing JSON
  • Example analysis of the usage of the new json field type in mysql5.7

<<:  The principle and application of ES6 deconstruction assignment

>>:  Detailed explanation of the use of umask under Linux

Recommend

HTML form value transfer example through get method

The google.html interface is as shown in the figur...

Introduction to Computed Properties in Vue

Table of contents 1. What is a calculated propert...

9 Practical CSS Properties Web Front-end Developers Must Know

1. Rounded Corners Today's web designs are con...

MySQL foreign key constraint (FOREIGN KEY) case explanation

MySQL foreign key constraint (FOREIGN KEY) is a s...

Web design must have purpose, ideas, thoughts and persistence

<br />Introduction: This idea came to me whe...

Docker dynamically exposes ports to containers

View the IP address of the Container docker inspe...

Use Docker to run multiple PHP versions on the server

PHP7 has been out for quite some time, and it is ...

Detailed explanation of identifying files with the same content on Linux

Preface Sometimes file copies amount to a huge wa...

Tips on HTML formatting and long files for web design

<br />Related articles: 9 practical suggesti...

Detailed introduction to JS basic concepts

Table of contents 1. Characteristics of JS 1.1 Mu...

Vue realizes price calendar effect

This article example shares the specific code of ...

How to install Postgres 12 + pgadmin in local Docker (support Apple M1)

Table of contents introduce Support Intel CPU Sup...

How to read the regional information of IP using Nginx and GeoIP module

Install GeoIP on Linux yum install nginx-module-g...

XHTML tags that are easily confused by the location of the use

<br />We have always emphasized semantics in...