27

私は解決できなかったこの厄介なエラーを抱えています..ここに私の機能があります

def savePicture(pic):
try:
    connection=sqlite3.connect('/home/faris/Desktop/site/site.db')
    db=connection.cursor()
    print type(pic.user.profile_picture)
    db.execute('INSERT INTO pictures (picture_id, caption, created_time, picture_url, link, username,full_name,profile_picture) VALUES (?,?,?,?,?,?,?,?)',
        [
        pic.id,
        pic.caption,
        pic.created_time,
        pic.get_standard_resolution_url(),
        pic.link,
        pic.user.username,
        pic.user.full_name,
        pic.user.profile_picture
        ])
    connection.commit()
    connection.close()
except sqlite3.IntegrityError:
    print 'pic already exist'

そして、これが私のテーブルです(Sqlite :D)

-- Describe PICTURES
CREATE TABLE "pictures" (
"picture_id" INTEGER PRIMARY KEY,
"caption" TEXT,
"created_time" TEXT,
"picture_url" TEXT,
"link" TEXT,
"username" TEXT,
"full_name" TEXT,
"profile_picture" TEXT
)

そして、これは私が抱えているエラーです、

<type 'str'>
Traceback (most recent call last):
File "/home/faris/Desktop/site/cron/pictures.py", line 15, in <module>
savePicture(picture)
File "/home/faris/Desktop/site/db.py", line 36, in savePicture
pic.user.profile_picture
sqlite3.InterfaceError: Error binding parameter 1 - probably unsupported type.

ご覧のとおり、「pic.user.profile_picture」のタイプを出力し、str を返しました。また、これら 2 つの関数 ( unicode と str ) を使用して、運のない文字列が返されていることを確認しました。

何か案は?乾杯:D

4

3 に答える 3

25

わかりました、それはばかです笑

    pic.caption,
    pic.created_time,

TEXTタイプではありません..しかし、エラーメッセージはpic.user.profile_pictureからの問題を言っています. したがって、このエラーが発生した場合は、すべてのパラメーターを確認してください:)

以下のコメントを読んでください:)

于 2012-10-18T10:56:34.080 に答える
0

エラーメッセージ

パラメータ 1 のバインディング エラー - おそらくサポートされていない型です

は、cursor.execute に渡される一連の値の位置 1 *の値が、Python の sqlite3 モジュールでサポートされていない型のインスタンスであることを報告しています。

Python の sqlite3 モジュールは、デフォルトで , , , の受け渡しをサポートしており、インスタンスの適応方法も認識Noneintfloatいます。strbytesdatetime.datedatetime.datetime

エラーを修正するには、報告された位置の値をサポートされている型に変換します。

アダプターとコンバーターを登録して、その型のオブジェクトをサポートされている型の 1 つとの間で変換することにより、サポートされていない型をデータベースに格納することをシミュレートできます**。これは、インスタンスの処理dateに使用されるメカニズムです。データベースに格納および取得できるようにしたいクラスがあるとしますdatetimeFruit

class Fruit:
    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return f'Fruit({self.name!r})'

アダプター関数とコンバーター関数を作成し、それらを sqlite に登録します。

def adapt_fruit(fruit):
    # We must return a supported type.
    return fruit.name

def convert_fruit(value):
    # value will always be bytes.
    return Fruit(value.decode('utf-8'))

sqlite3.register_adapter(Fruit, adapt_fruit)

# The first argument is the type of column that will store "fruits".
sqlite3.register_converter('fruit', convert_fruit)

接続に渡しdetect_typesて、 type の列を持つテーブルを作成できますfruit

apple = Fruit('Apple')
banana = Fruit('Banana')

with sqlite3.connect('so12952546.db', detect_types=sqlite3.PARSE_DECLTYPES) as conn:
    cur = conn.cursor()
    cur.execute("""DROP TABLE IF EXISTS tbl""")
    # Create a column with our "fruit" type.
    cur.execute("""CREATE TABLE tbl (name fruit)""")
    # Pass Fruit instances as values.
    cur.executemany("""INSERT INTO tbl (name) VALUES (?)""", [(apple,), (banana,)])
    conn.commit()
    cur.execute("""SELECT name FROM tbl""")
    # Fruit instances are returned.
    for row in cur:
        print(row)

出力:

(Fruit('Apple'),)
(Fruit('Banana'),)

列には Sqlite の型がありますが、この場合、fruitサポートされているストレージ クラスに関連付けられます。text

sqlite> .schema tbl
CREATE TABLE tbl (name fruit);
sqlite> SELECT name, typeof(name) FROM tbl;
Apple|text
Banana|text

*位置はゼロベースのカウントを使用して計算されるため、位置 1 が2 番目の値になります

**この例は、すべてのオプションをカバーしているわけではありません。詳細については、モジュールのドキュメントを参照してください。

于 2021-09-21T11:52:45.623 に答える