[問題] SQLite3 將資料存在資料庫中

看板Python作者 (Sam)時間5年前 (2018/07/14 19:25), 5年前編輯推噓0(003)
留言3則, 2人參與, 5年前最新討論串1/1
各位大大好 小弟非本科系生 目前自學Python3 程式碼如下:(Python3.6) import sqlite3 with sqlite3.connect('c.sqlite') as db: c.to_sql('c',con=db,self.if_exists='append') (c這資料是用pandas將台銀牌告匯率爬取下來,參考如下網址: https://www.youtube.com/watch?v=-c5rrzjsN34)
錯誤: File "<ipython-input-56-246156207101>", line 3 c.to_sql('c',con=db,self.if_exists='append') ^ SyntaxError: keyword can't be an expression 題目只是單純將c這資料存在資料庫中 另若是把self.拿掉 則出現以下錯誤: OperationalError Traceback (most recent call last) <ipython-input-57-e5a3883ff452> in <module>() 1 import sqlite3 2 with sqlite3.connect('c.sqlite') as db: ----> 3 c.to_sql('c',con=db,if_exists='append') c:\users\sam\appdata\local\programs\python\python36-32\lib\site-packages\pandas\core\generic.py in to_sql(self, name, con, schema, if_exists, index, index_label, chunksize, dtype) 2128 sql.to_sql(self, name, con, schema=schema, if_exists=if_exists, 2129 index=index, index_label=index_label, chunksize=chunksize, -> 2130 dtype=dtype) 2131 2132 def to_pickle(self, path, compression='infer', c:\users\sam\appdata\local\programs\python\python36-32\lib\site-packages\pandas\io\sql.py in to_sql(frame, name, con, schema, if_exists, index, index_label, chunksize, dtype) 448 pandas_sql.to_sql(frame, name, if_exists=if_exists, index=index, 449 index_label=index_label, schema=schema, --> 450 chunksize=chunksize, dtype=dtype) 451 452 c:\users\sam\appdata\local\programs\python\python36-32\lib\site-packages\pandas\io\sql.py in to_sql(self, frame, name, if_exists, index, index_label, schema, chunksize, dtype) 1478 if_exists=if_exists, index_label=index_label, 1479 dtype=dtype) -> 1480 table.create() 1481 table.insert(chunksize) 1482 c:\users\sam\appdata\local\programs\python\python36-32\lib\site-packages\pandas\io\sql.py in create(self) 571 "'{0}' is not valid for if_exists".format(self.if_exists)) 572 else: --> 573 self._execute_create() 574 575 def insert_statement(self): c:\users\sam\appdata\local\programs\python\python36-32\lib\site-packages\pandas\io\sql.py in _execute_create(self) 1249 with self.pd_sql.run_transaction() as conn: 1250 for stmt in self.table: -> 1251 conn.execute(stmt) 1252 1253 def insert_statement(self): OperationalError: duplicate column name: Date 希望各位大大可幫小弟解惑一下 謝謝 感恩 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.192.84.222 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1531567519.A.0A8.html

07/14 19:35, 5年前 , 1F
不用self吧
07/14 19:35, 1F
※ 編輯: xy210742 (123.192.84.222), 07/14/2018 19:56:16

07/15 06:44, 5年前 , 2F
怎麼感覺看到 pandas 語法,是不是整個搞錯了
07/15 06:44, 2F

07/15 06:47, 5年前 , 3F
你的程式碼也不完整,建議整個張貼好確認
07/15 06:47, 3F
※ 編輯: xy210742 (123.192.81.136), 07/15/2018 16:43:40
文章代碼(AID): #1RITsV2e (Python)