简体   繁体   中英

SQLite3 Queries

I am looking to fetch employee shift details (for 7 days) which works well; however, the each row ends with ",". I am looking to delete "," so that I can copy those results to tkinter entry boxes.

def showRecord():
    connection = sqlite3.connect("C:\Projects\Advisor Roster Swap\employee.db")
    connection.text_factory = sqlite3.OptimizedUnicode
    cursor = connection.cursor()
    cursor.execute('''SELECT "Scheduled Shift" FROM employee_details WHERE Ecode = "5568328"''')
    items = cursor.fetchall()
    print(items)
    connection.close()

The result looks like: [('WO',), ('10:30 - 19:30',), ('10:30 - 19:30',), ('10:30 - 19:30',), ('10:30 - 19:30',), ('10:30 - 19:30',), ('WO',)]

I need to delete the extra ",". Any help is welcome.

items is a list of tuples, the comma is only shown when printing it. If you want to get the column value for the n th row, items[n - 1] will give you a tuple containing just the value: ("WO",) . To then get the value in the tuple use items[n - 1][0] . You can wrap this in to a comprehension:

items = [i[0] for i in items]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM