Can json.dumps create multiple lines

Peter Otten __peter__ at web.de
Thu Dec 1 17:58:16 EST 2016


Cecil Westerhof wrote:

> On Thursday  1 Dec 2016 22:52 CET, Cecil Westerhof wrote:
> 
>> Now I need to convert the database. But that should not be a big
>> problem.
> 
> I did the conversion with:
>     cursor.execute('SELECT tipID FROM tips')
>     ids = cursor.fetchall()
>     for id in ids:
>         id = id[0]
>         cursor.execute('SELECT tip from tips WHERE tipID = ?', [id])
>         old_value = cursor.fetchone()[0]
>         new_value = json.dumps(json.loads(old_value), indent = 0)
>         cursor.execute('UPDATE tips SET tip = ? WHERE tipID = ?',
>         [new_value, id])

The sqlite3 module lets you define custom functions written in Python:

db = sqlite3.connect(...)
cs = db.cursor()
 
def convert(s):
    return json.dumps(
        json.loads(s),
        indent=0
    )

db.create_function("convert", 1, convert)
cs.execute("update tips set tip = convert(tip)")




More information about the Python-list mailing list