zoukankan      html  css  js  c++  java
  • Mysql新建表,插入中文时报错“Incorrect string value: 'xE4xBDxA0xE5xA5xBD' for column”问题

    有时候我们在往数据库中输入信息时,如果输入的内容是中文,会报错“Incorrect string value: 'xE4xBDxA0xE5xA5xBD' for column”。

    例如:

    CREATE TABLE test(ID INT PRIMARY KEY AUTO_INCREMENT

    , test_name VARCHAR(20) , test_num INT);                                                       /*建立一个表*/

    insert into test(test_name) values('你好');                                                         /*建表之后插入一条带有中文的数据*/

     

    解决的方法:

    1、在建立表时设置默认字符串编码方式为utf8

    CREATE TABLE test2(ID INT PRIMARY KEY AUTO_INCREMENT

    , test_name VARCHAR(20) , test_num INT)default charset = utf8;                        /*建立一个表,加上“default charset = utf8”,设置默认字符串编码方式为utf8。*/

    insert into test2(test_name) values('你好');                                                      /*建表之后插入一条带有中文的数据*/

    这次就可以成功添加

    2、已经添加的表,需要设置一下:ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

    ALTER TABLE test CONVERT TO

    CHARACTER SET utf8 COLLATE utf8_unicode_ci;                                               /*例如刚刚建立的test表,插入中文时会报错,现在对其进行设置*/

    insert into test(test_name) values('你好');                                                        /*设置之后插入一条带有中文的数据*/

    3、直接修改数据库的字符串编码属性:ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci

    ALTER DATABASE testdb CHARACTER

    SET utf8 COLLATE utf8_unicode_ci;                                                                /*修改数据库testdb的编码方式*/

    CREATE TABLE testdb.test3(ID INT PRIMARY KEY AUTO_INCREMENT

    , test_name VARCHAR(20) , test_num INT)default charset = utf8;                        /*建立一个表,加上“default charset = utf8”,设置默认字符串编码方式为utf8。*/

    insert into testdb.test3(test_name) values('你好');                                             /*建表之后插入一条带有中文的数据*/

  • 相关阅读:
    在“安装”阶段发生异常。 System.Security.SecurityException: 未找到源,但未能
    [转]C# 实现Jwt bearer Authentication
    json序列化数据超出最大值(maxJsonLength)
    設計之家-教程
    Python Dom 的介绍和使用day1
    Python CSS day2
    回顾
    Python CSS day1
    Python HTML day2
    Python HTML day1
  • 原文地址:https://www.cnblogs.com/wangjikun/p/5687370.html
Copyright © 2011-2022 走看看