This is a SQL question. I want to automatically turn the same query to many tables.

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

SELECT * FROM 'Table name' ORDER BY 'Column name' DESC Limit 30

I'd like to turn the above query as many as the number of tables, and there are about 300 tables.

1/ The PD table contains the table names of NameData. SELECT * FROM "Table Name" ORDER BY High DESC Limit 30 I'd like to get the name data of the table name on the PD table one by one and arrange the Keys tables.

If it is impossible to do number 2/1, I would like to arrange all the tables in the Keys database without deciding the 'table name' as one.

import sqlite3

con = sqlite3.connect("PD.db")
con1 = sqlite3.connect("Keys.db")
with con1:
    cur = con1.cursor()
    array1 = Keys table names in "SELECT* from NameData" #PD.
    rows = cur.execute(array1)

    for row in rows:
        array = "SELECT* FROM 'Table Name' ORDER BY 'Column Name' DESC Limit 30"
        rows = cur.execute(array)

3/ If you run it, the PD and Keys will come out separately It only says that there is at least a table called array1 in the 'table name'. Doesn't the query contain variables?

DB Browser for SQLite is in use.

sqlite3 python

2022-09-20 10:27

2 Answers

Maybe it should be this kind of development. I haven't tested this code, so please refer to it.

con = sqlite3.connect("PD.db")
con1 = sqlite3.connect("Keys.db")

con1tableNames = []
with con:
    cur = con.cursor()
    rows = cur.execute("SELECT tableName from NameData")
    for row in rows:

con1Results = []
with con1:
    for con1tableName in con1tableNames:
        cur = con1.cursor()
        # String + Variables + String = The process of creating a string is called 'interpolation'
        rows = cur.execute("SELECT * FROM " + con1tableName + " ORDER BY col1 DESC LIMIT 30")
        for row in rows:

2022-09-20 10:27

rows = cur1.execute("SELECT * FROM" "[f'{con1tableName}']"ORDER BY..."TypeError: can only concatenate str(not "list") to str
                    #"SELECT * FROM "   f'{con1tableName}' "ORDER BY...``"   ``"   =  qlite3.OperationalError: near"/" : synyax error
                    #"SELECT * FROM "  "[f'{con1tableName}']" "ORDER BY... ``"    = The table name does not contain f'{con1tableName}'.
                    #"SELECT * FROM "  "[BTC/USDT]" "ORDER BY...``"     = The desired value will be obtained.

2022-09-20 10:27

If you have any answers or tips

© 2022 pinfo. All rights reserved.