実現したいこと
pythonでMySQLへデータを挿入したいのです。
前提
- コネクターはMySQL Connectorです。
- 辞書profileに入っているデータは下記の通りです。
{'登録': '繁殖', '品種': 'サラ', '性別': '牡', '生年月日': '2002/03/2516歳', '馬主': '金子真人ホールディングス(株)', '毛色': '鹿毛', '調教師': '池江泰郎(栗東) ', '生産牧場': 'ノーザンファーム', '産地': '早来産', '戦績': '国内:13戦12勝海外:1戦0勝', '総賞金': '145455.1万円', '市場取引': '2002年7350.0万円セレクトセールサ ラブレッド当歳', '繋養先': '社台スタリオンステーション 勇払郡安平町電話:0145-22-4581', '種付料': '2018年度:プライベート'}
発生している問題・エラーメッセージ
Traceback (most recent call last): File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mysql/connector/connection_cext.py", line 377, in c md_query raw_as_string=raw_as_string) _mysql_connector.MySQLInterfaceError: 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 'values (742976, ディープインパクト, Deep Impact, JPN, 繁殖, サラ, ' at line 1 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "horse_q.py", line 76, in <module> profile['市場取引'], profile['繋養先'], profile['種付料'])) File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mysql/connector/cursor_cext.py", line 264, in execu te raw_as_string=self._raw_as_string) File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mysql/connector/connection_cext.py", line 380, in c md_query sqlstate=exc.sqlstate) mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for th e right syntax to use near 'values (742976, ディープインパクト, Deep Impact, JPN, 繁殖, サラ, ' at line 1
該当のソースコード
python
1cursor.execute('insert into horse \ 2 (id, name_ja, name_en, country, \ 3 state, kind, sex, date_of_birth, owner, coat_color, \ 4 trainer, breeding_farm, producting_area, past_performance, earning, \ 5 market, maintin_farm, stud_fee \ 6 values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)' % 7 (start, name_ja, name_en, country, 8 profile['登録'], profile['品種'], profile['性別'], 9 profile['生年月日'], profile['馬主'], profile['毛色'], 10 profile['調教師'], profile['生産牧場'], profile['産地'], 11 profile['戦績'], profile['総賞金'], 12 profile['市場取引'], profile['繋養先'], profile['種付料']))
ソースコード全文
python
1import mysql.connector 2import urllib.request, urllib.error 3from bs4 import BeautifulSoup 4import re 5 6connect = mysql.connector.connect( 7 host='localhost', user='root', passwd='********', db='horse_test', charset='utf8') 8cursor = connect.cursor() 9 10# プロフィールの項目 11profile_key_list = ['登録', '品種', '性別', '生年月日', '馬主', '毛色', 12 '調教師', '生産牧場', '産地', '戦績', '総賞金', 13 '市場取引', '繋養先', '種付料'] 14profile_dict = {i: [] for i in profile_key_list} 15profile = {i: [] for i in profile_key_list} 16 17start = 742976 18stop = start 19 20while start <= stop: 21 22 url = urllib.request.urlopen('http://www.jbis.or.jp/horse/' + '{0:010d}'.format(start) + '/') 23 html = BeautifulSoup(url, 'lxml') 24 25 try: 26 name_ja = html.find('h1', class_='hdg-l1-02').text 27 name_en = html.find('div', class_='sup').text.strip().splitlines()[0] 28 if name_ja == '_________': 29 name_ja, name_en = 'unknown', 'unknown' 30 elif '現在' in name_en: 31 name_en = None 32 except: 33 name_ja,name_en = None, None 34 35 try: 36 country = re.search(re.compile(r'(.+?)'), (name_ja + name_en))[0] 37 except: 38 country = 'unk' 39 name_ja, name_en = name_ja.replace(country, ''), name_en.replace(country, '') 40 if name_en == '': 41 name_en = None 42 country = country.replace('(', '').replace(')', '') 43 44 try: 45 profile_table_html = html.find_all('table', class_='tbl-data-05') 46 profile_table_text = profile_table_html[0].text.replace(' (アラブ血量)', '\n') 47 profile_list = [i for i in profile_table_text.splitlines() if i not in ['', u'\xa0']] 48 except: 49 profile_list = [] 50 51 key = profile_list[0] 52 assert key in profile_list 53 54 for i in profile_list[1:]: 55 if i in profile_dict: 56 key = i 57 elif i.strip() == '写真': 58 break 59 else: 60 profile_dict[key].append(i.strip().replace(u'\u3000', '')) 61 62 for i in profile_dict: 63 profile[i] = ''.join(profile_dict[i]) 64 65 cursor.execute('insert into horse \ 66 (id, name_ja, name_en, country, \ 67 state, kind, sex, date_of_birth, owner, coat_color, \ 68 trainer, breeding_farm, producting_area, past_performance, earning, \ 69 market, maintin_farm, stud_fee \ 70 values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)' % 71 (start, name_ja, name_en, country, 72 profile['登録'], profile['品種'], profile['性別'], 73 profile['生年月日'], profile['馬主'], profile['毛色'], 74 profile['調教師'], profile['生産牧場'], profile['産地'], 75 profile['戦績'], profile['総賞金'], 76 profile['市場取引'], profile['繋養先'], profile['種付料'])) 77 78 start += 1 79 80# 保存 81connect.commit() 82connect.close()
以上、よろしくお願い致します。
回答2件
あなたの回答
tips
プレビュー