sqlite3 从打印数据中删除括号

2022-01-05 00:00:00 python raspberry-pi database sqlite

我创建了一个脚本来查找数据库第一行中的最后一个值

I have created a script that finds the last value in the first row of my database

import sqlite3
global SerialNum
conn = sqlite3.connect("MyFirstDB.db")
conn.text_factory = str
c = conn.cursor()
SerialNum = c.execute('select Serial from BI4000 where Serial in (Select max(Serial) from BI4000)')
print SerialNum
conn.commtt()
conn.close()

程序打印结果

[('00003',)]

这是当前数据库中的最后一个结果,所有进入最终数据库的数据都是序列号,所以是有序的.

which is the last result in the current database, all the data that will be entered into the final database will be serial numbers and so it will be in order.

我的问题是我是否可以删除所有引号/括号/逗号,因为我希望将此值分配给一个变量.

My question is can I remove all the quotations/brackets/comma as I wish to asign this value to a variable.

我希望制作的程序是一个向数据库添加新条目的测试系统,我希望检查数据库中的最后一个条目是什么,以便系统可以从该点继续输入.

The program that I wish to make is a testing system that adds new entries to the database, I wish to check what the last entry is in the database so the system can continue the entries from that point.

推荐答案

您执行的查询的结果表示为 Python 元组的 Python 列表.

The result of the query you execute is being represented as a Python list of Python tuples.

列表中包含的元组代表查询返回的行.

The tuples contained in the list represent the rows returned by your query.

元组中包含的每个值代表该特定行的对应字段,按照您选择它的顺序(在您的情况下,您只选择了一个字段,因此每个元组只有一个值).

Each value contained in a tuple represents the corresponding field, of that specific row, in the order you selected it (in your case you selected just one field, so each tuple has only one value).

长话短说:your_variable = SerialNum[0][0]

相关文章