Tuesday, 15 September 2015

java - How can I populate an fts virtual table with the content from an regular table? -



java - How can I populate an fts virtual table with the content from an regular table? -

i've discovered possibility utilize virtual table searching thru database content. have been sitting 2 days now, , think i'm getting stupider every hr pass. can please provide me simple illustration of how can this?

this im at: got database class:

public class dbhelper { private static final string tag = dbhelper.class.getsimplename(); //database config private static int database_version = 2; public static string database_name = "shoppingdatabase.db"; //item table config private static final string item_table_name = "item_table"; private static final string item_table_column_id = "_id"; private static final string item_table_column_item_name = "item_name"; private static final string item_table_column_item_size = "item_size"; private static final string item_table_column_item_price = "item_price"; private static final string item_table_column_item_brand = "item_brand"; private static final string item_table_column_item_catagory = "item_catagory"; //viritual table config public static final string key_rowid = "rowid"; public static final string key_item = "name"; public static final string key_size = "size"; public static final string key_price = "price"; public static final string key_brand = "brand"; public static final string key_catagory = "catagory"; public static final string key_search = "searchdata"; private static final string fts_virtual_table = "iteminfo"; private databasehelper dbhelper; private sqlitedatabase db; public dbhelper(context acontext){ dbhelper = new databasehelper(acontext); db = dbhelper.getwritabledatabase(); } //i utilize method in additem class user add together item database public void additem(string iname, string isize, string iprice, string ibrand, string icatagory){ contentvalues cv = new contentvalues(); cv.put(item_table_column_item_name, iname); cv.put(item_table_column_item_size, isize); cv.put(item_table_column_item_price, iprice); cv.put(item_table_column_item_brand, ibrand); cv.put(item_table_column_item_catagory, icatagory); db.insert(item_table_name, null, cv); } public void populatevt() { } private class databasehelper extends sqliteopenhelper{ public databasehelper(context acontext){ super(acontext, database_name, null, database_version); } @override public void oncreate(sqlitedatabase sqlitedb){ string buildsql = "create table " + item_table_name + "( " + item_table_column_id + " integer primary key, " + item_table_column_item_name + " text, " + item_table_column_item_size + " text, " + item_table_column_item_price + " text, " + item_table_column_item_brand + " text, " + item_table_column_item_catagory + " text )"; string buildsql2 = "create virtual table " + fts_virtual_table + " using fts3(" + key_item + "," + key_size + "," + key_price + "," + key_brand + "," + key_catagory + "," + key_search + "," + " unique (" + key_item + "));"; log.d(tag, "oncreate sql: " + buildsql); log.d(tag, "oncreate sql: " + buildsql2); sqlitedb.execsql(buildsql); sqlitedb.execsql(buildsql2); } @override public void onupgrade(sqlitedatabase sqlitedb, int oldversion, int newversion){ string buildsql = "drop table if exists " + item_table_name; string buildsql2 = "drop table if exists " + fts_virtual_table; log.d(tag, "onupgrade sql: " + buildsql); log.d(tag, "onupgrade sql: " + buildsql2); sqlitedb.execsql(buildsql); sqlitedb.execsql(buildsql2); oncreate(sqlitedb); } } }

how can utilize populatevt() populate virtual table contents item_table_name? , should phone call updates every time user adds database?

this how solved problem.

in database class added these methods:

public long createitem(string name, string size, string price, string brand) { contentvalues initialvalues = new contentvalues(); string searchvalue = name + " " + size + " " + cost + " " + brand; initialvalues.put(key_item, name); initialvalues.put(key_size, size); initialvalues.put(key_price, price); initialvalues.put(key_brand, brand); initialvalues.put(key_catagory, catagory); initialvalues.put(key_search, searchvalue); homecoming db.insert(fts_virtual_table, null, initialvalues); } public cursor listall(){ string buildsql = "select * " + item_table_name; log.d(tag, "listall sql: " + buildsql); homecoming db.rawquery(buildsql, null); } public boolean deleteallitems() { int donedelete = 0; donedelete = db.delete(fts_virtual_table, null , null); log.w(tag, integer.tostring(donedelete)); homecoming donedelete > 0; }

and in mainactivity used cursor iterate thru original table , fill virtual table content:

@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); settitle(""); dbhelper = new dbhelper(this); dbhelper.deleteallcustomers(); fillvt(); } public void fillvt(){ string a, b, x, y, z; cursor c = dbhelper.listall(); if(c.movetofirst()){ do{ = c.getstring(1); b = c.getstring(2); x = c.getstring(3); y = c.getstring(4); z = c.getstring(5); dbhelper.createcustomer(a, b, x, y, z); }while(c.movetonext()); } } @override protected void onactivityresult(int reqcode, int rescode, intent data){ super.onactivityresult(reqcode, rescode, data); if(reqcode == enter_data_request_code && rescode == result_ok){ dbhelper.additem(data.getextras().getstring("tag_item_item_name"), data.getextras().getstring("tag_item_item_size"), data.getextras().getstring("tag_item_item_price"), data.getextras().getstring("tag_item_item_brand"), data.getextras().getstring("tag_item_item_catagory")); dbhelper.deleteallcustomers(); fillvt(); cursoradapter.changecursor(dbhelper.listall()); } }

i set dbhelper.deleteallcustomers() , fillvt() in onactivityresult() update virtual table every time new item added

java android sqlite fts3

No comments:

Post a Comment