且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在使用python检查是否存在的同时在sqlite3表中插入数据

更新时间:2023-01-22 08:17:23

对于第一个问题(ids 不加 1):我删除了 usrIDurlID 并使用rowid 代替

For the first problem (ids not incremented by 1) : i removed usrID and urlID and used the rowid instead

curr.executescript('''
CREATE TABLE IF NOT EXISTS USERS ( 

chatID INT(10) UNIQUE,

);
CREATE TABLE IF NOT EXISTS URLS ( 

linkID TEXT UNIQUE 

);
CREATE TABLE IF NOT EXISTS Receive ( 
USER_ID INTEGER ,
URL_ID INTEGER ,
PRIMARY KEY (USER_ID , URL_ID)

)

''')

固定代码:

curr.execute('SELECT rowid FROM USERS WHERE chatID = ? ', (userid , ))
id1 = curr.fetchone()[0]

curr.execute('SELECT rowid FROM URLS WHERE linkID = ? ', (urlId , ))
id1 = curr.fetchone()[0]

对于第二个问题(检查用户 ID 和网址):

For the second problem (checking for userID and the urls) :

        try:
            curr.execute('INSERT INTO Receive (USER_ID , URL_ID) VALUES (? , ?)' ,(id1 , id2))
            send_pic(subm.url , subm.title)
            time.sleep(1.5)
            connection.commit()

        except sqlite3.IntegrityError as e:
            ...
            print('Anything else')