How to split delimited values in a SQLite column into multiple columns
<p>How can I parse comma separated values in <code>Fruit Basket</code> and move them to other columns. </p>

<p>For example, I want this</p>

<pre><code>Fruit Basket  Fruit1    Fruit2    Fruit3
------------  --------  --------  --------
Apple
Banana, Pear
Lemon, Peach, Apricot
</code></pre>

<p>to becomes this</p>

<pre><code>Fruit Basket  Fruit1    Fruit2    Fruit3
------------  --------  --------  --------
Apple         Apple
Banana, Pear  Banana    Pear
Lemon, Pea... Lemon     Peach      Apricot
</code></pre>

<p>If I cannot do this with a pure SQLite statement, how can I do it using Python?</p>

<p>Got it</p>

<pre><code>def returnFruitName(string, index):
    #Split string and remove white space
    return [x.strip() for x in string.split(',')][index]


cur.create_function("returnFruitName", 2, returnFruitName)

cur.execute("UPDATE t SET Fruit1 = returnFruitName(FruitBasket,0) WHERE FruitBasket IS NOT NULL;")
cur.execute("UPDATE t SET Fruit2 = returnFruitName(FruitBasket,1) WHERE FruitBasket IS NOT NULL;")
cur.execute("UPDATE t SET Fruit3 = returnFruitName(FruitBasket,1) WHERE FruitBasket IS NOT NULL;")
</code></pre>

python sqlite