JY Tech

[파이썬,sqlite] incorrect number of bindings supplied. the current statement uses 1 and there are 3 supplied 본문

Programming/Error Solution

[파이썬,sqlite] incorrect number of bindings supplied. the current statement uses 1 and there are 3 supplied

J.Dragon 2023. 4. 21. 14:16
cur.execute("SELECT tool_Name FROM Tool WHERE tool_Name=?",(toolName,))

파이썬과 sqlite를 활용한 프로그램을 만들면서 아래와 같은 코드를 작성했는데 

incorrect number of bindings supplied. the current statement uses 1 and there are 3 supplied

 

이런 오류가 발생했다.

 

구글링을 해 보니 해결법은 생각보다 간단했다. 

cur.execute는 튜플을 전달해야 되는데 (toolName)의 형태로 값만 전달해서 그렇단다(데이터 바인딩 오류).

 

튜플로 값을 전달하기 위해 , 만 추가해 주면 된다

 

따라서 코드를 다음과 같이 수정하면 된다.

cur.execute("SELECT tool_Name FROM Tool WHERE tool_Name=?",(toolName,))

 

다시 코드를 실행하면 오류가 해결된 것을 확인할 수 있다.

 

 

참고로 위에서는 ?가 하나여서 전달하는 값이 하나이기 때문에 오류가 발생한 것이고 

cur.execute("SELECT * FROM Book WHERE book_User=? AND book_Phone=? AND book_Tool=? AND book_Count=?",(person, phone, toolName, quantity))

와 같이 전달하는 값이 여러개라면 ,를 추가하지 않아도 된다.

 

 

한줄요약: 파이썬에서 sqlite를 사용하고 ?를 사용하여 쿼리문을 작성할 때 값이 하나만 전달될 경우 끝부분에 ,를 추가해야 한다.