开发者

Inserting SQLite query results into another table

I am having trouble appending the results of a SQLite query query into another table. Eventually I would also like to add a datapoint to the query result and append it to the new table as well. Here is a representation of my attempt so far:

import datetime
import sqlite3

createDb = sqlite3.connect(r'C:\test') 
queryCurs = createDb.cursor()

def qryblock(): 
    return queryCurs.execute('''SELECT DB.R,
    DB.T,
    DB.A
    FROM DB.SAT SAT,
    DB.I I
    WHERE A.RECOR = T.RECOR AND
    ((A.DF Is Not Null) AND
    (A.PF Is Null) OR
    (A.DF Is Null) AND
    (A.PF Is Null) AND
    (A.P<{d '1999-09-09'}))''')

def addmonth(current_line):
    queryCurs.execute('''INSERT INTO monthview (record, route, date, amt)
    VALUES (?,?,?,?)''',(record, route, date, amt))

def main():
    current_date = datetime.datetime.today()
    todayslist = []
    if current_date == firstbusinessday():
        monthsblo开发者_开发问答ck = qryblock() 
        for line in monthsblock:
            for datapoint in line:
                todayslist.append(datapoint)
                todayslist.append(current_date)
                addmonth(todayslist)
            todayslist = []
        createDb.commit()
   else:
       pass

if __name__ == '__main__':
    main()

Is there a more efficient way of getting each line from the query into the new table? How about if I wanted to append the current date to each row in the query results before inserting it into a new table. And finally, say the list or tuple of the records in my query are in a different order than the parameters in the function to get them into the new table. How would I deal with that?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