python-3.x
  • postgresql
  • psycopg2
  • 2017-12-07 10 views 0 likes 
    0

    Hi..i 텍스트와 정수 values..my 코드를 모두 잘 작동 보유하고 내 테이블에서 항목을 검색하려고 sqlite3를 데이터베이스에 ... 그러나 이것은 당신이 원하는psycopg2.Data 오류 : 정수에 대한 잘못된 입력 구문 ""

    import psycopg2 
    class database: 
    
        def __init__(self): 
    
         self.con=psycopg2.connect("dbname='book_store' user='postgres' password='5283' host='localhost' port='5432' ") 
         self.cur=self.con.cursor() 
         self.cur.execute("CREATE TABLE if not exists books(id SERIAL PRIMARY KEY,title TEXT NOT NULL UNIQUE,author TEXT NOT NULL,year integer NOT NULL,isbn integer NOT NULL UNIQUE)") 
         self.con.commit() 
    
        def insert(self,title,author,year,isbn): 
         try: 
         self.cur.execute("INSERT INTO books(title,author,year,isbn) VALUES(%s,%s,%s,%s)",(title,author,year,isbn)) 
         self.con.commit() 
         except: 
          #print("already exists..") 
          pass 
    
        def view(self): 
         self.cur.execute("SELECT * FROM books") 
         rows=self.cur.fetchall() 
         print(rows) 
    
        def search(self,title="",author="",year="",isbn=""): 
         self.cur.execute("SELECT * FROM books WHERE title=%s or author=%s or year=%s or isbn=%s",(title,author,year,isbn)) 
         row=self.cur.ferchall() 
         print(row) 
    
    db=database() 
    #db.insert("The Naughty","AparnaKumar",1995,234567654) 
    db.view() 
    db.search(year=1995) 
    

    psycopg2.Data Error: invalid input syntax for integer:" "

    +0

    공백 문자열이 아닌 null로 isbn 전달 –

    +0

    ISBN은 길이가 10 또는 13 자이며 0으로 시작할 가능성이있는 10 자리 버전입니다. 당신의 테이블에있는 데이터 타입에 대해'integer'를 사용하는 것은 좋은 생각입니다. – Nicarus

    답변

    1

    .. PostgreSQL 데이터베이스에 데이터 오류가 발생합니다 :

    def search(self, title=None, author=None, year=None, isbn=None): 
        self.cursor.execute(""" 
         select * 
         from books 
         where 
          (title = %(title)s or %(title)s is null) 
          and 
          (author = %(author)s or %(author)s is null) 
          and 
          (year = %(year)s or %(year)s is null) 
          and 
          (isbn = %(isbn)s or %(isbn)s is null) 
         """, 
         {'title': title, 'author': author, 'year': year, 'isbn': isbn} 
        ) 
    
    +0

    그럴 경우 문제가 해결 될 수 있지만 그 이유는 무엇일까요? 그렇지 않은 경우 코드 전용 답변입니다. – Nicarus

    +0

    도움을 주셔서 감사합니다 ... 작동합니다. – kiruthika

     관련 문제

    • 관련 문제 없음^_^