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

Installation tutorial of docker in linux

The Docker package is already included in the def...

Getting Started Tutorial for Beginners ⑨: How to Build a Portal Website

Moreover, an article website built with a blog pro...

WeChat applet implements countdown for sending SMS verification code

This article shares the specific code for the WeC...

Detailed explanation of Vue filters

<body> <div id="root"> <...

Comparative Analysis of IN and Exists in MySQL Statements

Background Recently, when writing SQL statements,...

MySQL Index Optimization Explained

In daily work, we sometimes run slow queries to r...

Does MySql need to commit?

Whether MySQL needs to commit when performing ope...

Detailed explanation of the function and usage of DOCTYPE declaration

1. Browser rendering mode and doctype Some web pa...

How to call the browser sharing function in Vue

Preface Vue (pronounced /vjuː/, similar to view) ...

Implementation process of nginx high availability cluster

This article mainly introduces the implementation...

A brief analysis of the usage of USING and HAVING in MySQL

This article uses examples to illustrate the usag...

A brief analysis of the use of the HTML webpack plugin

Using the html-webpack-plugin plug-in to start th...

Mysql database index interview questions (basic programmer skills)

Table of contents introduction Indexing principle...