mysql為何不能插入資料?

來源:互聯網
上載者:User
安裝AppServ後首次使用mysql,沒有圖形介面,在“MySql Command Line Client”的操作如下:
======================================================================================
mysql> create database cars;
Query OK, 1 row affected (0.00 sec)

mysql> use cars;
Database changed
mysql> create table cars(id int,xm char(8),h char(8),vin char(20));
Query OK, 0 rows affected (0.06 sec)

mysql> insert into cars(id,xm,h,vin) values(1,'廉東方','豫A5F568','LDC666L2980790000');
Query OK, 1 row affected, 1 warning (0.02 sec)

mysql> select * from cars;
+------+------+----------+-------------------+
| id | xm | h | vin |
+------+------+----------+-------------------+
| 1 | | 豫A5F568 | LDC666L2980790000 |
+------+------+----------+-------------------+
1 rows in set (0.02 sec)

mysql> select xm from cars;
+------+
| xm |
+------+
| |
+------+
1 rows in set (0.00 sec)

mysql> update cars set xm='廉' where id=1;
Query OK, 0 rows affected, 1 warning (0.01 sec)
Rows matched: 1 Changed: 0 Warnings: 0

為何不能設定xm欄位的值?為何無法更新?謝謝啦~~~


回複討論(解決方案)

mysql> \W
Show warnings enabled.
mysql> update cars set xm='廉東方' where id=1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0

Warning (Code 1366): Incorrect string value: '\xC1\xAE\xB6\xAB\xB7\xBD' for colu
mn 'xm' at row 1
mysql> _

這裡看插入資料的長度是6個位元組,不到設計的8個位元組,怎麼就錯了呢?


mysql> set names utf-8;
ERROR 1115 (42000): Unknown character set: 'utf'
mysql> set names utf8;
Query OK, 0 rows affected (0.00 sec)

mysql> update cars set xm='廉' where id=1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0

Warning (Code 1366): Incorrect string value: '\xC1\xAE' for column 'xm' at row 1

mysql> _


還是不行,快急死了~~~


mysql> insert into cars(id,xm,h,vin) values(1,'廉東方','豫A5F568','LDC666L298079
0000');
Query OK, 1 row affected (0.00 sec)

mysql> select * from cars;
+------+--------+----------+-------------------+
| id | xm | h | vin |
+------+--------+----------+-------------------+
| 1 | 廉東方 | 豫A5F568 | LDC666L2980790000 |
+------+--------+----------+-------------------+
1 row in set (0.00 sec)


說一下你用的什麼作業系統

剛開始學php,嫌配置麻煩,下個 appserv-win32-2.6.0.exe 直接安裝的。

難道它安裝的不合理?

電腦安裝的是win7作業系統~~~

先執行 set names gbk; 應該就可以了

先執行 set names gbk; 應該就可以了

剛才查資料,有的說用:set names utf8;經測試不行,

但“set names gbk;”確實成功了,非常感謝!

能簡單說一下為什麼嗎?

你 #1 的內容是我在#3回複後才看到的,不然一開始就解釋了

\xC1\xAE\xB6\xAB\xB7\xBD 是 廉東方 的gbk 內碼
而他卻報錯了,這說明你的資料庫的預設字元集不是 latin1 的,也不是 gbk 的
那麼對中文報錯就只能是 utf8 的了(極少有人用 ucs-2 的,就算是也沒關係)

對 utf8 欄位插入 gbk 字元當然就要報錯了
所以要 set names gbk 通知 mysql 現在用的是 gbk 字元

那麼“豫”為什麼沒有報錯呢?
這就要看 utf-8 是如何編碼的了:
U+007F 0xxxxxxx
U+07FF 110xxxxx 10xxxxxx
U+FFFF 1110xxxx 10xxxxxx 10xxxxxx
U+1FFFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
U+3FFFFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
U+7FFFFFFF 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx

豫 的gbk二進位內碼為 11010100 10100101
正好符合 U+07FF 編碼規則,所以他就不會報錯

謝謝您的耐心回答,收穫匪淺~~~

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.