在int python中转换SQLITE' NoneType

时间:2018-06-18 19:47:33

标签: python sqlite

我试图" SELECT"来自Db的值并将此值添加到另一个变量,但是当我执行此操作时,我得到此错误" TypeError:不支持的操作数类型+:' NoneType'和' int' "

id = input("Digite o id do cartão: ")
cash = int(input("Digite o o valor a ser creditado: "))
dia = 3
sql = 'SELECT saldo FROM carteira where idcartao = ?'


def selectbanco():
     c.execute("SELECT saldo FROM carteira WHERE idcartao=?", (id,))
     row = c.fetchone()
     print(row)
     row = c.fetchone()
     soma = (row) + (cash)
     c.execute("UPDATE carteira SET saldo=?  WHERE idcartao=?", (soma, id))
     connection.commit()
selectbanco()

这是我的完整代码

import sqlite3
connection = sqlite3.connect('clientes.db')
c = connection.cursor()
#criação de tabela
def create_table():
    c.execute('CREATE TABLE IF NOT EXISTS carteira (idcartao REAL, saldo REAL, data text)')
create_table()
#variaveis
id = input("Digite o id do cartão: ")
cash = int(input("Digite o o valor a ser creditado: "))
dia = 3
sql = 'SELECT saldo FROM carteira where idcartao = ?'

#SELECT E RETORNAR VALOR
def selectbanco():
    c.execute("SELECT saldo FROM carteira WHERE idcartao=?", (id,))
    row = c.fetchone()
    print(row)
    row = c.fetchone()
    ##soma = (row + cash)
    ##print(soma)
    c.execute("UPDATE carteira SET saldo=?  WHERE idcartao=?", (cash, id))
    connection.commit()

selectbanco()

#leitura do banco
def read_data(wordUsed):
    for row in c.execute(sql, (wordUsed,)):
        print (row)
read_data(id)

connection.close()

2 个答案:

答案 0 :(得分:0)

你要两次获取行。您需要删除第二次获取才能接收该行。

def selectbanco():
     c.execute("SELECT saldo FROM carteira WHERE idcartao=?", (id,))
     row = c.fetchone()
     print(row)
     soma = (row) + (cash)
     c.execute("UPDATE carteira SET saldo=?  WHERE idcartao=?", (soma, id))
     connection.commit()
selectbanco()

变量被覆盖,因为你没有指定在获取之前执行的命令(第二次),因此是NoneType。

答案 1 :(得分:0)

这里有两个问题。

首先,您可以通过调用row = c.fetchone()两次来耗尽您的生成器,而无需重新执行查询。您只能为每个查询结果迭代一次光标;之后,您需要重新运行查询以“刷新”数据并能够再次迭代。

其次,fetchone()如果没有匹配,实际上会返回None。这与fetchall()相反,[]会在没有匹配的情况下返回空列表(import sqlite3 # Create a fake database conn = sqlite3.connect(':memory:') c = conn.cursor() c.execute("""CREATE TABLE IF NOT EXISTS some_table( something TEXT )""") c.execute(""" INSERT INTO some_table VALUES('hello') """) c.execute("SELECT * FROM some_table") # We get a match and this will print '(hello,)' data = c.fetchone() print(data) data = c.fetchone() # If we don't execute the query again but try to use the exhausted generator # then we'll also get None print(data) c.execute("SELECT * FROM some_table WHERE something = 'bye'") # This will print 'None' because fetchone() didn't get a result data = c.fetchone() print(data) c.execute("SELECT * FROM some_table WHERE something = 'bye'") # This will print an empty list because fetchall() didn't get a result data = c.fetchall() print(data) c.close() conn.close() )。

这个快速示例应该说明这种行为:

None

即使[]conn = sqlite3.connect(':memory:') c = conn.cursor() c.execute("""create table if not exists some_table( something TEXT )""") c.execute(""" INSERT INTO some_table VALUES('hello') """) # Get back None or an integer c.execute(""" SELECT * FROM some_table WHERE something = ?""", ('bye', )) data = c.fetchone() or 1 # This is where you return an integer instead of None print(data) c.close() conn.close() 不同,它们仍然是 falsey ,因此,在您的问题的上下文中,您仍然可以将响应转换为整数:< / p>

1

我选择了0的整数值,也许你想要None,我不确定。但需要注意的是,您可以通过两种途径获取C2:C200或虚假数据,而且您对它们的处理方式相同,这对于代码清晰度来说并不是很好。