简体   繁体   中英

Select last created table with sqlite3 python

I found a way to do it like this:

import sqlite3
conn = sqlite3.connect(
        'avg_prices.db',
        detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES)
cursorObj = conn.cursor()
cursorObj.execute('SELECT name from sqlite_master where type= "table"')
listed_tables =cursorObj.fetchall()
last_table = listed_tables[len(listed_tables)-1][0]

rate="VES/COP"
sql = "SELECT * FROM" + "'" + last_table + "'" + "WHERE Rates=" + "'" + rate + "'"
cursorObj.execute(sql)
result = cursorObj.fetchone()

Being result the values that I wanted to get.

I check this: MySQL - query for last created table And it seems there is an easier way, but I didn't manage to get it to work on python.

SQLite does not store the creation time of tables. That means that you cannot do what you want.

The sqlite_master has no relevant column. You are at your own risk if you assume that the results are returned in table creation order.

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