首页 新闻 赞助 找找看

pymysql的插入报错问题

0
悬赏园豆:5 [已解决问题] 解决于 2017-07-13 11:54
sql = "insert into articles(title,content,date,expired) values('%s','%s','%s','%s')" % (article['title'],article['content'],article['date'],'false')

执行总是报错说语法不对。
跟踪看到是:

insert into articles(title,content,date,expired) values(\\'央行媒体:进一步减少政府干预汇市 扩大汇率浮动区间\\',\\'843dbc9f1bca7434454c354d7d38d19c\\',\\'2017年07月12日08:31\\',\\'false\\');
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\\'央行媒体:进一步减少政府干预汇市 扩大汇率浮动区间\\',\\' at line 1


问:为什么python会自动编译成了\\' ,网上有的用""".........""",有的用“。。。。。”,这个到底是什么讲究

小小财经的主页 小小财经 | 初学一级 | 园豆:1
提问于:2017-07-12 17:37
< >
分享
最佳答案
1

%s 不要加单引号

sql = "insert into articles(title,content,date,expired) values('%s','%s','%s','%s')" % (article['title'],article['content'],article['date'],'false')

改成:

sql = "insert into articles(title,content,date,expired) values(%s,%s,%s,%s)" % (article['title'],article['content'],article['date'],'false')
收获园豆:5
悟行 | 专家六级 |园豆:12559 | 2017-07-13 09:07

"""insert into articles(title,content,date,expired) values('%s','%s','%s','%s')""" % (article['title'],article['content'],article['date'],article['expired'])

我把article这个dict的值提前赋英文字符串是没有问题的,但如果有中文就报错,根据提示来看,好像是说unicode字符问题,我的mysql数据库是utf-8的,

-----------------我的值是这样获取过来的------------

response.encoding = 'utf-8'
content = response.text
pqContent = pq(content)
title = pqContent('#artibodyTitle').text()    #这个值应该就是utf-8了吧,

小小财经 | 园豆:1 (初学一级) | 2017-07-13 10:07

@无才不肖生:  可以换一种方法

some_dictionary_with_the_data = {
    'name': 'awesome song',
    'artist': 'some band',
    etc...
}
cursor.execute ("""
            INSERT INTO Songs (SongName, SongArtist, SongAlbum, SongGenre, SongLength, SongLocation)
            VALUES
                (%(name)s, %(artist)s, %(album)s, %(genre)s, %(length)s, %(location)s)

        """, some_dictionary_with_the_data)

https://stackoverflow.com/questions/775296/python-mysql-parameterized-queries/775399#775399

悟行 | 园豆:12559 (专家六级) | 2017-07-13 10:44

@抽象ID: {'title': '逆周期因子引入至今 人民币成为亚洲最强的货币', 'content': 'ff0427ca2f9728c81d7ba0030cc89e16', 'date': '2017年07月13日04:16', 'expired': 'false'}

cursor.execute("""insert into articles(title,content,date,expired) values(%(title)s,%(content)s,%(date)s,%(expired)s)""" ,article)

也不行

 

UnicodeEncodeError: 'latin-1' codec can't encode characters in position 57-72: ordinal not in range(256)

小小财经 | 园豆:1 (初学一级) | 2017-07-13 11:04

@无才不肖生: 这个是报编码问题,和格式,语法没有关系了,你处理一下编码

悟行 | 园豆:12559 (专家六级) | 2017-07-13 11:15

@抽象ID: 其实我的写法和你的写法在传过来的article中没有中文时都可以执行通过,但就是中文不行,我知道不是语法的问题,可mysql数据库已经设置了utf-8编码,前面dict中的各个值是从res = requests.get()过来的,也设置了res.encoding = 'utf-8',所以我就搞不懂了

小小财经 | 园豆:1 (初学一级) | 2017-07-13 11:19

@无才不肖生:  conn = MySQLdb.connect('localhost','root','rootpass','oj',charset='utf8')

链接数据库加个编码试试:charset='utf8'

 

悟行 | 园豆:12559 (专家六级) | 2017-07-13 11:23
悟行 | 园豆:12559 (专家六级) | 2017-07-13 11:29

@抽象ID: 的确是,非常感谢你,这么有耐心的帮忙

小小财经 | 园豆:1 (初学一级) | 2017-07-13 11:51
其他回答(1)
1

参考下面:

#coding=utf-8
import MySQLdb

conn= MySQLdb.connect(
host='localhost',
port = 3306,
user='root',
passwd='123456',
db ='test',
)
cur = conn.cursor()

#插入一条数据
sqli="insert into student values(%s,%s,%s,%s)"
cur.execute(sqli,('3','Huhu','2 year 1 class','7'))

cur.close()
conn.commit()
conn.close()
ycyzharry | 园豆:25639 (高人七级) | 2017-07-12 19:03

这样我有试过是可以的,我后面传过来的是一个dict 

如果sqli="insert into student values(%s,%s,%s,%s)" cur.execute(sqli,(dict['key1'],dict['key2'],dict['key3'],dict['key4'])),这样就不行

支持(0) 反对(0) 小小财经 | 园豆:1 (初学一级) | 2017-07-12 20:28

q我发现是因为无法插入中文,我传过来的tulpe中值如果有中文就不行,

支持(0) 反对(0) 小小财经 | 园豆:1 (初学一级) | 2017-07-12 21:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册