使用Python从SqlLite3追加和检索列表

时间:2017-12-14 13:02:06

标签: python sqlite

在我的示例中,我需要在数据库中存储一个int列表和一个字符串列表。我目前只是将整个int列表和字符串列表转换为单个int。我想知道这是否是一个理想的工作流程,或者是否有人有一些关于如何处理这个问题的替代建议。我把它作为一个字符串存储的问题是我如何才能将这些信息正确地作为一个pythonic的int和字符串列表来检索?

import sqlite3
import hashlib


database = 'test.db'


def create_table():
    connection = sqlite3.connect(database)
    cursor = connection.cursor()

    cursor.execute("CREATE TABLE IF NOT EXISTS assets(url BLOB UNIQUE, colors BLOB, tags BLOB)")

    connection.commit()
    cursor.close()
    connection.close()


def kill_table():
    connection = sqlite3.connect(database)
    cursor = connection.cursor()
    cursor.execute('''DROP TABLE IF EXISTS assets''')
    connection.commit()


def read_from_db():
    connection = sqlite3.connect(database)
    cursor = connection.cursor()

    cursor.execute('SELECT * FROM assets')
    data = cursor.fetchall()
    print(len(data))
    for row in data:
        print(row)

    cursor.close()
    connection.close()


def get_data_entry(url=''):
    connection = sqlite3.connect(database)
    cursor = connection.cursor()

    url = hashlib.md5(url).hexdigest()

    cursor.execute('SELECT * FROM assets WHERE url=?', (url,))
    data = cursor.fetchall()

    if len(data) == 1:
        return data[0]
    else:
        print 'Found multiple entry instances'
        return False


def append_data_entries(url, colors, tags):
    '''
    Args:
        url (str): name of image item
        colors (list): list of dominant image colors
        tags (list): list of tags
    '''
    if not url or not colors or not tags:
        return False

    url = hashlib.md5(url).hexdigest()
    colors = str(colors)
    tags = str(tags)

    # updates or inserts
    cursor.execute("REPLACE INTO assets(url, colors, tags) VALUES (?, ?, ?)",
          (url, colors, tags))

    return True



if __name__ == '__main__':
    'Example'
    kill_table()
    create_table()

    # add test data to database
    connection = sqlite3.connect(database)
    cursor = connection.cursor()

    for i in range(10):
        url = '{num:08d}'.format(num=i)
        append_data_entries(url, '[[0,0,0],[10,10,10],[50,50,50]]','["red","green","blue","orange"]')

    connection.commit()
    cursor.close()
    connection.close()


    read_from_db()
    print 'ITEM:', get_data_entry('00000006')

1 个答案:

答案 0 :(得分:1)

检索数据时,它会按预期返回一个字符串元组,然后必须在合适的数据类型中转换每个元素,对于这种特殊情况,ast.literal_eval函数应该有效:

def convert(in_data):
    def cvt(data):
        try:
            return ast.literal_eval(data)
        except Exception:
            return str(data)
    return tuple(map(cvt, in_data))

示例代码:

import sqlite3
import hashlib
import ast

database = 'test.db'


def create_table():
    connection = sqlite3.connect(database)
    cursor = connection.cursor()

    cursor.execute("CREATE TABLE IF NOT EXISTS assets(url BLOB UNIQUE, colors BLOB, tags BLOB)")

    connection.commit()
    cursor.close()
    connection.close()


def kill_table():
    connection = sqlite3.connect(database)
    cursor = connection.cursor()
    cursor.execute('''DROP TABLE IF EXISTS assets''')
    connection.commit()

def convert(in_data):
    def cvt(data):
        try:
            return ast.literal_eval(data)
        except Exception:
            return str(data)
    return tuple(map(cvt, in_data))

def read_from_db():
    connection = sqlite3.connect(database)
    cursor = connection.cursor()

    cursor.execute('SELECT * FROM assets')
    data = cursor.fetchall()
    print(len(data))
    for row in data:
        print(convert(row))

    cursor.close()
    connection.close()


def get_data_entry(url=''):
    connection = sqlite3.connect(database)
    cursor = connection.cursor()

    url = hashlib.md5(url).hexdigest()

    cursor.execute('SELECT * FROM assets WHERE url=?', (url,))
    data = cursor.fetchall()

    if len(data) == 1:
        return convert(data[0])
    else:
        print('Found multiple entry instances')
        return False


def append_data_entries(url, colors, tags):
    '''
    Args:
        url (str): name of image item
        colors (list): list of dominant image colors
        tags (list): list of tags
    '''
    if not url or not colors or not tags:
        return False

    url = hashlib.md5(url).hexdigest()
    colors = str(colors)
    tags = str(tags)

    # updates or inserts
    cursor.execute("REPLACE INTO assets(url, colors, tags) VALUES (?, ?, ?)",
          (url, colors, tags))

    return True



if __name__ == '__main__':
    'Example'
    kill_table()
    create_table()

    # add test data to database
    connection = sqlite3.connect(database)
    cursor = connection.cursor()

    for i in range(10):
        url = '{num:08d}'.format(num=i)
        append_data_entries(url, '[[0,0,0],[10,10,10],[50,50,50]]','["red","green","blue","orange"]')

    connection.commit()
    cursor.close()
    connection.close()


    read_from_db()
    print('ITEM:', get_data_entry('00000006'))