2014-07-16 2 views
2

sqlite3을 사용하여 python으로 sqlite 데이터베이스를 만들려고합니다. 여기에 각각 하나의 필드 열만있는 table_names에 지정된 이름으로 새 테이블을 만듭니다. conn.execute('CREATE TABLE {} (quote TEXT PRIMARY KEY NOT NULL);'.format(table_names[i]))quote 파일을 읽는 중입니다. 그러나 나는 insert 문을 실행할 수 없다. 그리고 나는 아래에 내가 시도한 것과 실패한 것을 게시했다. 어떤 도움을 주시면 감사하겠습니다. SQL에 따옴표 모든 문자열이기 때문에 덕분에Python sqlite error

conn.execute('''INSERT INTO {} (quote) VALUES ('{}')'''.format(table_names[i], quote)) 
Error : sqlite3.OperationalError: near "s": syntax error 

conn.execute('''INSERT INTO {} (quote) VALUES ('?')'''.format(table_names[i]), (quote,)) 
Error : sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 0, and there are 1 supplied. 

conn.execute('''INSERT INTO ? (quote) VALUES ('?')''', (table_names[i], quote)) 
Error : sqlite3.OperationalError: near "?": syntax error 
+0

당신이 bar''의 값에 변수'foo'를 지정하려는 경우, 당신은'foo는 = "바"'합니까? –

+0

@ColonelThirtyTwo 실수를했습니다. 감사합니다. – sujithvm

답변

2

매개 변수 마커 ?는 인용하지 않아야합니다.

올바른 형식은 다음과 같습니다

conn.execute('''INSERT INTO {} (quote) VALUES (?)'''.format(table_names[i]), (quote,)) 
+0

하지만 추가하려는 값은 문자열입니다. – Holloway

+1

매개 변수의 * 값 *은 문자열이며 매개 변수 자체는 추상 변수입니다. –

+0

@CL. 고맙습니다 :) – sujithvm