sqlite를 사용하여 Excel 시트에서 데이터베이스 만들기, OperationalError : near "Android": 구문 오류

드루

OperationalError : near "Android": 구문 오류가 발생하는데, 지금까지 클래스의 예제에서 이러한 유형의 오류에 대한 언급없이 for 루프 및 format () 메서드를 사용한 이유를 잘 모르겠습니다. 그것을 수정하고 튜플 목록에 대한 내 데이터베이스를 만드는 방법에 대해 잃어 버렸습니다.

import openpyxl
import sqlite3
connection = sqlite3.connect("test1.db")
cursor = connection.cursor()
wb = openpyxl.load_workbook('items.xlsx')
sheet=wb['20190928-items']
savedata=[]
lseg=[] #a list of each rows contents
x=sheet.max_row
y=sheet.max_column
for i in range(1, x+1):
    for j in range(1, y+1):
        av=(sheet.cell(row=i, column=j).value)
        lseg.append(av)

    savedata.append(tuple(lseg))
    lseg=[]

sql_command = """
CREATE TABLE item ( 
asin CHAR(10) PRIMARY KEY, 
brand VARCHAR(20), 
title VARCHAR(30), 
url VARCHAR(80),
image VARCHAR(255),
rating float(1),
reviewUrl VARCHAR(80),
totalreviews int(100),
prices float(2));"""

cursor.execute(sql_command)

for p in savedata:
    format_str = """INSERT INTO item (asin, brand, title, url, image, rating, reviewUrl, totalreviews, prices)
    VALUES ("{asin}", "{brand}", "{title}", "{url}", "{image}", "{rating}", "{reviewUrl}", "{totalreviews}", "{prices}");"""

    sql_command = format_str.format(asin=p[0], brand=p[1], title=p[2], url=p[3], image=p[4], rating=p[5], reviewUrl=p[6], totalreviews=p[7], prices=p[8])
    cursor.execute(sql_command)

connection.commit()
connection.close()
Vsevolod Timchenko

값은 큰 따옴표가 아니라 작은 따옴표 여야합니다.

바꾸다

 VALUES ("{asin}", "{brand}", "{title}", "{url}", "{image}", "{rating}", "{reviewUrl}", "{totalreviews}", "{prices}");

 VALUES ('{asin}', '{brand}', '{title}', '{url}', '{image}', '{rating}', '{reviewUrl}', '{totalreviews}', '{prices}');

편집하다:

또한 더 나은 대안은 따옴표를 버리고 라이브러리가 처리하도록하는 것입니다 (값은 부분적으로 생략 됨).

format_str = """INSERT INTO item (asin, brand)
    VALUES (?, ?);"""
    cursor.execute(sql_command, (p[0], p[1]))

https://docs.python.org/3.5/library/sqlite3.html#sqlite3.Cursor.execute

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관