Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SQLite UPDATE命令不更新数据库_Python_Sqlite_Sql Update - Fatal编程技术网

Python SQLite UPDATE命令不更新数据库

Python SQLite UPDATE命令不更新数据库,python,sqlite,sql-update,Python,Sqlite,Sql Update,我对SQLite、python和编程非常陌生,我正在尝试创建一个家庭媒体服务器数据库。我正在尝试编写一个程序,将新条目添加到我创建的数据库中,并允许我编辑现有条目。虽然我可以添加条目,但更新特定条目是另一回事。作为一个例子,我想能够更新一个特定的电影标题,以一个新的,而我不知道它的id之前手。我的程序中导致问题的代码部分如下: def editMedia(): while True: print('Catagories: title, genre, type, direc

我对SQLite、python和编程非常陌生,我正在尝试创建一个家庭媒体服务器数据库。我正在尝试编写一个程序,将新条目添加到我创建的数据库中,并允许我编辑现有条目。虽然我可以添加条目,但更新特定条目是另一回事。作为一个例子,我想能够更新一个特定的电影标题,以一个新的,而我不知道它的id之前手。我的程序中导致问题的代码部分如下:

def editMedia():
    while True:
        print('Catagories: title, genre, type, director, length, rating')
        editValue = input('What would like to edit?: ')
        if editValue not in ('title', 'Title', 'genre', 'Genre', 'type',
                        'Type', 'director', 'Director', 'length', 'Length',
                        'rating', 'Rating'):
            print('Invalid entry: Please choose one of the catagories')
            continue
        elif editValue in ('title', 'Title'):
            while True:
                oldTitle = input('Enter old title: ')
                if len(oldTitle) < 1:
                    print('Invalid entry: Old title must have a value.')
                    continue
                else:
                    while True:
                        updateTitle = input('Enter new title: ')
                        if len(updateTitle) < 1:
                            print('Invalid entry: New title must have a value.')
                            continue
                        else:
                            break
                break
                cur.execute('''UPDATE Movie SET title = ? WHERE title = ?''', (updateTitle, oldTitle))
        print("Record Updated successfully ")
        break

    conn.commit()
当我尝试更新特定的电影标题时,程序运行时不会出错,但当我使用DB Browser for SQLite检查数据库以确认标题已更新时,我发现没有任何更改


我做错了什么?我所追求的是可能的吗?

我刚刚在笔记本电脑上运行了这个程序,可以看出您有一些问题。还有很多可能的改进,但我相信随着你技能的提高,这些都会随着时间的推移而发生

我匆忙做了一些基本的改变,但这是可行的,我将在下面解释我的发现

def editMedia():
    while True:
        print('Catagories: title, genre, type, director, length, rating')
        editValue = input('What would like to edit?: ').lower()
        if editValue not in ('title', 'genre', 'type', 'director', 'length', 'rating'):
            print('Invalid entry: Please choose one of the catagories')
            continue
        elif editValue == 'title':
            while True:
                oldTitle = input('Enter old title: ')
                if len(oldTitle) < 1:
                    print('Invalid entry: Old title must have a value.')
                    continue
                else:
                    while True:
                        updateTitle = input('Enter new title: ')
                        if len(updateTitle) < 1:
                            print('Invalid entry: New title must have a value.')
                            continue
                        else:
                            break
                conn = sqlite3.connect('mediadb.sqlite')
                cur = conn.cursor()
                cur.execute('''UPDATE Movie SET title = ? WHERE title = ?''', (updateTitle, oldTitle))
                cur.close()
                conn.commit()
                print("Record Updated successfully ")
                break
        break
def editMedia():
尽管如此:
印刷品(‘类别:标题、流派、类型、导演、长度、评级’)
editValue=input('要编辑什么?:')。lower()
如果editValue不在(‘标题’、‘流派’、‘类型’、‘导演’、‘长度’、‘评级’)中:
打印('无效条目:请选择一个类别')
持续
elif editValue==“标题”:
尽管如此:
oldTitle=输入('输入旧标题:')
如果len(旧标题)<1:
打印('无效条目:旧标题必须有值')
持续
其他:
尽管如此:
updateTitle=input('输入新标题:')
如果len(updateTitle)<1:
打印('无效条目:新标题必须有值')
持续
其他:
打破
conn=sqlite3.connect('mediadb.sqlite')
cur=连接光标()
当前执行(''更新电影集标题=?其中标题=?'',(更新标题,旧标题))
当前关闭()
康涅狄格州提交
打印(“记录更新成功”)
打破
打破
  • 对用户输入的要编辑的内容调用lower(),意味着您不必担心大写
  • 在更新命令之前,您正在调用break,因此脚本从未到达那里
  • 即使在解决了这个问题之后,您仍然试图在关闭的光标上执行一个命令,这会导致编程错误;我将再次为您打开它,然后运行update命令,然后关闭并提交

非常感谢您的帮助!我现在已经让它工作了,看着它做我想做的事情感觉真好。@holycow-不客气,伙计!欢迎来到开发者的生活;最值得骄傲的时刻是看到我们的创作栩栩如生。如果我的回答有帮助,请将其标记为已接受,以结束问题的循环
def editMedia():
    while True:
        print('Catagories: title, genre, type, director, length, rating')
        editValue = input('What would like to edit?: ').lower()
        if editValue not in ('title', 'genre', 'type', 'director', 'length', 'rating'):
            print('Invalid entry: Please choose one of the catagories')
            continue
        elif editValue == 'title':
            while True:
                oldTitle = input('Enter old title: ')
                if len(oldTitle) < 1:
                    print('Invalid entry: Old title must have a value.')
                    continue
                else:
                    while True:
                        updateTitle = input('Enter new title: ')
                        if len(updateTitle) < 1:
                            print('Invalid entry: New title must have a value.')
                            continue
                        else:
                            break
                conn = sqlite3.connect('mediadb.sqlite')
                cur = conn.cursor()
                cur.execute('''UPDATE Movie SET title = ? WHERE title = ?''', (updateTitle, oldTitle))
                cur.close()
                conn.commit()
                print("Record Updated successfully ")
                break
        break