Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

Cannot insert data in mongodb using python

I was learning to connect mongoDB with Python and came across this error.

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:Downloadanaconda pygamelibsite-packagespymongocollection.py", line 698, in insert_one
    self._insert(document,
  File "d:Downloadanaconda pygamelibsite-packagespymongocollection.py", line 613, in _insert
    return self._insert_one(
  File "d:Downloadanaconda pygamelibsite-packagespymongocollection.py", line 602, in _insert_one
    self.__database.client._retryable_write(
  File "d:Downloadanaconda pygamelibsite-packagespymongomongo_client.py", line 1498, in _retryable_write
    return self._retry_with_session(retryable, func, s, None)
  File "d:Downloadanaconda pygamelibsite-packagespymongomongo_client.py", line 1384, in _retry_with_session
    return self._retry_internal(retryable, func, session, bulk)
  File "d:Downloadanaconda pygamelibsite-packagespymongomongo_client.py", line 1416, in _retry_internal
    return func(session, sock_info, retryable)
  File "d:Downloadanaconda pygamelibsite-packagespymongocollection.py", line 590, in _insert_command
    result = sock_info.command(
  File "d:Downloadanaconda pygamelibsite-packagespymongopool.py", line 699, in command
    self._raise_connection_failure(error)
  File "d:Downloadanaconda pygamelibsite-packagespymongopool.py", line 683, in command
    return command(self, dbname, spec, slave_ok,
  File "d:Downloadanaconda pygamelibsite-packagespymongo
etwork.py", line 120, in command
    request_id, msg, size, max_doc_size = message._op_msg(
  File "d:Downloadanaconda pygamelibsite-packagespymongomessage.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()

Without the inserting code it works fine but when i add the data in the code it shows this dumb error Thank you for any help in advance

question from:https://stackoverflow.com/questions/65856735/cannot-insert-data-in-mongodb-using-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your variables contain objects of type tkinter.StringVar that MongoDB doesn't understand.

You can easily fix this by converting them to strings before inserting into the database, e.g.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...