On an INSERT, if the ROWID or INTEGER PRIMARY KEY column is not explicitly given a value, then it will be filled automatically with an unused integer, usually one more than the largest ROWID currently in use. In SQLite, a column with type INTEGER PRIMARY KEY is an alias for the ROWID (except in WITHOUT ROWID tables) which is always a 64-bit signed integer. The AUTOINCREMENT keyword imposes extra CPU, memory, disk space, and disk I/O overhead and should be avoided if not strictly needed. Logging.error('ERROR !!! Not able to connect to Database: ', connect_db_error)Ĭ:\Users\myTechMint\Documents\test-folder\ LOAD_TIMESTAMP = datetime.today().strftime('%Y-%m-%d %H%S')ĭef write_db (source_location, destination_location):Ĭonnection = nnect('database.db')Ĭursor.execute("""CREATE TABLE IF NOT EXISTS database_TABLE (ROWID INTEGER PRIMARY KEY, source_location TEXT, destination_location TEXT, load_time TIMESTAMP)""")ĭb_insert_query = """INSERT INTO 'database_TABLE' ('source_location', 'destination_location', 'load_time') VALUES (?, ?, ?) """ĭb_data = (source_location, destination_location, LOAD_TIMESTAMP) The first version is very fragile - if you ever add, move, or delete columns in your table definition the INSERT will either fail or produce incorrect data (with the values in the wrong columns).įull Code Example of Same: import sqlite3 If you explicitly assign a value of NULL on INSERT, it will get the next auto-increment value.Īlso, you should try to avoid: INSERT INTO people VALUES ("John", "Smith") Īnd use INSERT INTO people (first_name, last_name) VALUES ("John", "Smith") If you explicitly assign a non-NULL value on INSERT, it will get that specified value instead of the auto-increment. ROWID (by whatever name you call it) is assigned a value whenever you INSERT a row, as you would expect. If you include a column of type INTEGER PRIMARY KEY, that column points at (is an alias for) the automatic ROWID column. This is in every SQLite table whether you ask for it or not.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |