Home > front end >  How do I get rid of the "{}" in my windows output? (Python / SQLite3)
How do I get rid of the "{}" in my windows output? (Python / SQLite3)

Time:11-24

I'm creating a flashcard game to ask CompSci questions.

I'm trying to retrieve a random "CardFront" which acts as a varchar stored in an SQLite3 DB table, and output that result to a messagebox to "Prompt" the user with the question.

Only problem I can't seem to figure out is why it is returning with squiggly brackets around the statement?

from tkinter import *
import sqlite3
from tkinter import messagebox

def retrieve_random_cardfront():
    conn = sqlite3.connect('flashcards.db')
    cursor = conn.cursor()
    cursor.execute("SELECT CardFront FROM FLASHCARDS ORDER BY RANDOM() LIMIT 1;")
    result = cursor.fetchall()
    conn.close()
    messagebox.showinfo(title='Test', message=result[0])

Current Output

CodePudding user response:

You can try this as a fix, but what is happening is unclear here. Maybe check into the __repr__ of the result object ?

from tkinter import *
import sqlite3
from tkinter import messagebox

def retrieve_random_cardfront():
    conn = sqlite3.connect('flashcards.db')
    cursor = conn.cursor()
    cursor.execute("SELECT CardFront FROM FLASHCARDS ORDER BY RANDOM() LIMIT 1;")
    result = cursor.fetchall()
    conn.close()
    messagebox.showinfo(title='Test', message=str(result[0]).strip('{').strip('}'))

CodePudding user response:

I think you can implement something like this in order to remove that "{}". But since I'm not aware of using SQLite, I could give you a more precise answer.

data = "{Data}"
data = data.replace(data[0], "")
data = data.replace(data[-1], "")
print(data)
  • Related