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

2024-09-27 20:19:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我在学习将mongoDB与Python连接时遇到了这个错误

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'>

代码如下:

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()

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


Tags: inpydownloadliblineplacerootanaconda
2条回答

您的变量包含MongoDB不理解的tkinter.StringVar类型的对象

您可以通过在插入数据库之前将其转换为字符串来轻松解决此问题,例如

def goMongo():
    record1 = {'Name': str(clicked1),
               'Subject': str(SubSel),
               'Marks': str(MarkName1),
               'Grade': str(Grade1)}
    information.insert_one(record1)

您需要在那些StringVar上使用.get()来获取goMongo()函数中的值:

def goMongo():
    record1 = {'Name': clicked1.get(),
                'Subject': SubSel.get(),
                'Marks': MarkName1.get(),
                'Grade': Grade1.get()}
    information.insert_one(record1)

相关问题 更多 >

    热门问题