以上为我的数据库,我在数据库中书如华为,手机等 中文时出现错误[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;