简体   繁体   English

我无法从我的 Python sqlite3 数据库中获取 integer

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

I want to get the integer 5 from my database but I keep getting "[('5',)]" or "('5',)" this is my code:我想从我的数据库中获取 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())

It is not uncommon for database API to return list or tuples.数据库 API 返回列表或元组的情况并不少见。

A simple solution would be to cast this value directly to integer, if you are sure it will always be an integer:一个简单的解决方案是将此值直接转换为 integer,如果您确定它始终是 integer:

For the list of tuples ("[('5',)]"):对于元组列表 ("[('5',)]"):

return int(results[0][0])

For the tuple ("('5',)"):对于元组 ("('5',)"):

return int(results[0])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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