[Tutor] sqlite3 module
Dylan Armstrong
dylanarmstrong43 at yahoo.com
Mon Apr 17 18:42:42 EDT 2023
This makes sense but I'm not sure how to apply this. In my code I'm trying to limit my data to all orders created in 2023, but in my actual query these two dates are mentioned several times as it is a query spanning a few different tables. I've adjusted the sql query below to better represent what I'm doing. I'm familiar with APIs but I've never used them as a placeholder for variables.
import oracledbimport pandas as pd
# Set up the connectiondsn_tns = oracledb.makedsn( 'server', 'port', service_name='service name')connection_string = oracledb.connect(user='a', password='b', dsn=dsn_tns)
# Define the SQL querysql_query = """select * from orders1where delivery_date between :start_date and :end_dateselect * from orders2where delivery_date between :start_date and :end_dateselect * from orders3where delivery_date between :start_date and :end_date"""
start_date = '01-01-2023'end_date = '12-31-2023'
data_frame = pd.read_sql(sql_query, connection_string)
data_frame.to_excel('output_file.xlsx', index=False)
connection_string.close()
On Monday, April 17, 2023 at 04:00:56 PM CDT, Mats Wichmann <mats at wichmann.us> wrote:
On 4/16/23 21:49, Alex Kleider wrote:
> Good of you ('Q' aka ThreeBlindQuarks:-) to respond.
> Yes, I've been doing more researching and am realizing that one can in
> fact use dicts but with None/Null as values that are not meant to be
> touched (at least I think that's correct; haven't tested it yet!)
Just a comment, not as strong as a recommendation... if you're going to
be doing a lot of work with sql, you might want to look into the growing
list of Python ORM modules, that map between the Python object model and
the sql relational model, so you can basically "write Python" instead of
strings full of SQL syntax.
See SQLAlchemy, SQLObject, Django, peewee, storm, etc.
Some people have negative view of these while some swear by them.
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list