2015-01-02 3 views
2

안녕하세요. 저는 이와 같은 Python 스크립트가 있습니다. Python 버전 3.4.2에서 실행하고 있습니다.sqlite3.OperationalError : near "CSV1": 구문 오류

import csv 
import sqlite3 

def createTable(cursor, rows, tablename): 
    tableCreated = False 
    for row in rows: 
     if not tableCreated: 
      sql = "CREATE TABLE %s(ROW INTEGER PRIMARY KEY, " + ", ".join(["c%d" % (i+1) for i in range(len(row))]) + ")" 
      cur.execute(sql % tablename) 
      tableCreated = True 
     sql = "INSERT INTO %s VALUES(NULL, " + ", ".join(["'" + c + "'" for c in row]) + ")" 
     cur.execute(sql % tablename) 
    conn.commit() 


conn = sqlite3.connect(":memory:") 
cur = conn.cursor() 

for filename, tablename in [("in1.csv", "CSV1"), ("out1.csv", "CSV2")]: 
    with open(filename, "r") as f: 
     reader = csv.reader(f, delimiter=',') 
     rows = [row for row in reader] 
    createTable(cur, rows, tablename) 

sql = """WITH 
MATCHES AS(SELECT  CSV2.* 
       , CSV1.ROW as ROW_1     
       , CSV1.C4 as C4_1 
       , CSV1.C5 as C5_1 
    FROM  CSV2 
    LEFT JOIN CSV1 
    ON   CSV1.C4 LIKE '%' || CSV2.C2 || '%'  
), 
EXACT AS(CSV1.C4 = CSV1.C5 
    SELECT  * 
    FROM  MATCHES 
    WHERE  C4_1 = C5_1 
), 
MIN_ROW AS(SELECT  C1 
       , min(ROW_1) as ROW_1 
    FROM  MATCHES 
    WHERE  C1 NOT IN (SELECT C1 FROM EXACT) 
    GROUP BY C1, C2, C3, C4, C5     
) 
SELECT  * 
FROM  EXACT 
UNION 
SELECT  MATCHES.* 
FROM  MIN_ROW 
INNER JOIN MATCHES 
ON   MIN_ROW.C1 = MATCHES.C1 
AND   (MIN_ROW.ROW_1 = MATCHES.ROW_1 OR MIN_ROW.ROW_1 IS NULL) 
ORDER BY C1""" 
for row in cur.execute(sql): 
    print (row) 

이 스크립트를 실행하면 내가 지금 꽤 많은 시간 동안이 스크립트 작업을 봤는데 나는 완전히 잃었어요

Traceback (most recent call last): 
    File "script.py", line 55, in <module> 
    for row in cur.execute(sql): 
sqlite3.OperationalError: near "CSV1": syntax error 

나에게 제공합니다. 누군가가 작업 스크립트로 나를 빠져 나올 수 있다면 정말 고맙겠습니다. 아래 샘플 CSV 파일을 찾으십시오.

in1.csv

Homo sapiens,Vertebrate Taxonomy Ontology,direct,Homo sapiens,Homo sapiens,Vertebrate Taxonomy Ontology 
Homo sapiens,Systematized Nomenclature of Medicine - Clinical Terms,direct,Homo sapiens,Homo sapiens,Systematized Nomenclature of Medicine - Clinical Terms 
Homo,Vertebrate Taxonomy Ontology,direct,Homo sapiens,Homo,Vertebrate Taxonomy Ontology 

out1.csv

!Sample_title, !Sample_geo_accession, !Sample_status, !Sample_type, !Sample_source_name_ch1, !Sample_organism_ch1, !Sample_characteristics_ch1, !Sample_characteristics_ch1, !Sample_characteristics_ch1, !Sample_characteristics_ch1, !Sample_characteristics_ch1, !Sample_characteristics_ch1, !Sample_molecule_ch1, !Sample_extract_protocol_ch1, !Sample_label_ch1, !Sample_label_protocol_ch1, !Sample_hyb_protocol, !Sample_scan_protocol, !Sample_description, !Sample_data_processing, !Sample_platform_id 
PBMC_S.aureus_MSSA_INF005, GSM173178, Public on march 16 2007, ribonucleic acid, PBMC_S. aureus, Homo sapiens, Age: 10 years- when sample taken, Gender: male, Race: Hispanic, Illness: Osteomyelitis, Treatment: Cefazolin, Pathogen: S. aureus- MSSA, total ribonucleic acid, RNeasy mini, biotin, Biotinylated complementary rna were prepared according to the standard Affymetrix protocol., Standard Affymetrix protocol., GeneChips were scanned using the Agilent GeneArray 2500 Scanner., The subject was infected with S. aureus- MSSA., The data were analyzed with Microarray Suite version 5.0 (meconium aspiration syndrome 5.0) using Affymetrix default analysis settings and global scaling as normalization method. The trimmed mean target intensity of each array was arbitrarily set to 500., GPL96 
PBMC_S.pneumoniae_INF009, GSM173179, Public on march 16 2007, ribonucleic acid, PBMC_S. pneumoniae, Homo sapiens, Age:4 months- when sample taken, Gender: male, Race: Caucasian, Illness: Abscess, Treatment: Cefazolin, Pathogen: S. pneumoniae, total ribonucleic acid, RNeasy mini, biotin, Biotinylated complementary rna were prepared according to the standard Affymetrix protocol., Standard Affymetrix protocol., GeneChips were scanned using the Agilent GeneArray 2500 Scanner., The subject was infected with S. pneumoniae., The data were analyzed with Microarray Suite version 5.0 (meconium aspiration syndrome 5.0) using Affymetrix default analysis settings and global scaling as normalization method. The trimmed mean target intensity of each array was arbitrarily set to 500., GPL96 
+1

sqlite 오류는 일반적으로 sql 쿼리에 문제가있을 때 발생합니다. 구문 문제가있는 것처럼 보입니다. 특히 "CSV1"인스턴스 근처의 어딘가에 문제가있는 것 같습니다. 가능한 경우 SQL * Plus와 같은 전용 SQL 유틸리티에서 쿼리를 실행 해보십시오. 좀 더 유익한 오류 메시지를 줄 수도 있습니다. – Kevin

+0

답변 해 주셔서 대단히 감사합니다. 저는 SQL을 처음 접했습니다. SQL * Plus 다운로드를 시도했지만 설치 오류가 발생했습니다. 새로운 오류 집합을 디버깅 할 수없는 상황에서 쿼리를 디버깅 할 수 있다면 정말 고맙겠습니다. 나는 진심으로 당신의 이해에 감사드립니다. @ 케빈 – abn

답변

0

데이터베이스는 EXACT의 정의에서 불필요한 CSV1.C4 = CSV1.C5에 대해 불평.

관련 문제