2017-05-20 3 views
0

이것은 내 프로그램입니다. 하여 구문 오류가 정확히 어디에 그것은 나에게 말한다, 그러나 나는 말 그대로 문제가 무엇인지 아무 생각이 없다 :구문 오류 알아낼 수 없습니까? python3

import sqlite3 

def create_table(dbName, table_name, sql): 
    with sqlite3.connect("ATM.db") as db: 
     cursor = db.cursor() 
     cursor.execute("select name from sqlite_master where name=?",(table_name,)) 
     result = cursor.fetchall() 
     keep_table = True 
     if len(result) == 1: 
      response = input("The table{0} already exists, do you wish to recreate? (y/n) \n".format(table_name)) 
      if response.lower() == "y": 
       keep_table = False 
       print("the {0} table will be recreated".format(table_name)) 
       cursor.execute("drop table if exists {0}".format(table_name)) 
       db.commit() 
       insert_data() 
      else: 
       print("The existing table was kept") 
       insert_data() 
     else: 
      keep_table = False 
     if not keep_table: 
>>>>>>  cursor.execute(sql) #Problem is supposedly here? 
      db.commit() 
      insert_data() 

def insert_data(): 
    with sqlite3.connect("ATM.db") as db: 
     cursor = db.cursor() 
     sql = """insert into ATM(Title, First Name, Surname, Balance) values (Mr., Jeremy, Clarkson, 172.16), 
                      (Miss, Suzanne, Perry, 15.62)""" 
     cursor.execute(sql, values) 
     db.commit() 


dbName = "ATM.db" 
sql = """ create table ATM 
      (CustomerID integer, 
      Title text 
      First Name text, 
      Surname text, 
      Balance real, 
      CustomerID(CustomerID))""" 
create_table(dbName, "ATM", sql) 

이것은 내가 무엇입니까 구문 오류 메시지입니다. 코드를 강조하는 부분에 화살표를 추가했습니다.

line 23, in create_table 
    cursor.execute(sql) 
sqlite3.OperationalError: near "(": syntax error 

답변

1

오류는 Python이 아닌 SQL 코드에 있습니다.

몇 가지 :

  1. 세미콜론으로 sqlite3 모든 명령을 종료해야합니다, 그렇지 않으면 그것은 단지 하나의 명령으로 읽는 유지합니다. 그로 인해 오류가 어디에 있었는지 혼란스러워졌습니다.
  2. 세미콜론을 추가하면 실제로 문제가 CREATE TABLE 명령에 있음을 알 수 있습니다. 이 라인은 의미가 없습니다 : "CustomerID(CustomerID))". PRIMARY KEY을 설정하려 했습니까?

    create table ATM (
        CustomerID integer, 
        Title text 
        First Name text, 
        Surname text, 
        Balance real, 
        PRIMARY KEY(CustomerID) 
    ); 
    

은 덧붙여, 당신은 또 다른 문제가있다.

insert into ATM(Title, First Name, Surname, Balance) values ("Mr.", "Jeremy", "Clarkson", 172.16), 
                     ("Miss", "Suzanne", "Perry", 15.62); 

을 그렇지 않으면 :

insert into ATM(Title, First Name, Surname, Balance) values (Mr., Jeremy, Clarkson, 172.16), 
                     (Miss, Suzanne, Perry, 15.62) 

문제는 오히려 그들에게로 베어 단어를 치료하고 희망보다, 따옴표에 문자열을 포장 할 필요가 제대로 해석 될 것이라는 점이다 이 당신의 쿼리입니다 ,이 단어는 잘못된 단어로 인해 잘못된 형식으로 표시됩니다.

+0

나는 여전히 같은 구문 오류를 얻고 있습니다. 나는 그 쿼리에서 내 실수를 깨닫고 이미 변경했습니다! 심지어 전체 삽입 데이터 함수를 제거하여 그것을 테스트하고 cursor.execute (SQL)가리키는 동일한 구문 오류가 있습니다. sqlite3.OperationalError : 근처에 "(": 구문 오류 –

+1

수정 한 고정 된. –

+0

도와 주셔서 감사합니다. 좋은 하루 되세요! –