I have created an SQLite FTS3 table that I am using within the Android development platform (target is 2.2 and minVersion is 8). For example, I created the table as follows:
create virtual table person using fts3(first,last);
I know that when I insert data into this table, I long is returned, which is the row ID of the record inserted. How do I get the row ID from this table? If I perform:
select * from person
I get -1 for Cursor.getColumnColumnIndex("rowid");. Also, Cursor.getColumnNames() only shows the two columns ('first' and 'last') and not the 'rowid'. Any suggestikns on how to solve this problem?
Try using:
select rowid,* from person
Rowid is a hidden field and is not included in "*".
Related
i have two tables, an FP_master table and a Take_Away_pack_details table. my first code below successfully copies data to column TakeAwayPkCost in table FP_master from column Pack_Cost in table Take_Away_Pack_Details.
In the second case I have a third table called View_Cart_Temp and I try to copy data from Column TakeAwayPKType in the FP_Master table to column Pack_type in the 3rd table View_Cart_temp. The 2nd piece of SQlite UPDATE code is shown below. The Syntax are identical in both cases and when i run this in my app, there is no error but the column is not updated. When I run this also in the DB Browser for SQLite, I get a message saying successfully updated but when i check the table its still null and no data has been copied. What could I be missing?
UPDATE FP_Master SET TakeAwayPkCost = (SELECT Pack_Cost FROM Take_Away_Pack_Details WHERE Pack_Type = TakeAwayPkType)
UPDATE View_Cart_Temp SET Pack_Type = (SELECT TakeAwayPkType FROM FP_Master WHERE Item = SKU)
I'm developing an Android app that uses a SQLite database with FTS4 tables.
In the app there's an option to import a database from the external memory. This database needs to be checked to confirm that it has all the correct tables and columns. I already have the code to do that however I don't know how to check if the tables are "normal" or FTS4. This will result in problems later on with queries with MATCH on them.
The only way I can think of to check if the tables are FTS4 is to do a random query with MATCH and if it gets an error it's because they are not.
Is there a better way to do this like with just a command?
Using MATCH on a plain table results in an error message only if the table has at least one row.
FTS table have a virtual column with the same name as the table name. So you could try a query like SELECT MyTable FROM MyTable.
You could check whether the shadow tables (MyTable_content, MyTable_segdir, etc.) exist.
You could check the CREATE TABLE statement in the system table: SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'MyTable';
I already read from here
FTS4 sqlite MATCH not working
and here
Full text search example in Android
but It can't solve my problem.
I have a external database and I use sqlite 3 version 3.13.0 with 2 table like this.
And I create a virtual table:
CREATE VIRTUAL TABLE tb_bank_fts USING fts4 (content="tb_bank",address, typename)
Virtual table create successfull and I can use select.
But I can't use "match" query with fts4 table, alway return null with no error.
Half month ago I can query fts4 table with "match" but now i can't do that.I don't know why. I try using SQlite Manager addon in Firefox but same problem.
The documentation says:
The FTS4 module never writes to the content table, and writing to the content table does not affect the full-text index. It is the responsibility of the user to ensure that the content table and the full-text index are consistent.
So when you've inserted data into the base table, you also have to insert it into the FTS table:
INSERT INTO tb_bank_fts(docid, address, typename)
SELECT rowid, address, typename FROM tb_bank;
or simply:
INSERT INTO tb_bank_fts(tb_bank_fts) VALUES('rebuild');
I am creating an FTS4 external content table in SQLite like this:
CREATE TABLE t2(id INTEGER PRIMARY KEY, col_a, col_b, col_text);
CREATE VIRTUAL TABLE fts_table USING fts4(content="t2", col_text);
I'm using an external content table so that I don't need to store duplicate values of col_text in fts_table. I'm only indexing col_text because col_a and col_b don't need to be indexed.
However, when I do a query of fts_table like this
SELECT * FROM fts_table WHERE fts_table MATCH 'something';
I don't have access to col_a and col_b from the content table t2. How do return all these columns (col_a, col_b, col_text) from a single FTS query?
Update
I tried using the notindexed=column_name option as in
CREATE VIRTUAL TABLE fts_table USING fts4(content="t2", col_a, col_b, col_text, notindexed=col_a, notindexed=col_b);
This should work for some people, but I am using it in Android and the notindexed option isn't supported until SQLite 3.8, which Android doesn't support until Android version 5.x. And I need to support android 4.x. I am updating this question to include the Android tag.
FTS tables have an internal INTEGER PRIMARY KEY column called docid or rowid.
When inserting a row in the FTS table, set that column to the primary key of the row in the original table.
Then you can easily look up the corresponding row, either with a separate query, or with a join like this:
SELECT *
FROM t2
WHERE id IN (SELECT docid
FROM fts_table
WHERE col_text MATCH 'something')
I am planning to build an app that lets the user select a record from a particular database such as:
name favorite_color favorite_team
sue red Dal
mike blue Mia
sam purple Bal
My problem is that most of the tutorials that I have come across only demonstrates examples using a table with one column. What if my pre-populated database more then on column? What if it had 100 columns? Does anyone know how this is done?????
Here's an example:
Step1:Declare SQLiteDatabase,Declare Databasehelper
Step2:Declare string or number which is to be used as key or get it as an intent(in this example 'rowid'
Step 3: In the OnCreate method add lines of code similar to following:
String query="select * from my_table_name where _id="+rowid;
Cursor myCursor = database.rawQuery(query,null);
myCursor.moveToFirst();
//This line implies i am getting data from column four of selected row
String x=myCursor.getString(4);
//This line implies i am getting data from column two of selected row
String y=myCursor.getString(4);
myCursor.close();
Note:
a)Don't forget that your database size must not exceed 1.2mb
b)Also include a column with name _id which auto-increments in each table that you are using,you may use sqlite browser to do so
c)also create the following table in your database :
CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US')
Now insert a single row with the text 'en_US' in the "android_metadata" table
INSERT INTO "android_metadata" VALUES ('en_US')
read this you will learn every thing you need to know. One other way to access db information is using an ORM like ormlite. I'm using it in various apps that i've developed, and it's simple to use.