Psycopg2 doesn't like table names that start with a lower case letter

前端 未结 3 1574
灰色年华
灰色年华 2020-12-19 03:22

I am running ActiveState\'s ActivePython 2.6.5.12 and PostgreSQL 9.0 Beta 1 under Windows XP.

If I create a table with an upper case first letter (i.e. Books), psyco

3条回答
  •  被撕碎了的回忆
    2020-12-19 04:22

    I often have to use Psycopg2 with tables that have been created by other people and they use a lot of case mixing in their column names.

    The solution I found is to use

    from psycopg2.extensions import AsIs
    

    and to then put the column name in a variable like:

    column_name = '"Column_Mixed_Case"'#Mind the '" quotes combination !
    

    and pass the variable to the sql as follows

    data = AsIs(column_name)
    sql = "select %s from table"
    cur.execute(sql,data)
    

提交回复
热议问题