sqlite3:从Excel导入时找到哪个是双重记录

发布于 2025-01-25 05:31:10 字数 926 浏览 1 评论 0原文

我正在使用Python从Excel信息导入到我的SQLITE3数据库。我需要避免添加两次相同的记录,但是我还需要知道哪些记录是添加了sceffully的记录,哪些是数据库中已经呈现的记录。这就是我使用熊猫的导入方式:

   def import(self):
       file = filedialog.askopenfilename(filetypes=(("Excel File", "*.csv"), ("All Files","*.*")), title="Select Excel") 

  
       with open(file, "r", encoding='utf-8') as f:
           conn = sqlite3.connect('database/save.db')
       double = []
       ok = []
           c = conn.cursor()
           c.execute("SELECT rowid, * FROM record")
       r = c.fetchall()

           imported = pd.read_csv(f, keep_default_na=False, sep=';')

           imported.to_sql('record', conn, if_exists='replace', index = False)

       for row in r:
       if row in imported:
               double.append(row)
           else:
               ok.append(row)

           conn.commit()

           conn.close()

使用for循环,我能够将重复的行保存到列表中,而不是正确插入的行,这总是空的,

谢谢您的帮助!

I'm using python to import from Excel information to my sqlite3 database. I need to avoid the same record to be added twice, but I'd also need to know which are the records added succeffully and which are the one that were already presented in the database. This is how I'm importing with pandas:

   def import(self):
       file = filedialog.askopenfilename(filetypes=(("Excel File", "*.csv"), ("All Files","*.*")), title="Select Excel") 

  
       with open(file, "r", encoding='utf-8') as f:
           conn = sqlite3.connect('database/save.db')
       double = []
       ok = []
           c = conn.cursor()
           c.execute("SELECT rowid, * FROM record")
       r = c.fetchall()

           imported = pd.read_csv(f, keep_default_na=False, sep=';')

           imported.to_sql('record', conn, if_exists='replace', index = False)

       for row in r:
       if row in imported:
               double.append(row)
           else:
               ok.append(row)

           conn.commit()

           conn.close()

Using the for loop I'm able to save the duplicate row to the list but not the one inserted correctly which is always empty

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文