编码时如何纠正错误消息

问题描述 投票:0回答:1

我正在使用 Linux Mint。当我编写以下代码时,我收到错误“表书没有名为年份的列”消息:

import sqlite3
connection = sqlite3.connect('mydatabase.db')
connection.execute('''
    CREATE TABLE IF NOT EXISTS books(
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                title TEXT,
                author TEXT,
                year INTEGER,
                Remks   TEXT
                )
                ''')
books_data =[
    ("Hunt for Red October", "ABC", 1999,"Good"),
    ("The Monk who sold his Ferrari","DEF",1957,"Excellent"),
    ("Python for Beginners", "GHI",1987,"Standard"),
    ("Learn Python", "KLM", 1857,"Below Par")

]
connection.executemany('INSERT INTO books (title, author, year, Remks) VALUES         (?,?,?)',books_data)
result = connection.execute('SELECT * FROM books')
data = result.fetchall()
for row in data:
    print(f'Title: {row[1]}')
    print(f'Author: {row[2]}')
    print(f'Year: {row[3]}')
    print('')
connection.commit()`

connection.close()

我尝试删除并重新编写代码,但没有成功

python-3.x sqlite
1个回答
0
投票

我做了一些更正。你还需要一个诅咒器:

import sqlite3
connection = sqlite3.connect('mydatabase.db')
connection.execute('''CREATE TABLE IF NOT EXISTS books(
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                title TEXT,
                author TEXT,
                year TEXT,
                remark TEXT);
                ''')
books_data =[
    ("Hunt for Red October", "ABC", "1999","Good"),
    ("The Monk who sold his Ferrari","DEF","1957","Excellent"),
    ("Python for Beginners", "GHI","1987","Standard"),
    ("Learn Python", "KLM", "1857","Below Par")]

sql = 'INSERT INTO books (title, author, year, remark) VALUES (?,?,?,?);'
cur = connection.cursor()
cur.executemany(sql, books_data)
connection.commit()

result = connection.execute('SELECT * FROM books')
data = result.fetchall()
for row in data:
    print(f'Title: {row[0]}')
    print(f'Author: {row[1]}')
    print(f'Year: {row[2]}')
    print(f'Remark: {row[3]}')

connection.close()

输出:

Title: 1
Author: Hunt for Red October
Year: ABC
Remark: 1999
Title: 2
Author: The Monk who sold his Ferrari
Year: DEF
Remark: 1957
Title: 3
Author: Python for Beginners
Year: GHI
Remark: 1987
Title: 4
Author: Learn Python
Year: KLM
Remark: 1857
© www.soinside.com 2019 - 2024. All rights reserved.