2017-01-08 16 views
2

に角括弧を返しません。sqlite3カラム名を取得するためにcursor.descriptionを使用しようとしています。 https://docs.python.org/2/library/sqlite3.htmlを見ると、sqlite3.connect()のdetect_types paramを使用できるかもしれませんが、それは効果がないようです。ここで私が試したものです:cursor.descriptionはカラム名(python 2.7 + sqlite3)エイリアス

import sqlite3 
import sys 

# tried using detect_types=0, sqlite3.PARSE_COLNAMES, or sqlite3.PARSE_DECLTYPES) 
# all 3 values yield the same result 
conn = sqlite3.connect(':memory:', detect_types=0) 
cur = conn.cursor() 
cur.execute('select 1 as "H[ello"') 

for x in cur.description: 
    sys.stderr.write("should write H[ello, but only writes: " + x[0]) 
    # output: 
    # should write H[ello, but only writes: H 

答えて

1

[特殊なケースように見える - それはfiltered by the sqlite3 module while building a column name for the cursor descriptionです:

PyObject* _pysqlite_build_column_name(const char* colname) 
{ 
    const char* pos; 

    if (!colname) { 
     Py_RETURN_NONE; 
    } 

    for (pos = colname;; pos++) { 
     if (*pos == 0 || *pos == '[') { 
      if ((*pos == '[') && (pos > colname) && (*(pos-1) == ' ')) { 
       pos--; 
      } 
      return PyUnicode_FromStringAndSize(colname, pos - colname); 
     } 
    } 
} 

わからない、なぜけれども私は - おそらくのMS Accessとの互換性に関連します。

+0

ありがとうございました。 detect_typesの設定によって動作が影響を受けますか?もしそうなら、_pysqlite_build_column_name(detect_typesを示すために追加のargが必要です)を変更してから、それを(_pysqlite_query_executeから少なくともどこか別の場所に)呼び出す方法を変更するのは簡単なようです。この問題を提起し、パッチを追求するための最良のフォーラムは何でしょうか(それがうまくいくと思います) – mwag

+0

@mwagええ、私は 'detect_types'が動作に影響を与えることを期待します。私は間違いなく[Python issue tracker](https://bugs.python.org/)で問題を提起します。ありがとう、良い質問! – alecxe

関連する問題