I want to make SELECT * FROM my table and i don't want to sync colnames with colindex.
Is there a built-in way to fetch column names onto Cursor with columnNames instead of columnIndexes?
So i can use cursor.getStringForColumnName("name"); instead of knowing that 4th column is "name" column and using cursor.getString(4);
What I understood is that you want to use coulmn name in Cursor. You can actually use getColumnIndex(String columnName) to get the index automatically using column name. Read more here. Then you can continue with the operations. This way you don't need to remember the indexes. hope this helps else please comment.
well, YES you can use PRAGMA query for this purpose
Here, is an example
Cursor ti = db.rawQuery("PRAGMA table_info(mytable)", null);
if ( ti.moveToFirst() ) {
do {
System.out.println("col: " + ti.getString(1));
} while (ti.moveToNext());
}
cursor.getString(getColumnIndex("name"));
Related
I have a column in my SQLite table named 'img_name'. An example of data in that column: '/storage/extSdCard/img1.jpg /storage/extSdCard/img2.jpg /storage/extSdCard/pic3.jpg'. Lets say I wanted to delete an image. I would delete the corresponding word(path). Only problem is, I do not know how to delete a specific word(path) from that column.
Any help will be appreciated.
The way to "delete a specific word" is to update the existing value.
So you will need an UPDATE statement which selects the appropriate rows, and changes the value of the column. The new value will have to be "computed" from the old value, as you would do in a programming language, using string functions.
UPDATE column1
SET column1 = trim(replace(column1||' ','/storage/extSdCard/img2.jpg ',''))
WHERE column2 = 'example'
Note that this is an example only. The correct string manipulation required may be different. Your question does not specify your exact requirements.
Please consult the SQLite documentation and internet articles for details of string functions in SQLite.
Note that this would not be necessary if you didn't store more than one value in a column in each row.
You should get id of your string that you need to remove, and then pass it in to this:
public void deleteById(int id)
{
SQLiteDatabase db=getWritableDatabase();
String[] position =new String[]{id+""};
db.delete("img_name", "id=?", position );
}
Note: "id=?". Replace "id" in this statement by your id column
I want to do a rawquery and sort the result according to some alphanumeric value. Like I have column ItemId which contains values like A0001,A0002,A0036,B0085 etc.
String query="select S_PriceP,ItemId,ItemName from PDAProduct where ItemId<=A0001 and ItemId>=A0099 order by ItemId";
Is there any way I can achieve this?
You can use rawQuery.
String query="select S_PriceP,ItemId,ItemName from PDAProduct where ItemId<=A0001 and
ItemId>=A0099 order by ItemId";
Cursor objCursor = objSQLiteDatabase.rawQuery(query, null);
With the little info you have given us, my best answer would be to look at SQLiteDatabase docs... in particular, you want one of the query methods. If you were to post some of the code you have tried we might be able to find what you're missing.
I am trying to create a dictionary application on Android. I have a database of 80000 articles. When user enters a word in an EditText, I want to show suggestions in a ListView, To do that I use the following code:
public Cursor query(String entry){
String[] columns = new String[]{"_id", "word"};
String[] selectionArgs = new String[]{entry + "%"};
return mDB.query("word", columns, "word LIKE ?", selectionArgs, null, null, null);
}
and I use SimpleCursorAdapter for the ListView.
The problem is that suggestions appear very late. I think the reason is LIKE in the SQL. I do not know any other way to do that. Is there anything I can do to boost the performance of getting the suggestions?
You might find that adding an index on the word column helps a lot. See the documentation.
So you might try this, just after you create the table:
CREATE INDEX word_idx ON word (word);
(Note: I'm not sure if having the table and column both named word will cause syntax issues here. Try it and see!)
Aside from the obvious index you should look into using full text search with MATCH rather than like if this is a dictionary. Android should support FTS3.
Check out http://www.sqlite.org/fts3.html and some answers here on SO regarding fts3 on Android.
It seems that the words should start with the string.
Maybe this type of trick could help: SQLite FTS3 simulate LIKE somestring%
As a simple alternative, you can limit the suggestions in the arbitrary order with Limit as in this post: Using the LIMIT statement in a SQLite query
mDB.query("word", columns, "word LIKE ?", selectionArgs, null, null, null, "LIMIT 150" );
Since all the results are equally valid suggestions, the order will not matter.
Also you wonT be able to show crazy amount of suggestions anyway so you can simply use some fixed limit count depending on your UI. I gave 150 as an example.
Hope it helps..
I'm attempting to delete all rows in one table that do not have a corresponding ID in another table. Since apparently SQLite does not support joins in deletes I am trying to do something along these lines:
DELETE FROM my_table WHERE my_id NOT IN (SELECT _id FROM my_table2);
However, I apparently can not use rawQuery since it returns a cursor so I have to use the delete function. I'm having some trouble getting this working. Here is the query I'm trying:
mDb.delete("my_table", "my_id NOT IN ?", new String[]{"(SELECT _id FROM my_table2)"});
Thanks.
You shouldn't use a .rawQuery, as you stated, but you can use .execSQL() to accomplish it. I regularly use it for deletions myself.
I think the only way is to execute your select and dynamically combine your WHERE clause.
You have to use execSQL instead rawQuery because rawQuery is used for the statement that return data and execSQL is used for the statements that don't return data like DELETE
Android's API provides a clean mechanism via SQLite to make queries into the contact list. However, I am not sure how to limit the results:
Cursor cur = ((Activity)mCtx).managedQuery(
People.CONTENT_URI,
columns,
"LIMIT ? OFFSET ?",
new String[] { Integer.toString(limit), Integer.toString(offset) },
null
);
Doesn't work.
Actually, depending on the provider you can append a limit to the URI as follows:
uri.buildUpon().appendQueryParameter("limit", "40").build()
I know the MediaProvider handles this and from looking at recent code it seems you can do it with contacts too.
You are accessing a ContentProvider, not SQLite, when you query the Contacts ContentProvider. The ContentProvider interface does not support a LIMIT clause directly.
If you are directly accessing a SQLite database of your own, use the rawQuery() method on SQLiteDatabase and add a LIMIT clause.
I found out from this bug that Android uses the following regex to parse the LIMIT clause of a query:
From <framework/base/core/java/android/database/sqlite/SQLiteQueryBuilder.java>
LIMIT clause is checked with following sLimitPattern.
private static final Pattern sLimitPattern = Pattern.compile("\\s*\\d+\\s*(,\\s*\\d+\\s*)?");
Note that the regex does accept the format offsetNumber,limitNumber even though it doesn't accept the OFFSET statement directly.
I think you have to do this sort of manually. The Cursor object that is returned from the managedQuery call doesn't execute a full query right off. You have to use the Cursor.move*() methods to jump around the result set.
If you want to limit it, then create your own limit while looping through the results. If you need paging, then you can use the Cursor.moveToPosition(startIndex) and start reading from there.
You can specify the "limit" parameter in the "order" parameter, maybe even inside other parameters if you don't want to sort, because you'll have to specify a column to sort by then:
mContentResolver.query(uri, columnNames, null, null, "id LIMIT 1");