在操作數據庫時出現錯誤:[HY000][1366] Incorrect string value: '\xE8\x8D\xA3\xE5\x82\xB2...' for column 'name' at......解決方案


以上為我的數據庫,我在數據庫中書如華為,手機等 中文時出現錯誤[HY000][1366] Incorrect string value: '\xE8\x8D\xA3\

xE5\x82\xB2... for column "address" at row 1 .

錯誤原因:這是字段編碼不是utf8。

 

 

解決:

創建數據庫時指定utf8:

create database day70 default character set utf8 collate utf8_general_ci;
 

可以看到我的表index_project有三個字段id、name、type。

解決方法:

1、檢查數據表所有字段的狀態 :show full columns from index_prodect;

2、可以看到name、type的Collation為  latin1_swedish_ci他不是非utf8,修改它

 alter table index_product change name name varchar(100) character set utf8 collate utf8_unicode_ci not null default '';

 

alter table index_product change type type varchar(100) character set utf8 collate utf8_unicode_ci not null default '';

修改之后再查看所有字段:show full columns from index_prodect;

可以看到:

字段已經改成了:utf8_unicode_ci

utf8編碼,這樣就可以輸入中文了。在MySQL workbench中添加數據   平板電腦   平板&穿戴,運行結果如下:

 

數據已成功添加。

或者在Pycharm中直接操作:

 二.、

>python manage.py makemigrations
You are trying to add a non-nullable field 'price_monthly' to product without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows)
 2) Quit, and let me add a default in models.py
Select an option

 

錯誤原因:是原來舊的數據庫文件和現在執行的操作有沖突。

 

解決方案:

1、在基類b中添加允許為空或者添加默認值,並設置b不建表(meta中的abstract = true)

class Base(models.Model):
    '''
    基類
    '''
    title = models.CharField(max_length=150, null=True)
    content = models.TextField(null=True)
    time_stamp = models.DateTimeField(auto_now_add=True, default=timezone.now())
    link = models.URLField(blank=True, verbose_name='url_link')

    class Meta:
        abstract = True

2、解決方法是:把migrations文件夾內除了__init__.py以外的文件刪除,重新執行makemigrations就可以了

3、創建數據庫時指定utf8:

create database day70 default character set utf8 collate utf8_general_ci;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM