未创建动态查询psycopg2表,但未引发错误

2024-09-27 00:21:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过psycopg2在postgresql10中创建一个分区表。表的名称是作为参数获取的变量。我没有收到任何错误,但表未创建:

     def create_partition(day,month,year):
        cursor=connect_db()
        relname='log_full_'+year+'_'+month+'_'+day
        range_day=year+'-'+month+'-'+day + ' 00:00:00'
        partition_range_day=datetime.strptime(range_day,"%Y-%m-%d %H:%M:%S")    partition_range_next_day=datetime.strftime(partition_range_day+timedelta(days=1),"%Y-%m-%d %H:%M:%S")
        partition_range_day=str(partition_range_day)
        partition_range_next_day=str(partition_range_next_day)
        cursor.execute("SELECT count(*) from pg_class where relname like %s",(relname,))
        rows=cursor.fetchall()
        if int(rows[0][0])==0:
                 cursor.execute(sql.SQL("create table {} partition of log_full(end_date) for VALUES from (%s) TO (%s) partition by list (start_stop)").format(sql.Identifier(relname)),(str(partition_range_day),str(partition_range_next_day),))

我还尝试更改execute命令的语法,但结果是一样的:

cursor.execute("create table %s partition of radius_log_full(end_date) for VALUES from (%%s) TO (%%s) partition by list (start_stop);" % relname,[str(partition_range_day),str(partition_range_next_day)])

我没有得到任何错误,表也没有创建。。 在postgresql数据库的serverlog中,我看到了下一个输出:

[2018-02-07 13:12:47 IST] LOG:  duration: 10.618 ms  statement: create table "log_full_2018_02_01" partition of log_full(end_date) for VALUES from ('2018-02-01 00:00:00') TO ('2018-02-02 00:00:00') partition by list (start_stop);

当我在psql客户机接口上运行这个命令时,表就被创建了。此外,它没有在不同的模式上创建表,因为我查询了pgèu类,但没有找到关系。你知道吗

我的问题是我的代码中有什么错误,什么是动态查询的最佳解决方案,为什么?我的psycopg2的版本是2.7.1


Tags: fromlogexecute错误createrangeyearcursor

热门问题