How to overwrite values in a string without increasing id?
1 answer
import sqlite3 category = 1 directians = "Описания" time = 45 img = 'http://..' author = 'author' title = 'terror' c = sqlite3.connect(':memory:') c.executescript(""" CREATE TABLE content(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,title TEXT NOT NULL, category INTEGER NOT NULL, directians TEXT NOT NULL,time INTEGER,img TEXT,author TEXT, datetime DATETIME DEFAULT CURRENT_TIMESTAMP); CREATE UNIQUE INDEX title_x ON content(title); INSERT INTO content(title,category,directians,time,img,author) values('terror',31,'Описания','33','http://','author'); INSERT INTO content(title,category,directians,time,img,author) values('terror2',32,'Описания','33','http://','author') """) for values in [(title,category,directians,time,img,author)]: with c: cur = c.execute('INSERT OR IGNORE INTO content(title,category,directians,time,img,author) ' 'values(?,?,?,?,?,?)', values) if cur.rowcount == 0: # already exists data = dict(zip("title category directians time img author".split(), values)) cur.execute('update content set title=:title, category=:category,directians=:directians,time=:time,img=:img,author=:author ' 'where title=:title', data) print("БД: Данные были заменены!") else: print("БД: Новые данные добавлены.") |
UNIQUE INDEXand why is it needed? - Kill Noise