为什么我的SQL搜索在Python中不起作用?

发布于 2025-02-01 02:30:52 字数 4307 浏览 0 评论 0原文

本质上,我一直在做SQL作为计算机科学GCSE的一部分,其中一项任务是在Python中练习SQL。在这样做的时候,我已经撞到了砖墙,从某种意义上说我的程序。试图解决它是一场真正的努力,我希望这里有人可以帮助我。我添加了完整的代码。主要问题是,当我运行它时,它将返回或仅[]。也许有人能够提供更干净的答案。该标准是通过Python在数据库上添加记录,更新记录,删除记录并执行查询。

import sqlite3, os


def add():
    c.execute('CREATE TABLE Movie_Collection(Name text, Released text, Genre text, Rating text)')

    c.execute('INSERT INTO Movie_Collection VALUES("Top Gun", "1986", "Action", "6.9")')
    c.execute('INSERT INTO Movie_Collection VALUES("Bee Movie", "2007", "Comedy", "6.1")')
    c.execute('INSERT INTO Movie_Collection VALUES("Hotel Rwanda", "2004", "Biography", "8.1")')
    c.execute('INSERT INTO Movie_Collection VALUES("Pirates of the Carribean: Curse of the Black Pearl", "2003", "Adventure", "8.0")')
    
    ndb.commit()


def input1():
    print('****MENU****')
    print('''Enter 1 to add record
Enter 2 to find item
Enter 3 to update records
Enter 4 to delete records
''')
    
    var = int(input('Enter number: '))
    print(' ')

    #adding
    if var == 1:
        name = str(input('Movie name: '))
        real = int(input('Release date: '))
        genre = str(input('Genre: '))
        rating = float(input('Rating: '))
        c.execute('INSERT INTO Movie_Collection VALUES(?, ?, ?, ?)', (name, real, genre, rating))
        ndb.commit()

    #searching
    elif var == 2:
        query_type = str(input('Query Type (name, release, genre, rating): '))
        
        if query_type.lower() == 'name':
            name_q =  input('Enter keyword: ')
            c.execute('SELECT * FROM Movie_Collection WHERE Name = ?', (name_q,))
            row = c.fetchall()
            print(row)
            ndb.commit()
                
        elif query_type.lower() == 'release':
            real_q = input('Enter keyword: ')
            c.execute('SELECT * FROM Movie_Collection WHERE Released = ?', (real_q,))
            row = c.fetchall()
            print(row)
            ndb.commit()
                
        elif query_type.lower() == 'genre':
            genre_q = str(input('Enter keyword: '))
            c.execute('SELECT * FROM Movie_Collection WHERE Genre = ?', (genre_q,))
            row = c.fetchall()
            print(row)
            ndb.commit()
                
        elif query_type.lower() == 'rating':
            rate_q = input('Enter keyword: ')
            c.execute('SELECT * FROM Movie_Collection WHERE Rating = ?', (rate_q,))
            row = c.fetchall()
            print(row)
            ndb.commit()

    #Updating
    elif var == 3:
        record_q1 = str(input('Enter the name of the record you would like to update: '))
        print('''1. Release date
2. Genre
3. Rating
''')
        record_q2 = int(input('What field do you want to update? (1, 2 or 3) '))
        if record_q2 == 1:
            new_date = input('Enter a new release date: ')
            c.execute('UPDATE Movie_Collection SET Released = ? WHERE Name = ?', (new_date, record_q1))
            ndb.commit()
        elif record_q2 == 2:
            new_genre = input('Enter a new genre: ')
            c.execute('UPDATE Movie_Collection SET Genre = ? WHERE Name = ?', (new_genre, record_q1))
            ndb.commit()
        elif record_q2 == 3:
            new_rating = input('Enter a new rating: ')
            c.execute('UPDATE Movie_Collection SET Rating = ? WHERE Name = ?', (new_rating, record_q1))
            ndb.commit()
        

    #Deleting
    elif var == 4:
        record_q3 = input('Enter the name of the record you would like to delete: ')
        c.execute('DELETE FROM Movie_Collection WHERE Name = ?', (record_q3,))
        ndb.commit()


if not os.path.isfile('MoviesCollections.db'):
    ndb = sqlite3.connect('MovieCollections.db')
    c = ndb.cursor()

    add()
    run = True
    while run == True:
        input1()
        print(' ')
        ask = str(input('Do u want to continue? '))
        if ask.lower() == 'n' or ask.lower() == 'no':
            run = False
        else:
            run = True
        print(' ')
    ndb.commit()
    ndb.close()

