与Sqlite3一样真实

时间:2016-12-04 14:05:35

标签: python sqlite

我有一些问题,要在sqlite3中发送数据。这是我的代码:

date = raw_input("Enter date: ")

f_name = []
s_name = []

def enter():
    while True:
        f = raw_input("Enter name: ")
        e = raw_input("Enter second name: ")

        if len(f) == 0:
            break
        elif len(e) == 0:
            break
        else:
            a = f
            f_name.append(a)
            y = e
            s_name.append(y)

    zipped = zip(f_name, s_name)

    c.executemany("INSERT INTO test_table(first_name, "
              "second_name, date) VALUES(?, ?, ?)",
              (zipped, date))
    conn.commit()  

create_table()
enter()
conn.close()

只有我需要的是,虽然True循环必须工作直到停止,并且在循环的每次迭代中,发送到数据:名字,名字和日期。

1 个答案:

答案 0 :(得分:0)

您需要在while中使用其他2个while True循环,请查看以下代码:

my_date = raw_input("Enter date: ")

f_name = []
s_name = []

def enter():
    while True:
        fn = raw_input("Enter name: ")
        while not fn:  # You do not need to use len(fn) == 0
            fn = raw_input("Enter name: ")

        sn = raw_input("Enter second name: ")
        while not sn:
            sn = raw_input("Enter second name: ")

        f_name.append(fn)
        s_name.append(sn)

    zipped = zip(f_name, s_name)
    zipped = [item + (my_date,) for item in zipped]  # You need to my_date to each item in zipped

    c.executemany("""
        INSERT INTO test_table ('first_name', 'second_name', 'date')
        VALUES(?, ?, ?)""", zipped)
    # ...