简体   繁体   中英

sqlite3 python leave a blank column

I created a table with 8 columns, i want to just insert values less than the column number. How can I do that? An answer would be much appreciated.

c.execute("""INSERT INTO tb_transaction VALUES(:empID, :date, :time_in_morning, :time_out_morning, :time_in_afternoon, :time_out_afternoon,:Tardy,:Undertime)""",
                  {--here i want to insert like just the empID, time_in_morning and time_out_morning--})

You should be able to just specify the columns for which you are providing data in the VALUES clause:

INSERT INTO tb_transaction (empID, time_in_morning, time_out_morning)
    VALUES (?, ?, ?)

Your Python code:

c.execute("""INSERT INTO tb_transaction (empID, time_in_morning, time_out_morning) VALUES (:empID, :time_in_morning, :time_out_morning)""")

The above might not work on SQLite (though it certainly works on MySQL). One workaround you could try would be to do an INSERT INTO ... SELECT , with placeholders:

INSERT INTO tb_transaction (empID, time_in_morning, time_out_morning)
SELECT ?, ?, ?;

Here is the Python code for that:

c.execute("""INSERT INTO tb_transaction (empID, time_in_morning, time_out_morning) SELECT :empID, :time_in_morning, :time_out_morning""")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM