日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

python mysql插入数据报错:TypeError: %d format: a number is required, not str

發(fā)布時(shí)間:2023/12/18 数据库 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python mysql插入数据报错:TypeError: %d format: a number is required, not str 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

(0)目錄

走,是一輩子,不走,也是一輩子(程序猿之路)

Navicat連接mysql出現(xiàn)2003——can't connect to mysql server on localhost(10061)

mysql 數(shù)據(jù)庫(kù)導(dǎo)入導(dǎo)出方法總結(jié)(是時(shí)候總結(jié))


1:起因

最近工作需求 ---- 實(shí)時(shí)統(tǒng)計(jì)一份數(shù)據(jù),insert到mysql數(shù)據(jù)庫(kù)中;?

方法:很自然的就想到了python插入數(shù)據(jù)庫(kù),yum install MySQL-python.x86_64à import MySQLdb(python2.X僅僅適用)

報(bào)錯(cuò)如下

" 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的字符串格式化不是標(biāo)準(zhǔn)的python的字符串格式化,應(yīng)當(dāng)一直使用%s用于字符串格式化


2,具體細(xì)節(jié)

(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數(shù)據(jù)庫(kù)安裝

[@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,開(kāi)始書(shū)寫(xiě)代碼

(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)遇到的錯(cuò)誤:

" 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的字符串格式化不是標(biāo)準(zhǔn)的python的字符串格式化,

應(yīng)當(dāng)一直使用%s用于字符串格式化

(4)同時(shí)還遇到一個(gè)奇葩的錯(cuò)誤:

_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 #插入一條數(shù)據(jù)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()


總結(jié)

以上是生活随笔為你收集整理的python mysql插入数据报错:TypeError: %d format: a number is required, not str的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。