2016-12-28 68 views
2

我對python和MySql相當新。我試圖使用python將excel文件中收到的數據導入到MySql中。腳本運行安靜,但我有一個簡單的選擇部分,這不正常工作。 它試圖從存儲已導入文件名的單個表中檢索數據,並按文件類型進行分類。所以桌子上有這兩列和幾百行。sql鍊金術查詢得到根據價值stucked

CREATE TABLE `imp_doc` (
    `docname` varchar(50) NOT NULL, 
    `doc_type` varchar(12) DEFAULT NULL, 
    PRIMARY KEY (`docname`), 
    KEY `doctype` (`doc_type`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

該腳本以除「1」之外的所有「文件類型」運行。當該值作爲值發送給select時,腳本將停止運行而不出現錯誤消息。

將「doc_type」參數設置爲「ibot」使其失速的腳本!

import sqlalchemy 
from sqlalchemy import create_engine 
from sqlalchemy.pool import NullPool 

engine = create_engine('mysql+mysqlconnector://userid:[email protected]:3306/mydb', poolclass = NullPool) 
engine.echo = True 
conn = engine.connect() 

saved_list =[] 
doc_type="ibot" 
query = "SELECT docname FROM imp_doc WHERE doc_type = %s" 
cur=conn.execute(query, (doc_type,)) 
for (docname) in cur: 
    dn=docname[0] 
    dn=unicode.encode(dn,'cp1252') 
    saved_list.append(str(dn)) 
print saved_list 
cur.close() 

結果,似乎Python是從MySQL等待結果,但在數據庫連接處於「休眠」。

2016-12-28 14:18:15,407 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode' 
2016-12-28 14:18:15,407 INFO sqlalchemy.engine.base.Engine {} 
2016-12-28 14:18:15,598 INFO sqlalchemy.engine.base.Engine SELECT DATABASE() 
2016-12-28 14:18:15,598 INFO sqlalchemy.engine.base.Engine {} 
2016-12-28 14:18:15,987 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS CHAR(60)) AS anon_1 
2016-12-28 14:18:15,997 INFO sqlalchemy.engine.base.Engine {} 
2016-12-28 14:18:16,187 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS CHAR(60)) AS anon_1 
2016-12-28 14:18:16,187 INFO sqlalchemy.engine.base.Engine {} 
2016-12-28 14:18:16,747 INFO sqlalchemy.engine.base.Engine SELECT docname FROM imp_doc WHERE doc_type = %s 
2016-12-28 14:18:16,757 INFO sqlalchemy.engine.base.Engine ('ibot',) 

當我運行與其他DOC_TYPE值相同的代碼它完美的作品:

2016-12-28 14:31:45,105 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode' 
2016-12-28 14:31:45,105 INFO sqlalchemy.engine.base.Engine {} 
2016-12-28 14:31:45,306 INFO sqlalchemy.engine.base.Engine SELECT DATABASE() 
2016-12-28 14:31:45,306 INFO sqlalchemy.engine.base.Engine {} 
2016-12-28 14:31:45,727 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS CHAR(60)) AS anon_1 
2016-12-28 14:31:45,727 INFO sqlalchemy.engine.base.Engine {} 
2016-12-28 14:31:45,947 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS CHAR(60)) AS anon_1 
2016-12-28 14:31:45,957 INFO sqlalchemy.engine.base.Engine {} 
2016-12-28 14:31:46,507 INFO sqlalchemy.engine.base.Engine SELECT docname FROM imp_doc WHERE doc_type = %s 
2016-12-28 14:31:46,507 INFO sqlalchemy.engine.base.Engine ('wiz',) 
['WIZARD_2015-W1001-20160102.xlsx', 'WIZARD_2016-W0104-20160612.xlsx', 'WIZARD_2016-W16.xlsx', 'WIZARD_2016-W17.xlsx', 'WIZARD_2016-W18.xlsx', 'WIZARD_2016-W19.xlsx', 'WIZARD_2016-W20.xlsx', 'WIZARD_2016-W21.xlsx', 'WIZARD_2016-W22.xlsx', 'WIZARD_2016-W23.xlsx', 'WIZARD_2016-W24.xlsx', 'WIZARD_2016-W25.xlsx', 'WIZARD_2016-W26.xlsx', 'WIZARD_2016-W27.xlsx', 'WIZARD_2016-W28.xlsx', 'WIZARD_2016-W29.xlsx', 'WIZARD_2016-W30.xlsx', 'WIZARD_2016-W31.xlsx', 'WIZARD_2016-W32.xlsx', 'WIZARD_2016-W33.xlsx', 'WIZARD_2016-W34.xlsx', 'WIZARD_2016-W35.xlsx', 'WIZARD_2016-W36.xlsx', 'WIZARD_2016-W37.xlsx', 'WIZARD_2016-W38.xlsx', 'WIZARD_2016-W39.xlsx', 'WIZARD_2016-W40.xlsx', 'WIZARD_2016-W41.xlsx', 'WIZARD_2016-W42.xlsx', 'WIZARD_2016-W43.xlsx', 'WIZARD_2016-W44.xlsx', 'WIZARD_2016-W45.xlsx', 'WIZARD_2016-W46.xlsx', 'WIZARD_2016-W47.xlsx', 'WIZARD_2016-W48.xlsx', 'WIZARD_2016-W49.xlsx', 'WIZARD_2016-W50.xlsx'] 

運行選擇在MySQL的作品當然也:

docname         doc_type  
Hotspot Detail 2.0 iBot_20161226.xlsx ibot 
Hotspot Detail 2.0 iBot_20161225.xlsx ibot 
Hotspot Detail 2.0 iBot_20161224.xlsx ibot 
20161226 def.xlsx      def 
20161225 def.xlsx      def 
20161224 def.xlsx      def 
20161223 def.xlsx      def 

我敢肯定,問題不是參數的價值,但請給我一些幫助,瞭解爲什麼同一選擇有時會起作用,而在其他情況下不起作用。 謝謝

+0

還有一點評論。當結果值包含docname中的空格時,似乎會出現問題!運行腳本 –

+0

這種修改工作,但當然不提供所需的結果:查詢=「選擇替換(docname,','_')從imp_doc WHERE doc_type =%s」 –

+0

替換函數並沒有完全解決問題。事實上,如果我用不同於「_」的字符替換空格,我會得到不同的行爲!這看起來非常可怕,並且使整個代碼不可靠。即使使用ORM而不是文本SQL也會產生類似的行爲。這個問題似乎與「mysqlconnector」連接。用「pymysql」代替它似乎提供了一種解決方法,但原始問題依然存在。 –

回答

0

您是否嘗試過使用SQLAlchemy text()函數與您的代碼?

E.g.是這樣的:

from sqlalchemy.sql import text 

query = text("SELECT docname FROM imp_doc WHERE doc_type = :type") 
cur=conn.execute(query, type=doc_type) 

技術上你有什麼看起來有效,但使用text()是用於編寫文本SQL SQLAlchemy的一個最佳實踐,有時你,如果你不使用它(在我的經驗),得到意想不到的行爲。

+0

感謝PJ花時間來解答我的問題。我按照你的建議修改了我的代碼,但代碼仍然沒有完成。無論如何,我將從現在開始使用這種語法來處理文本SQL。在修改後的代碼上應用上面評論過的替換函數可以使查詢的這個版本也起作用。任何想法爲什麼? –