mysql数据库用python插入时,unknown column “中文字“ in field list

尝试用python写sql进去

  • 第一轮
 insert_sql2 = 'insert into hotword_12345 (hotword,num) values ({},{})'
        for i in range (resultDf.shape[0]):
            cursor.execute(insert_sql2.format(str(resultDf.iloc[i, 0]),
                                              int(resultDf.iloc[i, 1])))

一直失败,以为是utf8的问题,

alter table tablename convert to charset utf8;

  • 第二轮
    结果还是报错,结果突然想到是不是中文识别不出,改成
 insert_sql2 = 'insert into hotword_12345 (hotword,num) values ("{}",{})'
        for i in range (resultDf.shape[0]):
            cursor.execute(insert_sql2.format(str(resultDf.iloc[i, 0]),
                                              int(resultDf.iloc[i, 1])))

结果还是写不进去,但是我在navicat里面,尝试这条sql语句,是可以写入的

insert into hotword_12345 (hotword,num) values (“人身安全”,1)
在这里插入图片描述

  • 第三轮
    然后还是发现还是写不进去,这个时候是不是不是string格式的问题
insert_sql2 = 'insert into hotword_12345 (hotword,num) values ("{}",{})'
        for i in range (resultDf.shape[0]):
            cursor.execute(str(insert_sql2.format(str(resultDf.iloc[i, 0]),
                                              int(resultDf.iloc[i, 1]))))

可以了
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值