SQLite和Python正在向数据库中插入不需要的换行符

时间:2022-03-03 10:16:37

I'm writing a little program that inserts and retrieves movie information in a database. If I type the info into the .db file manually, the program retrieves it and displays it perfectly in my GUI's textboxes. However, if I try to insert data from those textboxes into my database, it adds a linebreak to the end of each field. When I retrieve it again, it has "\n" at the end. The retrieving works great, but somehow linebreaks are being included in my inserts. Here is my insert function:

我正在编写一个小程序,在数据库中插入和检索电影信息。如果我手动将信息输入.db文件,程序将检索它并在GUI的文本框中完美地显示它。但是,如果我试图将这些文本框中的数据插入到数据库中,它会在每个字段的末尾添加换行符。当我再次检索它时,它的末尾有“\n”。检索效果很好,但是在插入中包含了换行符。这是我的插入函数:

def addRecord(self):
    newTitle = self.text_title.get(1.0, 'end')
    newSynopsis = self.text_summary.get(1.0, 'end')
    newCast = self.text_cast.get(1.0, 'end')
    newRuntime = self.text_runtime.get(1.0, 'end')
    newRating = self.text_rating.get(1.0, 'end')
    newTrailer = self.text_trailer.get(1.0, 'end')
    newDates = self.text_date2d.get(1.0, 'end')
    newImage = self.text_image.get(1.0, 'end')

    c.execute("INSERT INTO Movies (Title, Synopsis, Actors, Runtime, Rating, Trailer, Dates, Image) VALUES ('{}','{}','{}','{}','{}','{}','{}','{}');"
              .format(newTitle, newSynopsis, newCast, newRuntime, newRating, newTrailer, newDates, newImage))
    conn.commit()

1 个解决方案

#1


2  

Tkinter automatically adds a newline as the last character in the text widget. The proper way to get only the contents of the text widget that you or the user inserted you should use "end-1c" ("end" minus one character) rather than "end" or END.

Tkinter自动添加一个换行符作为文本小部件中的最后一个字符。正确的方法是只获取您或用户插入的文本小部件的内容,应该使用“end-1c”(“end”-1字符),而不是“end”或end。

Also, to be pedantic, the first index should be a string, not a float. In the case of 1.0 it doesn't matter, but you should be in the habit of always using strings for the indexes in a text widget.

而且,要想学究式,第一个索引应该是字符串,而不是浮点数。对于1.0来说,这并不重要,但是您应该习惯在文本小部件中为索引使用字符串。

For example:

例如:

newTitle = self.text_title.get("1.0", 'end-1c')

#1


2  

Tkinter automatically adds a newline as the last character in the text widget. The proper way to get only the contents of the text widget that you or the user inserted you should use "end-1c" ("end" minus one character) rather than "end" or END.

Tkinter自动添加一个换行符作为文本小部件中的最后一个字符。正确的方法是只获取您或用户插入的文本小部件的内容,应该使用“end-1c”(“end”-1字符),而不是“end”或end。

Also, to be pedantic, the first index should be a string, not a float. In the case of 1.0 it doesn't matter, but you should be in the habit of always using strings for the indexes in a text widget.

而且,要想学究式,第一个索引应该是字符串,而不是浮点数。对于1.0来说,这并不重要,但是您应该习惯在文本小部件中为索引使用字符串。

For example:

例如:

newTitle = self.text_title.get("1.0", 'end-1c')