Repeat a command while true or x times (equivalent of while/for loop)

前端 未结 2 1837
野趣味
野趣味 2021-01-28 22:04

I would like to repeat this command as many times as there is still sometextin the field note (several rows from the table itemNotes could

2条回答
  •  遇见更好的自我
    2021-01-28 22:55

    As recommended by Stephan in his last comment, I used python to do this.

    Here is my code :

    import sqlite3
    import re
    keyword = "sometext"
    replacement = "abc"
    
    db = sqlite3.connect(path_to_sqlite)
    cursor = db.cursor()
    
    cursor.execute(f'SELECT * FROM itemNotes  WHERE note like "%{keyword}%"')
    
    for row in cursor.fetchall():
        row_regex = re.compile(re.escape(keyword), re.IGNORECASE)
        row_regex_replaced = row_regex.sub(replacement, row[2])
    
        rowID = row[0]
        sql = "REPLACE INTO itemNotes (itemID,note) VALUES (?,?)"
        data = (rowID, row_regex_replaced)
        cursor.execute(sql, data)
        db.commit()
    

提交回复
热议问题