簡體   English   中英

我無法從我的 Python sqlite3 數據庫中獲取 integer

[英]I can't get an integer from my Python sqlite3 database

我想從我的數據庫中獲取 integer 5 但我不斷收到“[('5',)]”或“('5',)”這是我的代碼:

import sqlite3

db = sqlite3.connect("Users.db")
cursor = db.cursor()

def get_clearance(username='CC-2225', clearance=None):
        find_clearance = "SELECT clearance FROM user WHERE username = username"
        cursor.execute(find_clearance)
        results = cursor.fetchall()
        return results

print(get_clearance())

數據庫 API 返回列表或元組的情況並不少見。

一個簡單的解決方案是將此值直接轉換為 integer,如果您確定它始終是 integer:

對於元組列表 ("[('5',)]"):

return int(results[0][0])

對於元組 ("('5',)"):

return int(results[0])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM