import sqlite3, os

def create_db(path='info_test.db'):
    '''表名:info, 字段:A,B,C'''
    if os.path.exists(path) and os.path.isfile(path):
        os.remove(path)
    conn = sqlite3.connect(path)
    cu = conn.cursor()

    #创建
    create_sql = 'CREATE TABLE info("A" int(2) NOT NULL, "B" int(2) NOT NULL, "C" int(2) NOT NULL);'
    cu.execute(create_sql)
    conn.commit()

    #添加数据
    data_lst = [(1,2,3),
                (4,5,6),
                (7,8,9),
                (1,2,3),
                (4,5,6),
                (0,2,3),
                (0,1,2),
                (4,5,6),
                (7,8,9),
                (1,2,3)]
    insert_sql = 'INSERT INTO info (a, b, c) values (?, ?, ?);'
    cu.executemany(insert_sql, data_lst)
    conn.commit()

    cu.close()
    conn.close()

def update_db(path='info_test.db'):
    '''删除A,B,C均重复数据'''
    if not os.path.exists(path):
        print('文件不存在!')
        return
    conn = sqlite3.connect(path)
    cu = conn.cursor()
    update_sql = 'DELETE FROM info WHERE rowid NOT IN(SELECT MIN(rowid) FROM info GROUP BY a,b,c);'
    cu.execute(update_sql)
    conn.commit()
    cu.close()
    conn.close()

if __name__ == '__main__':
    # create_db()
    update_db()

 

posted on 2021-03-24 23:09  math98  阅读(469)  评论(0)    收藏  举报