python mysql插入数据报错:TypeError: %d format: a number is required, not str
(0)目錄
走,是一輩子,不走,也是一輩子(程序猿之路)
Navicat連接mysql出現2003——can't connect to mysql server on localhost(10061)
mysql 數據庫導入導出方法總結(是時候總結)
1:起因
最近工作需求 ---- 實時統計一份數據,insert到mysql數據庫中;?
方法:很自然的就想到了python插入數據庫,yum install MySQL-python.x86_64à import MySQLdb(python2.X僅僅適用)
報錯如下
" File"/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 151, inexecute query = query % db.literal(args)
TypeError: %d format: a number is required, not str"
解決方案:The?format string is?not really a normal Python?format string. Youmust always?use?%s?for all fields.?也就是MySQLdb的字符串格式化不是標準的python的字符串格式化,應當一直使用%s用于字符串格式化
2,具體細節
(1)MySQLdb python包安裝:
[@10.134.105.160 auto_tarh]#yum list | grep -i sql?? ----
MySQL-python.x86_64??????????????? ??????1.2.3-0.3.c1.1.el6?? @rhel-6.stable-rhel-x86_64-server-6??
-----yum install MySQL-python.x86_64 --> import MySQLdb(python2.X僅僅適用)
(2) mysql數據庫安裝
[@10.134.105.160 auto_tarh]#yum list installed | grep sql? ??---- 檢查安裝列表
mysql.x86_64?????????????????? 5.1.61-1.el6_2.1??????? @rhel-6.2-rhel-x86_64-server-6??? ----- mysql –h192.168.0.1 –uusername -ppassword
mysql-devel.x86_64???????????? 5.1.61-1.el6_2.1??????? @rhel-6.2-rhel-x86_64-server-6
mysql-libs.x86_64????????????? 5.1.61-1.el6_2.1??????? @rhel-x86_64-server-6/6.2
php-mysql.x86_64?????????????? 5.3.3-3.el6_2.8???????? @rhel-6.2-rhel-x86_64-server-6
----- yum installmysql.x86_64, 安裝mysql命令
3,開始書寫代碼
(1)python insert代碼
cursor.execute("""insert into tree (id,parent_id,level,description,code,start,end)values (%d,%d,%d,%s,%s,%f,%f)""", (1,1,1,'abc','def',1,1)) (2)The structure of my MYSQL table is: id int(255), parent_id int(255), level int(11), description varchar(255), code varchar(255), start decimal(25,4), end decimal(25,4)(3)遇到的錯誤:
" File"/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 151, inexecute query = query % db.literal(args)
TypeError: %d format: a number is required, not str"
解決方案:The?format string is?not really a normal Python?format string. Youmust always?use?%s?for all fields.?也就是MySQLdb的字符串格式化不是標準的python的字符串格式化,
應當一直使用%s用于字符串格式化
(4)同時還遇到一個奇葩的錯誤:
_mysql_exceptions.ProgrammingError: (1064, "You havean error in your SQ L syntax; check the manual that
corresponds to your MySQLserver version for the right syntax to use near ')' at line 1")
1 #coding=utf-82 3 import sys4 import traceback 5 import MySQLdb 6 7 conn= MySQLdb.connect(8 host='res.searchapp.rds.sogou',9 port = 3306,10 user='searchapp',11 passwd='searchappres',12 db ='searchapp',13 )14 cur = conn.cursor()15 def test():16 #插入一條數據17 #sqli="""insert into novel_card values('test3', 100, 1, 0.01, '20170707')"""18 #sqli="""insert into novel_card values('%s', %d, 1, 0.01, '20170707')"""19 20 #cur.execute(sqli)21 #sqli="""insert into novel_card values('%s', '%s', '%s', '%s', '%s')"""22 #sqli="""insert into novel_card (runoob_author, show, click, ctr, data_time) values('%s', %s, %s, %s, '%s')"""23 sqli="insert into novel_card values(%s, %s, %s, %s, %s)"24 print sqli %('t4','1','2','0.1','20170707')25 cur.execute(sqli, ('t5','1','2','0.1','20170707'))26 #cur.execute(sqli,('test5', '100', '1', '0.01', '20170707'))27 #sqli="insert into novel_card values('%s','%s')"28 #cur.execute(sqli,('test3', '20170707')) ?48 test()49 cur.close()50 conn.commit()51 conn.close()
總結
以上是生活随笔為你收集整理的python mysql插入数据报错:TypeError: %d format: a number is required, not str的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我有阳光(我爱生活)
- 下一篇: 作为程序员,如何防辐射?