Python の SQLite3 にはcreate_function
という関数があり、Python側で定義した関数をSQLite側に登録して使うことができます。たとえば String を 2 つ取り、その文字列長の差の絶対値を返す関数 func
を定義して、それを SQLite 側で使用した例を以下に示します。
python
1import sqlite3 2 3def func(x, y): 4 return abs(len(x) - len(y)) 5 6con = sqlite3.connect(':memory:') 7con.execute('create table foo(val text)') 8con.execute('insert into foo(val) values ("tomato"), ("potato"), ("onion"), ("mushroom")') 9con.create_function('func', 2, func) 10 11c = con.cursor() 12c.execute('select val, func(val, "hello") from foo') 13 14for row in c: 15 print(row[0], row[1])
このプログラムを実行すると、以下の文字列が出力されます。
tomato 1 potato 1 onion 0 mushroom 3
Go言語でも同じようなことができると嬉しいのですが、そのような機能を持つライブラリはありますでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/01/03 23:47
退会済みユーザー
2018/01/04 03:12