相关文章推荐
火星上的紫菜汤  ·  LXD ...·  3 周前    · 
怕老婆的大熊猫  ·  C++ string 类详解 - ...·  2 月前    · 
彷徨的斑马  ·  Platform.OnRequestPerm ...·  9 月前    · 

无法使用python在mongodb中插入数据

1 人关注

我正在学习用Python连接mongoDB,遇到了这个错误。

Traceback (most recent call last):
  File "d:/My Documents/Programming/Intaf/Production Register/Retrieve_Data.py", line 93, in <module>
    Submit = Button(root, text = "SUBMIT", command = goMongo()).place(x = .5*900, y = .30*screenHeight)
  File "d:/My Documents/Programming/Intaf/Production Register/Retrieve_Data.py", line 89, in goMongo
    information.insert_one(record1)
  File "d:\Download\anaconda pygame\lib\site-packages\pymongo\collection.py", line 698, in insert_one
    self._insert(document,
  File "d:\Download\anaconda pygame\lib\site-packages\pymongo\collection.py", line 613, in _insert
    return self._insert_one(
  File "d:\Download\anaconda pygame\lib\site-packages\pymongo\collection.py", line 602, in _insert_one
    self.__database.client._retryable_write(
  File "d:\Download\anaconda pygame\lib\site-packages\pymongo\mongo_client.py", line 1498, in _retryable_write
    return self._retry_with_session(retryable, func, s, None)
  File "d:\Download\anaconda pygame\lib\site-packages\pymongo\mongo_client.py", line 1384, in _retry_with_session
    return self._retry_internal(retryable, func, session, bulk)
  File "d:\Download\anaconda pygame\lib\site-packages\pymongo\mongo_client.py", line 1416, in _retry_internal
    return func(session, sock_info, retryable)
  File "d:\Download\anaconda pygame\lib\site-packages\pymongo\collection.py", line 590, in _insert_command
    result = sock_info.command(
  File "d:\Download\anaconda pygame\lib\site-packages\pymongo\pool.py", line 699, in command
    self._raise_connection_failure(error)
  File "d:\Download\anaconda pygame\lib\site-packages\pymongo\pool.py", line 683, in command
    return command(self, dbname, spec, slave_ok,
  File "d:\Download\anaconda pygame\lib\site-packages\pymongo\network.py", line 120, in command
    request_id, msg, size, max_doc_size = message._op_msg(
  File "d:\Download\anaconda pygame\lib\site-packages\pymongo\message.py", line 714, in _op_msg
    return _op_msg_uncompressed(
bson.errors.InvalidDocument: cannot encode object: <tkinter.StringVar object at 0x000001F9329D3B50>, of type: <class 'tkinter.StringVar'>

here is the code:

from tkinter import * 
import pymongo
client = pymongo.MongoClient('mongodb://127.0.0.1:27017/')
mydb = client['FirstContact']
information = mydb.studentinfo
root = Tk()
screenWidth = root.winfo_screenwidth()  
screenHeight = root.winfo_screenheight()
screen_resolution = "900x500"
root.geometry(screen_resolution)
Names = ["Shlok", 
         "Sumit", 
         "Srishti", 
         "Swati"]
clicked1 = StringVar()
clicked1.set("Choose the students name")
clicked2 = StringVar()
clicked2.set("Choose the students name")
clicked3 = StringVar()
clicked3.set("Choose the students name")
clicked4 = StringVar()
clicked4.set("Choose the students name")
mainframe = Frame(root, bg = "green", bd = 2, width = screenWidth, height = screenHeight).pack(side = TOP)
DropDown1 = OptionMenu(root, clicked1, *Names).place(x = .01*screenWidth, y = .02*screenHeight)
DropDown2 = OptionMenu(root, clicked2, *Names).place(x = .15*screenWidth, y = .02*screenHeight)
DropDown3 = OptionMenu(root, clicked3, *Names).place(x = .29*screenWidth, y = .02*screenHeight)
DropDown4 = OptionMenu(root, clicked4, *Names).place(x = .43*screenWidth, y = .02*screenHeight)
Subs = ["Physics", "English", "Hindi", "Chemistry"]
SubSel = StringVar()
SubSel.set("Which Subject?")
SubDown = OptionMenu(root, SubSel, *Subs).place(x = .01*screenWidth, y = .09*screenHeight)
MarkName1 = StringVar()
MarkName1.set("The marks recieved")
MarkName2 = StringVar()
MarkName2.set("The marks recieved")
MarkName3 = StringVar()
MarkName3.set("The marks recieved")
MarkName4 = StringVar()
MarkName4.set("The marks recieved")
Mark1 = Entry(root, width = 30, textvariable = MarkName1).place(x = .01*screenWidth, y = .16*screenHeight)
Mark2 = Entry(root, width = 30, textvariable = MarkName2).place(x = .15*screenWidth, y = .16*screenHeight)
Mark3 = Entry(root, width = 30, textvariable = MarkName3).place(x = .29*screenWidth, y = .16*screenHeight)
Mark4 = Entry(root, width = 30, textvariable = MarkName4).place(x = .43*screenWidth, y = .16*screenHeight)
Grades = ["A", "B", "C", "D", "E"]
Grade1 = StringVar()
Grade1.set("Grade")
Grade2 = StringVar()
Grade2.set("Grade")
Grade3 = StringVar()
Grade3.set("Grade")
Grade4 = StringVar()
Grade4.set("Grade")
GradeDown1 = OptionMenu(root, Grade1, *Grades).place(x = .01*screenWidth, y = .23*screenHeight)
GradeDown2 = OptionMenu(root, Grade2, *Grades).place(x = .15*screenWidth, y = .23*screenHeight)
GradeDown3 = OptionMenu(root, Grade3, *Grades).place(x = .29*screenWidth, y = .23*screenHeight)
GradeDown4 = OptionMenu(root, Grade4, *Grades).place(x = .43*screenWidth, y = .23*screenHeight)
def goMongo():
    record1 = {'Name': clicked1,
                'Subject': SubSel,
                'Marks': MarkName1,
                'Grade': Grade1}
    information.insert_one(record1)
Submit = Button(root, text = "SUBMIT", command = goMongo()).place(x = .5*900, y = .30*screenHeight)
root.mainloop()

如果没有插入代码,它可以正常工作,但当我在代码中添加数据时,它显示了这个愚蠢的错误。 谢谢你的任何帮助。

5 个评论
Show the 全程跟踪 .这个错误对我们任何一个人来说都很难单独理解。
对不起,忘了这么做了,给你。
这并不是 整个追踪记录 .这只是错误信息。把出现在你的控制台的所有东西,以字开头的 回溯 并以信息结束。即使它非常长,而且你不知道它怎么可能有帮助。
这就是全部内容
You should use .get() on those StringVar inside goMongo() .
python
mongodb
tkinter
pymongo
Shlok
Shlok
发布于 2021-01-23
2 个回答
Belly Buster
Belly Buster
发布于 2021-01-23
已采纳
0 人赞同

你的变量含有MongoDB不理解的 tkinter.StringVar 类型的对象。

你可以通过在插入数据库之前将它们转换为字符串来轻松解决这个问题,比如说

def goMongo():
    record1 = {'Name': str(clicked1),
               'Subject': str(SubSel),
               'Marks': str(MarkName1),
               'Grade': str(Grade1)}
    information.insert_one(record1)
    
acw1668
acw1668
发布于 2021-01-23
0 人赞同

你需要在这些 .get() 上使用 StringVar 来获得 goMongo() 函数内的值。

def goMongo():
    record1 = {'Name': clicked1.get(),