java - SQLite create dynamic table (or view?) -
i have 3 tables:
table.keys, table.tags, table.values
table.keys create table statement:
createtablestatement = "create table " + tables.keys + "(" + keyscolumns._id + " integer primary key autoincrement," + keyscolumns.key + " text not null," + "unique (" + keyscolumns.key + ") on conflict ignore" + ");"; execsql(sqlitedatabase, createtablestatement);
table.tags create table statement:
createtablestatement = "create table " + tables.tags + " (" + tagscolumns._id + " integer primary key autoincrement," + tagscolumns.name + " text not null," + "unique (" + tagscolumns.name + ") on conflict ignore" + ");"; execsql(sqlitedatabase, createtablestatement);
table.value create table statement:
createtablestatement = "create table " + tables.values + " (" + valuescolumns._id + " integer primary key autoincrement," + valuescolumns.key_id + " text not null," + valuescolumns.tag_id + " text not null," + valuescolumns.value + " text not null," + "unique (" + valuescolumns.key_id + ", " + valuescolumns.tag_id + ") on conflict replace" + ");"; execsql(sqlitedatabase, createtablestatement);
if following join:
tables.keys + " join " + tables.values + " on " + values.key_id + " = " + keys.column(keys._id) + " join " + tables.tags + " on " + values.tag_id + " = " + tags.column(tags._id);
i duplicate rows of course because result is
key | tag | value ================= | |
what accomplish query , cursor table or view no duplicate rows following schema:
key | tag 1 | tag 2 | ... | tag n ================================= | | | |
not keys must have values each tag, keys can have values.
i'm not sure how accomplish this. i'm not sure start.
in meantime have created table stores tag values know exist.
but feel inefficient because @ time have 'n' number of new tag values why able create view or table schema listed.
sqlite has no pivot functions; have in 2 steps.
first, possible tags:
select _id, name tags order name;
then, using returned data, construct query looks each possible tag each key:
select key, (select value values key_id = keys._id , tag_id = 111 ) tag_111, (select value values key_id = keys._id , tag_id = 222 ) tag_222, ... keys;
Comments
Post a Comment