【问题标题】:How do i use input variables to locate ids with UPDATE in sqlite3?如何使用输入变量在 sqlite3 中使用 UPDATE 定位 id?
【发布时间】:2015-04-26 02:47:54
【问题描述】:

我正在尝试通过用户输入来管理联系人列表。程序询问用户想要做什么,添加新联系人、编辑联系人、删除联系人或显示联系人列表,如果用户想要编辑联系人,则询问他想要更改的联系人的 ID ,然后是他想要更改的行(姓名、姓氏或数字)。我不知道的是,我如何使用 sql UPDATE 命令以及用户提供的 ID,然后我该如何使用用户使用相同命令给出的新名称、姓氏或数字,是否更新?这样用户就可以从菜单中实际选择要编辑的联系人和要编辑的参数。

这是我的代码。

import sqlite3

asd = True
while asd:
    conn = sqlite3.connect('contactsdb.db')
    c = conn.cursor()
    c.execute('''DROP TABLE IF EXISTS contacts''')
    c.execute('''CREATE TABLE contacts
                  (id INTEGER PRIMARY KEY, Nombre TEXT, Apellido TEXT, Numero de celular INTEGER)''')

    # def agregarconbg():
    #     global nombre
    #     global apellido
    #     global numero

    def agregarcon():
        nombre = input("Add name")
        apellido = input("Add lastname")
        numero = input("Add number")
        # c = conn.cursor()
        c.execute("INSERT INTO contacts (nombre, apellido, numero) VALUES(?,?,?);",(nombre,apellido,numero))
        conn.commit()

    def editnom():
        nuevonom = input("Add new name")
        ###################################################
        ###################################################
        #########What do i do around here?!################
        c.execute("UPDATE contacts set nombre where ID=1",(nuevonom))
        conn.commit
    # def editap():
    #
    # def editnum():

    def editarcon():
        print ("What id do you want to change?")
        cambiar = input("Introduce the id you want to change:")
        print("""What do you want to change
        1 - Name
        2 - Lastname
        3 - Number""")
        cambiarpar = int(input("Seleccion:"))
        if cambiarpar == 1:
            editnom()
        elif cambiarpar == 2:
            editap()
        elif cambiarpar == 3:
            editnum()
        else:
            print("Wrong INPUT")
        # c = conn.cursor()


    #def borrarcon():
    #    c = conn.cursor()
    def printsql():
        # c = conn.cursor()
        c.execute('SELECT * FROM contacts ORDER BY id')
        listT = ["ID","Nombre:","Apellido:","Numero:"]
        k = 0
        for i in c:
            print("\n")
            for j in i:
                print (listT[k])
                print(j)
                if k < 3: k+=1
                else: k = 0
    conn.commit()

    print('Agenda de contactos')
    print('''Que deseas hacer?
    1 - Add a contact
    2 - Edit a contact
    3 - Delete a contact
    4 - Show contacts''')

    seleccion = int(input("Seleccion:"))

    if seleccion == 1:
        agregarcon()
    elif seleccion == 2:
        editarcon()
        conn.commit()
    elif seleccion == 3:
        borrarcon()
        conn.commit
    elif seleccion == 4:
        printsql()

【问题讨论】:

    标签: python-3.x sqlite


    【解决方案1】:

    将 Cambiar(ID) 作为参数传递给 editnom() 并将其用作更新语句的一部分,并使用 nombre = 变量来实际更新记录

    See this page for SQLite3 UPDATE

    See this for how to pass variables through to functions in Python

    def editnom(cambiar): nuevonom = input("Add new name") ################################################### ################################################### #########What do i do around here?!################ c.execute("UPDATE contacts set nombre = ? where ID=?",(nuevonom,cambiar)) conn.commit

    if cambiarpar == 1: editnom(cambiar)

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 2015-03-10
      • 1970-01-01
      • 1970-01-01
      • 2021-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多