解决出现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
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。