解决出现Incorrect integer value: '' for column 'id' at row 1的问题


Posted in Python onOctober 29, 2017

解决出现Incorrect integer value: '' for column 'id' at row 1的问题

前言:

今天在学习Python的过程中操作数据库,遇到了一个问题,执行sql语句插入数据的时候报错Incorrect integer value: '' for column 'id' at row 1

我的数据库设计是这样的:

一个很简单的用户信息表,其中ID是int类型,自动填充的主键,其他两个非主键,我执行的insert语句是这样的:mysql.db_insert("insert into userinfo values('','sven','111222','sven@qq.com','1')")

执行的时候一直报错Incorrect integer value: '' for column 'id' at row 1,最后google了一下,发现mysql版本到5以上的都会遇到这样的问题,插入空字符要使用NULL 正确的SQL语句应该是mysql.db_insert("insert into userinfo values(NULL,'sven','111222','sven@qq.com','1')"),记录下来与大家共勉,共通学习。

顺便分享一个自己写的操作数据库的Python类

我的Python数据库编码使用utf-8,所以连接数据库的时候charset这个字段我就没有做处理

# -*- coding: utf-8 -*- 
import MySQLdb 
 
class Mysql: 
  conn = ''  
  cursor = '' 
 
  def __init__(self, host='localhost', usr='blogtest', password='111222', db='blogtest'): 
    try: 
      self.conn = MySQLdb.connect(host, usr, password, db) 
    except Exception, e: 
      print e 
    self.cursor = self.conn.cursor() 
    #self.query('SET NAME %s ' % charset) 
 
 
  def query(self, sql): 
    self.cursor.execute(sql) 
 
  def show(self): 
    return self.cursor.fetchall() 
 
  def db_insert(self,sql): 
    self.cursor.execute(sql) 
    self.conn.commit() 
 
  def __del__(self): 
    self.cursor.close() 
    self.conn.close() 
 
if __name__ == '__main__': 
  mysql = Mysql() 
  mysql.db_insert("insert into userinfo values(NULL,'sven','111222','sven@qq.com','1')") 
  mysql.query('SELECT * FROM userinfo') 
  data = mysql.show() 
  for x in data: 
    print x

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

Python 相关文章推荐
python实现批量下载新浪博客的方法
Jun 15 Python
python使用wmi模块获取windows下的系统信息 监控系统
Oct 27 Python
python构建自定义回调函数详解
Jun 20 Python
python如何重载模块实例解析
Jan 25 Python
python程序封装为win32服务的方法
Mar 07 Python
PyCharm搭建Spark开发环境实现第一个pyspark程序
Jun 13 Python
Python中的类与类型示例详解
Jul 10 Python
使用python实现微信小程序自动签到功能
Apr 27 Python
Python如何实现远程方法调用
Aug 07 Python
详解python百行有效代码实现汉诺塔小游戏(简约版)
Oct 30 Python
python常量折叠基础知识点讲解
Feb 28 Python
PYTHON基于Pyecharts绘制常见的直角坐标系图表
Apr 28 Python
Python批量更改文件名的实现方法
Oct 29 #Python
python生成二维码的实例详解
Oct 29 #Python
python 读写中文json的实例详解
Oct 29 #Python
Python3 处理JSON的实例详解
Oct 29 #Python
深入理解Python中的*重复运算符
Oct 28 #Python
13个最常用的Python深度学习库介绍
Oct 28 #Python
python探索之BaseHTTPServer-实现Web服务器介绍
Oct 28 #Python
You might like
一个用于MySQL的PHP XML类
2006/10/09 PHP
将时间以距今多久的形式表示,PHP,js双版本
2012/09/25 PHP
php多层数组与对象的转换实例代码
2013/08/05 PHP
PHP strstr 函数判断字符串是否否存在的实例代码
2013/09/28 PHP
PHP中使用imagick实现把PDF转成图片
2015/01/26 PHP
php将远程图片保存到本地服务器的实现代码
2015/08/03 PHP
javascript中input中readonly和disabled区别介绍
2012/10/23 Javascript
jquery ajaxSubmit 异步提交的简单实现
2014/02/28 Javascript
Jquery对数组的操作技巧整理
2014/03/25 Javascript
node.js中的console.assert方法使用说明
2014/12/10 Javascript
ECMAScript6中Set/WeakSet详解
2015/06/12 Javascript
学习vue.js计算属性
2016/12/03 Javascript
js实现本地图片文件拖拽效果
2017/07/18 Javascript
Angular使用Md5加密的解决方法
2017/09/16 Javascript
javascript使用正则实现去掉字符串前面的所有0
2018/07/23 Javascript
微信小程序自定义弹窗wcPop插件
2018/11/19 Javascript
详解ng-alain动态表单SF表单项设置必填和正则校验
2019/06/11 Javascript
three.js利用gpu选取物体并计算交点位置的方法示例
2019/11/25 Javascript
JavaScript运行机制实例分析
2020/04/11 Javascript
JavaScript中使用Spread运算符的八种方法总结
2020/06/18 Javascript
[13:21]DOTA2国际邀请赛采访专栏:RSnake战队国士无双,Fnatic.Fly
2013/08/06 DOTA
[44:04]OG vs Mineski 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/18 DOTA
python编写网页爬虫脚本并实现APScheduler调度
2014/07/28 Python
python简单实现获取当前时间
2016/08/27 Python
Python数据结构与算法之列表(链表,linked list)简单实现
2017/10/30 Python
tensorflow构建BP神经网络的方法
2018/03/12 Python
Python工程师必考的6个经典面试题
2020/06/28 Python
利于python脚本编写可视化nmap和masscan的方法
2020/12/29 Python
投资意向书范本
2014/04/01 职场文书
网络技术专业求职信
2014/05/02 职场文书
小学生环保标语
2014/06/13 职场文书
简单租房协议书
2014/10/21 职场文书
管理失职检讨书
2015/05/05 职场文书
导游词之天下银坑景区
2019/11/21 职场文书
在容器中使用nginx搭建上传下载服务器
2022/05/11 Servers
Windows7下FTP搭建图文教程
2022/08/05 Servers