Ubuntu创建utf-8字符集的mysql数据库,解决 Incorrect string value: \\xE6\\x88\\x91\\xE6的问题

版权声明:转载请标注来源 https://blog.csdn.net/hehedadaq/article/details/82290759

Ubuntu创建utf-8字符集的mysql数据库

前言:

最近一直在做这个爬虫结合MySQL的小项目,发现具体操作的bug是真的很多!
尤其是我对基本的命令,以及一些细节的忽略,导致的格式问题!
特别是这个中文字符串的问题!
从Windows到Ubuntu,总会遇到!

在windows中有一个可视化软件Navicat,可以根据步骤走,也可以看到我创建的数据库的属性,但是在Ubuntu上,只有命令行,根本难以想象。

我第一次用命令行创建了一个数据库,以为默认的会是utf8的,没想到默认的是Latin的,,,
将Windows下能顺利执行的代码,复制到Ubuntu下,出现了下面的bug:

(1366, u"Incorrect string value: '\\xE6\\x88\\x91\\xE6\\x98\\xAF...' for column 'title' at row 1")
failed!

咦?难道是两个系统不同导致编码问题?

其实并不是!只是我的数据库创建有问题!

问题原因:

参考链接:
Mysql 插入中文错误:Incorrect string value: ‘\xE7\xA8\x8B\xE5\xBA\x8F…’ for column ‘course’ at row 1
出现这个错误的原因是,数据库的编码格式为latin1 而我要将utf8的中文插入到数据库中。

他这个是修改了原数据库的编码格式,而我是直接删除了,然后重新建一个靠谱的。

修改原数据库步骤:

1、先进入数据库:

mysql -uroot -p

然后输入你的数据库密码,这个是安装这个的时候,设置的;

sudo apt-get install mysql-server

这个密码一定要记得,否则就特别尴尬,就算是卸载重装都不行!
这个界面设置输入密码的:
这里写图片描述
在红线框中输入,然后摁enter确定就好,还得确认一次密码。

我那个Ubuntu的数据库密码就忘了,,,尴尬的一匹!

~/csdn$ mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 28
Server version: 5.7.23-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>这里就是你需要输入的命令位置了 

2、进入数据库

输入命令:

use csdn;

其中csdn是我创建的数据库名称,你换成你自己的就好,大小写不敏感,其他教程里,会把参数大写,自己的变量小写,好像是这样~

mysql> use csdn;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> 

这才是进入了CSDN这个数据库,你自己创建的,要不然就会在默认的root数据库那儿。

3、查看数据表编码

输入下面指令,查看数据库到底是什么编码:

mysql> show create table table_1;
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                                                                                                   |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| table_1 | CREATE TABLE `table_1` (
  `num` int(11) NOT NULL,
  `blogs_id` int(11) NOT NULL,
  `title` tinytext NOT NULL,
  `page_view` int(11) NOT NULL,
  PRIMARY KEY (`blogs_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> 

这里注意的是,第一个table是参数,必须要写的,第二个table_1,是我创建的表。
这是后来我重新建的,所以在default那儿,可以看到charset=utf8.
本来应该是这样的:
这里写图片描述
这个是参考博客的图,可以看到username和course中,编码是latin1,这个就很要命了啊!

我自己又为了验证,重新创建了一个数据库和表:
1、创建数据库,默认是Latin1编码!

mysql> create database demo;
Query OK, 1 row affected (0.00 sec

2、展示有几个数据库:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| csdn               |
| demo               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

3、切换到demo数据库:

mysql> use demo;
Database changed

看一下它的属性:

mysql> show create database demo;
+----------+-----------------------------------------------------------------+
| Database | Create Database                                                 |
+----------+-----------------------------------------------------------------+
| demo     | CREATE DATABASE `demo` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)

4、创建一个表格:

mysql> create table demo_table(title text not null)

显示如下:

mysql> show create table demo_table;
+------------+--------------------------------------------------------------------------------------------+
| Table      | Create Table                                                                               |
+------------+--------------------------------------------------------------------------------------------+
| demo_table | CREATE TABLE `demo_table` (
  `title` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+------------+--------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

再创建一个设置为utf8的表格:

mysql> create table demo_table_1(title text not null)ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

显示效果:

mysql> show create table demo_table_1;
+--------------+--------------------------------------------------------------------------------------------+
| Table        | Create Table                                                                               |
+--------------+--------------------------------------------------------------------------------------------+
| demo_table_1 | CREATE TABLE `demo_table_1` (
  `title` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+--------------+--------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

没想到,第二种方法,竟然可以插入中文!

mysql> insert into demo_table_1(title) values ('我是中文');
Query OK, 1 row affected (0.00 sec)

5、咦,有点出乎意料~标题改为:如果上面的不行,也可以直接修改不合适的编码:
先看看表格的属性:

mysql> show create table demo_table;
+------------+--------------------------------------------------------------------------------------------+
| Table      | Create Table                                                                               |
+------------+--------------------------------------------------------------------------------------------+
| demo_table | CREATE TABLE `demo_table` (
  `title` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+------------+--------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

输入:

mysql> alter table demo_table change title title text character set utf8;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

标注:第一个table是参数,demo_table 是你表的名称,change是参数,title和title都是你的栏目、表头,这俩是一样的!,而且不加单引号!
text是你的数据格式,定义的时候是啥就是啥,看看上面的表也能看到。
character参数。
set,这个原教程里没有加上!
也是参数
utf8不加引号!
最后的;记得加上!
真是错一个都不行!

最后看看效果?

mysql> show create table demo_table;
+------------+------------------------------------------------------------------------------------------------------+
| Table      | Create Table                                                                                         |
+------------+------------------------------------------------------------------------------------------------------+
| demo_table | CREATE TABLE `demo_table` (
  `title` text CHARACTER SET utf8
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+------------+------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

咦,我只改了title的编码格式,整个表的没有!
所以最后加一个改表的!
table是参数,demo_table是表名,其他的都是参数。
真是受够了!

mysql> alter table demo_table default character set utf8;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table demo_table;
+------------+---------------------------------------------------------------------------------+
| Table      | Create Table                                                                    |
+------------+---------------------------------------------------------------------------------+
| demo_table | CREATE TABLE `demo_table` (
  `title` text
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+------------+---------------------------------------------------------------------------------+
1 row in set (0.00 sec)

6、最后删除这些垃圾:
drop命令就好,database是参数,demo是你建立的那个数据库。OK~

mysql> drop database demo;
Query OK, 2 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| csdn               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

直接创建一个utf8的数据库!

差点忘了,我最终采用的是直接创建utf8的数据库,命令行该这样写:

CREATE DATABASE db_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

大小写没关系的,你只需要修改一下db_name就好,换成你自己的~

猜你喜欢

转载自blog.csdn.net/hehedadaq/article/details/82290759