如何通过tkinter接受来自用户的凭据来连接mongodb

gcxthw6b  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(355)
from pymongo import MongoClient
client = MongoClient ('localhost', 27017)
db_label = Label (frame3, text="Database 
                   Name:",padx=5,pady=10).grid(row=1,column=1, sticky=E)
db_name = Entry (frame3, bd=5)
db_name.grid (row=1, column=2)
coll_label = Label (frame3, text="Collection Name:", padx=5, pady=10).grid 
              (row=2, column=1, sticky=E)
coll_name = Entry (frame3, bd=5)
coll_name.grid (row=2, column=2)
db = db_name.get()
collection = coll_name.get()
create_coll = Button (frame3, text="Create Mongo Collection", padx=5, 
pady=10)
create_coll.grid (row=4, column=3)            

db.collection.insertmany(b)

如何通过接受来自tkinterui的凭据来形成连接uri?

ozxc1zmp

ozxc1zmp1#

我没有mongo数据库来测试它,但是在检查了pymongo教程之后,我认为您需要类似的代码。
你必须把函数分配给按钮,在这个函数中你可以从中得到字符串 Entry 你必须用它作为钥匙 client["database-name"]["collection-name"] 编辑:再次检查教程后,我看到应该有 insert_many 而不是 insertmany 但是我没有mongodb来检查它是否解决了所有的问题。

import tkinter as tk
from pymongo import MongoClient

# --- functions ---

def create():
    db = db_name.get()
    collection = coll_name.get()
    client[db][collection].insert_many("new data")

# --- main ---

root = tk.Tk()

client = MongoClient('localhost', 27017)

l = tk.Label(root, text="Database Name:")
l.grid(row=1,column=1)

l = tk.Label(root, text="Collection Name:")
l.grid(row=2, column=1)

db_name = tk.Entry(root)
db_name.grid(row=1, column=2)

coll_name = tk.Entry(root)
coll_name.grid(row=2, column=2)

b = tk.Button(root, text="Create Mongo Collection", command=create)
b.grid(row=4, column=3)            

root.mainloop()

相关问题