这是我运行程序时发生的情况 即使记录没有调整而没有调整

Essentially, I've been doing SQL as a part of my Computer Science GCSE, and one task was to practice SQL in Python. Whilst doing so, I have hit a brick wall in the sense that the search for my program. It's been a real struggle trying to fix it, and I was hoping someone here could help me out. I've added in the full code. The main issue is that when I run it, it returns either none or just []. Maybe someone is able to provide a cleaner answer. The criteria is to add records, update records, delete records and perform queries on the database through Python.

import sqlite3, os


def add():
    c.execute('CREATE TABLE Movie_Collection(Name text, Released text, Genre text, Rating text)')

    c.execute('INSERT INTO Movie_Collection VALUES("Top Gun", "1986", "Action", "6.9")')
    c.execute('INSERT INTO Movie_Collection VALUES("Bee Movie", "2007", "Comedy", "6.1")')
    c.execute('INSERT INTO Movie_Collection VALUES("Hotel Rwanda", "2004", "Biography", "8.1")')
    c.execute('INSERT INTO Movie_Collection VALUES("Pirates of the Carribean: Curse of the Black Pearl", "2003", "Adventure", "8.0")')
    
    ndb.commit()


def input1():
    print('****MENU****')
    print('''Enter 1 to add record
Enter 2 to find item
Enter 3 to update records
Enter 4 to delete records
''')
    
    var = int(input('Enter number: '))
    print(' ')

    #adding
    if var == 1:
        name = str(input('Movie name: '))
        real = int(input('Release date: '))
        genre = str(input('Genre: '))
        rating = float(input('Rating: '))
        c.execute('INSERT INTO Movie_Collection VALUES(?, ?, ?, ?)', (name, real, genre, rating))
        ndb.commit()

    #searching
    elif var == 2:
        query_type = str(input('Query Type (name, release, genre, rating): '))
        
        if query_type.lower() == 'name':
            name_q =  input('Enter keyword: ')
            c.execute('SELECT * FROM Movie_Collection WHERE Name = ?', (name_q,))
            row = c.fetchall()
            print(row)
            ndb.commit()
                
        elif query_type.lower() == 'release':
            real_q = input('Enter keyword: ')
            c.execute('SELECT * FROM Movie_Collection WHERE Released = ?', (real_q,))
            row = c.fetchall()
            print(row)
            ndb.commit()
                
        elif query_type.lower() == 'genre':
            genre_q = str(input('Enter keyword: '))
            c.execute('SELECT * FROM Movie_Collection WHERE Genre = ?', (genre_q,))
            row = c.fetchall()
            print(row)
            ndb.commit()
                
        elif query_type.lower() == 'rating':
            rate_q = input('Enter keyword: ')
            c.execute('SELECT * FROM Movie_Collection WHERE Rating = ?', (rate_q,))
            row = c.fetchall()
            print(row)
            ndb.commit()

    #Updating
    elif var == 3:
        record_q1 = str(input('Enter the name of the record you would like to update: '))
        print('''1. Release date
2. Genre
3. Rating
''')
        record_q2 = int(input('What field do you want to update? (1, 2 or 3) '))
        if record_q2 == 1:
            new_date = input('Enter a new release date: ')
            c.execute('UPDATE Movie_Collection SET Released = ? WHERE Name = ?', (new_date, record_q1))
            ndb.commit()
        elif record_q2 == 2:
            new_genre = input('Enter a new genre: ')
            c.execute('UPDATE Movie_Collection SET Genre = ? WHERE Name = ?', (new_genre, record_q1))
            ndb.commit()
        elif record_q2 == 3:
            new_rating = input('Enter a new rating: ')
            c.execute('UPDATE Movie_Collection SET Rating = ? WHERE Name = ?', (new_rating, record_q1))
            ndb.commit()
        

    #Deleting
    elif var == 4:
        record_q3 = input('Enter the name of the record you would like to delete: ')
        c.execute('DELETE FROM Movie_Collection WHERE Name = ?', (record_q3,))
        ndb.commit()


if not os.path.isfile('MoviesCollections.db'):
    ndb = sqlite3.connect('MovieCollections.db')
    c = ndb.cursor()

    add()
    run = True
    while run == True:
        input1()
        print(' ')
        ask = str(input('Do u want to continue? '))
        if ask.lower() == 'n' or ask.lower() == 'no':
            run = False
        else:
            run = True
        print(' ')
    ndb.commit()
    ndb.close()

This is what happens when I run the program
Even though the records are there without adjustment

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文