Inserting record in sqlite results in duplicate record - android

When i insert one record then duplicate entry is generated that means total two same entries are display. My query for select statement is following:
How to make distinct record???
Cursor cur = db.query("timer", null, null, null, null, null, null);

To make a distinct selection define a boolean Variable like :
private boolean Distinct = true;
then change your query to :
c = db.query(Distinct,DATABASE_TABLE,cols,null,null, null, null,
null,null);

Related

retrieve particular column between different columns of sqlite databse in android

Here is the code, by this I can retrieve all the columns data from the database. But the problem is that now I want to retrieve only one column, that is my requirement.link1st link 2nd link3rd
word_list = new ArrayList<>();
SQLiteDatabase sd = mydb.getWritableDatabase();
Cursor cursor = sd.query("stickerstable",null, null, null, null, null, null);
if (cursor.moveToFirst()){
do{
word_list.add(new data_items(
cursor.getInt(cursor.getColumnIndex(ID)),
cursor.getString(cursor.getColumnIndex(STICKER_AUTHOR)),
cursor.getString(cursor.getColumnIndex(STICKER_NAME)
)));
}while (cursor.moveToNext());
cursor.close();
sd.close();
}
You can use rawQuery instead of query
Cursor cursor = sd.rawQuery("SELECT YOUR_COLUMN FROM stickerstable",null);
replace YOUR_COLUMN with the name of column you want to retrieve you can even use your Constants like this
Cursor cursor = sd.rawQuery("SELECT " + ID + " FROM stickerstable",null);
or a better way to use String.format
Cursor cursor = sd.rawQuery(String.format("SELECT %s FROM stickerstable", ID),null);
UPDATE
you can use query and specify the column in the second argument
Cursor cursor = sd.query("stickerstable",new String[]{ID}, null, null, null, null, null);
when you pass null means get all columns

best way to create sort order in the Cursor in android?

Is creating different cursor for creating sort orders and distribute it in an model clases for alternating sorting style in an listview in android good practice?
titleCursor = context.getContentResolver().query(sourceUri, projection,
null, null, orderByTitle);
timeCursor = context.getContentResolver().query(sourceUri, projection,
null, null, orderByTime);
dateCursor = context.getContentResolver().query(sourceUri, projection,
null, null, orderByDate);
and values of its cursor will be destributed to each models to get the cursor for sorting for alternating sorting in listview? is it a good practice?
You don't have to create a new cursor. You can just change the sort value for the query, if all other things remain constant. That's why it's a parameter, so you alter it's value upon execution of the query.
Instead of creating a different cursor for each sort, you could maintain a string value or some integer value and based on the value inside an if condition simply change the sort parameter like:
if(value.equals("time")) {
cursor = context.getContentResolver().query(sourceUri, projection,
null, null, orderByTime);
}else if(value.equals("title")) {
cursor = context.getContentResolver().query(sourceUri, projection,
null, null, orderByTitle);
}
and so on.

Android SQLite distinct select?

I have created a Sqlite database in my android app, it looks like this:
create table msgs ( id integer primary key autoincrement, msg text not null, session text not null, sender text not null);
I can get all the entry's like this, but I don't fly understand what is happening.
String[] allColumns = {"msg","session","sender"};
Cursor cursor = database.query(msgs, allColumns, id = insertId, null, null, null, null);
What i could like to do, is to only get the latest entry with a distinct session how can i do this in android?
Edit: If this was mysql i whould do "SELECT MAX(id) AS id2,msg, session FROM msgs GROUP BY session"
But cant seam to get it to work in SQLite :/
To execute a complete SQL query, you could use rawQuery:
cursor = database.rawQuery("SELECT MAX(id) AS id2,msg, session FROM msgs GROUP BY session", null);
With query, you would have to set the parameters like this:
cursor = database.query("msgs",
new String[] { "MAX(id) AS id2", "msg", "session" },
null, null,
"session",
null, null);
Please note that using an unaggregated colum (msg) in an aggregated query does not work before SQLite version 3.7.11 (Android API version 16, Jelly Bean).
Always check documentation and be careful with types!
Your using following method:
query(
msgs, //String table: OK
allColumns, //String[] columns: OK
id = insertId, //String selection: NOT OK
// ^--- this is a boolean which will render SQL something like
// "SELECT ... WHERE TRUE ..." or "SELECT ... WHERE FALSE ..."
// causing all rows or none to be displayed
null, //String[] selectionArgs: OK
null, //String groupBy: OK
null, //String having: OK
null); //String orderBy: OK
Correction:
query(
msgs, //table
allColumns, //columns
"id = ?", //selection
new String[] {String.valueOf(insertId)}, //selectionArgs
null, //groupBy
null, //having
null); //orderBy

SQLiteException no such column

I am writing an SQL query like this:
Cursor data = db.query(WhereWolfOpenHelper.FRIEND_TABLE_NAME, null, WhereWolfOpenHelper.FRIEND_GROUP_COLUMN+"="+params[0], null, null, null, null);
And it's producing the following error:
ERROR/AndroidRuntime(620): Caused by: android.database.sqlite.SQLiteException: no such column: Friends: , while compiling: SELECT * FROM ww_friend WHERE friend_group=Friends
There is no column called Friends, I want to retrieve the rows where the friend_group column has the value Friends. What am I doing wrong?
you need to surround Friend value with quote :
Cursor data = db.query(WhereWolfOpenHelper.FRIEND_TABLE_NAME, null, WhereWolfOpenHelper.FRIEND_GROUP_COLUMN+"='"+params[0]+"'", null, null, null, null);
PS: be sure to escape dangerous characters from param[0] when you do like this to avoid SQL Injection
I'm guessing that "Friends" is a value, so you must enclose it within quotes.
Try this:
Cursor data = db.query(WhereWolfOpenHelper.FRIEND_TABLE_NAME, null, WhereWolfOpenHelper.FRIEND_GROUP_COLUMN+"=\""+params[0]+"\"", null, null, null, null);

Android SQLiteDatabase: Reading one column

I have a one row database just for saving app data. My goal is to read one column (one value) from it.
This query returns all the columns in a Cursor:
public Cursor readAll() {
return getReadableDatabase().query(tableName, null, null, null, null, null, null);
}
It returns a Cursor with one row in it, just perfect. However, I don't want to read all columns at once, because it's slow as I have blob's in db too.
Instead, I'd like to read just one column at a time, separately. For example, for a column called "TEXT" it would be this:
public Cursor readText() {
String[] projection = new String[]{"TEXT"};
return getReadableDatabase().query(tableName, projection, null, null, null, null, null);
}
However, this won't work, as I get back a Cursor with zero rows.
So, how to read a specific column from SQLiteBatabase in Android?
public Cursor readText() {
return getReadableDatabase().rawQuery("SELECT colName FROM myTable", new String[] {});
}
Syntax seems to be correct. Check please that you use right name of the column. Showing the table generation code and actual query code could help.
You can use this one also
public Cursor readText() {
return getReadableDatabase().rawQuery("SELECT column_name FROM table_name", null);
}

Categories

Resources