There are usually several ways to query by date:
There are several methods for querying by date range, and the date field type is generally:
Timestamp without timezone
Method 1:
select * from user_info where create_date >= '2015-07-01' and create_date < '2015-08-15';
Method 2:
select * from user_info where create_date between '2015-07-01' and '2015-08-15';
Method 3:
select * from user_info where create_date >= '2015-07-01'::timestamp and create_date < '2015-08-15'::timestamp;
Method 4:
select * from user_info where create_date between to_date('2015-07-01','YYYY-MM-DD') and to_date('2015-08-15','YYYY-MM-DD');
When pandas.to_sql encounters duplicate primary key, how can you skip and continue execution? It is actually very simple, just insert one by one, because to_sql does not have a good solution.
The specific code is as follows:
for exchange in exchange_list.items(): if exchange[1]==True: pass else: continue sql = """ SELECT * FROM %s WHERE "time" BETWEEN '2019-07-05 18:48' AND '2019-07-09' """ % (exchange[0]) data = pd.read_sql(sql=sql, con=conn) print(()) for i in range(len(data)): #sql = "SELECT * FROM `%s` WHERE `key` = '{}'"%(exchange).format() #found = pd.read_sql(sql, con=conn2) #if len(found) == 0: try: [i:i + 1].to_sql(name=exchange[0], index=False,if_exists='append', con=conn2) except Exception as e: print(e) pass
pandas.to_sql cannot set the primary key, this is certain. The method that can be done is to use the method of creating a table before to_sql to create a table
The code for building a table is as follows:
/* Create SEQUENCE for table */ DROP SEQUENCE IF EXISTS @exchangeName_id_seq; CREATE SEQUENCE @exchangeName_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1; /* Create Table structure for table */ DROP TABLE IF EXISTS "public"."@exchangeName"; CREATE TABLE "public"."@exchangeName" ( "id" int4 NOT NULL DEFAULT nextval('@exchangeName_id_seq'::regclass), "time" timestamp(6) NOT NULL, "open" float8, "high" float8, "low" float8, "close" float8, "volume" float8, "info" varchar COLLATE "pg_catalog"."default" NOT NULL ) ; /* Create Primary Key structure for table */ ALTER TABLE "public"."@exchangeName" DROP CONSTRAINT IF EXISTS "@exchangeName_pkey"; ALTER TABLE "public"."@exchangeName" ADD CONSTRAINT "@exchangeName_pkey" PRIMARY KEY ("time", "info");
Supplement: Postgresql database time interval data query
The current time is pushed forward one day:
SELECT current_timestamp - interval '1 day'
The current time is pushed forward by one month:
SELECT current_timestamp - interval '1 month'
The current time is one year ahead:
SELECT current_timestamp - interval '1 year'
The current time is pushed forward by one hour:
SELECT current_timestamp - interval '1 hour'
Push the current time forward by one minute:
SELECT current_timestamp - interval '1 min'
The current time is pushed forward by 60 seconds:
SELECT current_timestamp - interval '60 second'
The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.