Invalid characters in sqlite query? - java

I'm having issues creating a valid sql query.
Im trying to check if a value, title is present in my db.
Here is the method in my adapter class that creates the query.
public Cursor byTitle(String title) throws SQLException {
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_URL
},
"title" + "=" +title,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
And its called onClick with
holder.fb.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final FDBAdapter db = new FDBAdapter(ctx);
title = holder.itemName.getText().toString();
db.open();
Cursor c = db.byTitle(title);
if (c.getCount()>0) {
while (c.moveToNext()) {
Toast.makeText(ctx, "yup! " + c.getString(0), Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(ctx, "nope! " + holder.itemName.getText().toString(), Toast.LENGTH_LONG).show();
}
c.close();
db.close();
}
});
My error is
android.database.sqlite.SQLiteException: near "Rootz": syntax error: , while compiling: SELECT DISTINCT url FROM favs WHERE title=[Guide] Rootz Wiki's List of ROMs / Kernels / Tweaks / Theme's / Radios LTE Edition
I've tried putting quotes around title like "\""+title+"\"" but that wasn't working for me either. How can I make this a valid db query?

Can you try this, might work.
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_URL
},
"title=?",
title,
null,
null,
null,
null);

Not familiar with SQLite, but I'd try single quotes, like this:
"title" + "='" + title + "'",

Related

i want to display username in my project using SQLite?

in my project after user sign up , i want to display the username in next page
so, this line works but with only first user sign up if another user sign up it will display the first username.
displName =findViewById(R.id.displayName);
displName.setText(database.getAllNote().get(0).getUserName());
my database
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void openDatabase() {
db = this.getWritableDatabase();}
#SuppressLint("Range")
public List<UserTasks> getAllNote(){
List<UserTasks> listNote = new ArrayList<>();
Cursor cur = null;
db.beginTransaction();
try{
cur = db.query(TABLE_NAME, null, null, null, null, null, null, null);
if(cur != null){
if(cur.moveToFirst()){
do{
UserTasks note = new UserTasks();
note.setUser_id(cur.getInt(cur.getColumnIndex(UID)));
note.setUserName(cur.getString(cur.getColumnIndex(UserName)));
note.setPassword(cur.getString(cur.getColumnIndex(Password)));
note.setNote(cur.getString(cur.getColumnIndex(Note)));
note.setStatus(cur.getInt(cur.getColumnIndex(STATUS)));
note.setDate(cur.getString(cur.getColumnIndex(date)));
note.setTitle(cur.getString(cur.getColumnIndex(titleChe)));
note.setDecs(cur.getString(cur.getColumnIndex(desc)));
note.setDay(cur.getString(cur.getColumnIndex(day)));
listNote.add(note);
}
while(cur.moveToNext());
}
}
}
finally {
db.endTransaction();
assert cur != null;
cur.close();
}
return listNote;
}
how i can fix this small issue? pls help
You always get the first sign up user because you always retrieve table index 0
displName.setText(database.getAllNote().get(0).getUserName());
So I suggest you to use SharedPreference to increment the count of signup user.
When user successfully signup you have to increase the number of user
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Int countUser = pref.getInt("count_user", 0); // getting Integer
Editor editor = pref.edit();
editor.putInt("count_user", countUser+1);
editor.apply();
After that, change your displName.setText to this:
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Int countUser = pref.getInt("count_user", 0); // getting Integer
displName.setText(database.getAllNote().get(countUser).getUserName());
You will always get latest signup username.

Recycler chat Pagination add new message at top

I am working on a chat app where I need to show reverse pagination and show the user's previous chat when the user scrolls up.
I am first loading data from SQLite database to show the last 10 messages of the user.
public Cursor getLimitUserChat(String UserID,int nextChat){
Log.d(TAG,"Get Single Row Running= ID "+UserID);
database=this.getReadableDatabase();
cursor = database.rawQuery("SELECT * FROM " + TABLE_NAME + " Where "+ RECEIVER_USERID +"="+ UserID+" ORDER BY ID DESC LIMIT 10 OFFSET "+nextChat,null);
return cursor;
}
Then I add a cursor to the custom ArrayList
private class databaseAsync extends AsyncTask<Void,Void,Void> {
boolean checkDB_Exist,chatItemsCounts;
private Parcelable recyclerViewState;
#Override
protected void onPreExecute() {
super.onPreExecute();
Log.d(TAG,"Chat Database Function "+databaseChatValue);
if (databaseChatValue == 0){
message.clear();
}
recyclerViewState = recyclerView.getLayoutManager().onSaveInstanceState();
//chatCursor=chat_database.getUserChat(UserID_Intent);
chatCount = chat_database.getUserChatCount(UserID_Intent);
chatCursor=chat_database.getLimitUserChat(UserID_Intent,databaseChatValue);
checkDB_Exist=functions.DatabaseExist(getActivity(),"CHAT_DATABASE.DB");
chatItemsCounts=chatCursor.getCount()>0;
chatCursor.moveToFirst();
Log.d(TAG,"Chat Cursor "+ chatCursor.getCount());
Log.d(TAG,"Total Chat Counts "+chatCount);
}
#Override
protected Void doInBackground(Void... voids) {
if (checkDB_Exist && chatCursor.getCount()>0 && chatCursor.getString(chatCursor.getColumnIndex("RECEIVER_USER_ID")).equals(UserID_Intent)) {
chatCursor.moveToFirst();
do {
database_rowID = chatCursor.getInt(chatCursor.getColumnIndex("ID"));
database_userID = chatCursor.getString(chatCursor.getColumnIndex("USER_ID"));
database_RoomName = chatCursor.getString(chatCursor.getColumnIndex("ROOM_NAME"));
database_ReceiverID = chatCursor.getString(chatCursor.getColumnIndex("RECEIVER_USER_ID"));
database_MessageType = chatCursor.getString(chatCursor.getColumnIndex("MESSAGE_TYPE"));
database_Message = chatCursor.getString(chatCursor.getColumnIndex("USER_MESSAGE"));
database_MsgFrom = chatCursor.getString(chatCursor.getColumnIndex("SENDER_NAME"));
database_MsgTo = chatCursor.getString(chatCursor.getColumnIndex("RECEIVER_NAME"));
database_TimeStamp = chatCursor.getString(chatCursor.getColumnIndex("TIME_STAMP"));
database_FCMfrom = chatCursor.getString(chatCursor.getColumnIndex("SENDER_TOKEN"));
database_FCMto = chatCursor.getString(chatCursor.getColumnIndex("RECEIVER_TOKEN"));
database_LocalPath = chatCursor.getString(chatCursor.getColumnIndex("DOWNLOADED_AT"));
database_PhoneFrom = chatCursor.getString(chatCursor.getColumnIndex("MY_PHONE"));
database_PhoneTo = chatCursor.getString(chatCursor.getColumnIndex("OTHER_PHONE"));
Log.d(TAG, "Value Of Database Message String = " + database_Message);
Log.d(TAG, "Row ID of Database " + database_rowID);
// Check Message Type
if (true) {
if (database_MessageType.equals("Image")) {
Log.d(TAG, "Message Type Is Image");
Log.d(TAG, "Row ID of Database " + database_rowID);
Chat_Wrapper image = new Chat_Wrapper(null, database_Message, null, null, null, null, null, database_TimeStamp, database_PhoneTo, UserImage_Intent, database_MsgFrom, null, null, database_rowID);
message.add(image);
} else if (database_MessageType.equals("GIF")) {
Chat_Wrapper image = new Chat_Wrapper(null, database_Message, null, null, null, null, null, database_TimeStamp, database_PhoneTo, UserImage_Intent, database_MsgFrom, null, null, database_rowID);
message.add(image);
} else if (database_MessageType.equals("Video") || (database_MessageType.equals("Youtube_Video"))) {
Log.d(TAG, "Message Type Is Video");
Chat_Wrapper Video = new Chat_Wrapper(null, null, database_Message, null, null, null, null, database_TimeStamp, database_PhoneTo, UserImage_Intent, database_MsgFrom, null, null, database_rowID);
message.add(Video);
} else if (database_MessageType.equals("Audio")) {
Log.d(TAG, "Message Type Is Audio");
Chat_Wrapper Audio = new Chat_Wrapper(null, null, null, database_Message, null, null, null, database_TimeStamp, database_PhoneTo, UserImage_Intent, database_MsgFrom, null, null, database_rowID);
message.add(Audio);
} else if (database_MessageType.equals("Documents")) {
Log.d(TAG, "Message Type Is Documents");
Chat_Wrapper Docs = new Chat_Wrapper(null, null, null, null, database_Message, null, null, database_TimeStamp, database_PhoneTo, UserImage_Intent, database_MsgFrom, null, null, database_rowID);
message.add(Docs);
} else if (database_MessageType.equals("Location")) {
Log.d(TAG, "Message Type Is Location");
Chat_Wrapper Location = new Chat_Wrapper(null, null, null, null, null, database_Message, null, database_TimeStamp, database_PhoneTo, UserImage_Intent, database_MsgFrom, null, null, database_rowID);
message.add(Location);
} else if (database_MessageType.equals("Geofence")) {
Log.d(TAG, "Message Type Is Geofence");
Chat_Wrapper GeoFence = new Chat_Wrapper(null, null, null, null, null, null, null, Chat_TimeStamp, UserPhone_Intent, UserImage_Intent, database_MsgFrom, database_Message, null, database_rowID);
message.add(GeoFence);
} else if (database_MessageType.equals("Contacts")) {
Log.d(TAG, "Message Type Is Contacts");
Chat_Wrapper Contacts = new Chat_Wrapper(null, null, null, null, null, null, database_Message, database_TimeStamp, database_PhoneTo, UserImage_Intent, database_MsgFrom, null, null, database_rowID);
message.add(Contacts);
} else if (database_MessageType.equals("Online Media")) {
Log.d(TAG, "Message Type Is Online Media");
Chat_Wrapper Online_Media = new Chat_Wrapper(null, null, database_Message, null, null, null, null, database_TimeStamp, database_PhoneTo, UserImage_Intent, database_MsgFrom, null, null, database_rowID);
message.add(Online_Media);
} else if (database_MessageType.equals("Text")) {
Log.d(TAG, "Message Type Is Text");
Chat_Wrapper text = new Chat_Wrapper(database_Message, null, null, null, null, null, null, database_TimeStamp, database_PhoneTo, UserImage_Intent, database_MsgFrom, null, null, database_rowID);
message.add(text);
} else if (database_MessageType.equals("Google_Search")) {
Log.d(TAG, "Message Type Is Google Search");
List<String> gSearch = new ArrayList<>();
gSearch.add(database_Message);
Chat_Wrapper GoogleSearch = new Chat_Wrapper(null, null, null, null, null, null, null, database_TimeStamp, database_PhoneTo, UserImage_Intent, database_MsgFrom, null, gSearch, database_rowID);
message.add(GoogleSearch);
}
}
}
while (chatCursor.moveToNext()) ;
Room_Name_Intent = database_RoomName;
chatCursor.close();
boolean value = chat_database.isDatabaseClose();
Log.d(TAG, "Value Of Database Close or Not " + value);
}
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
recyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);
adapter.notifyDataSetChanged();
if (message.size()>2){
recyclerView.smoothScrollToPosition(message.size()-1);
}else {
recyclerView.smoothScrollToPosition(layoutManager.getItemCount());
}
Log.d(TAG,"Chat Database "+database_OFFSET);
database_OFFSET +=10;
Log.d(TAG,"Chat Database "+database_OFFSET);
}
}
I also have made RecyclerView and LayoutManager settings like this
layoutManager.setStackFromEnd(true);
layoutManager.setReverseLayout(true);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
At first load, it fetches total 10 result from the local database but as soon as it loads into screen first, it loads in the correct order (Latest message at the end and older at the top). But RecyclerView doesn't scroll at bottom automatically. At the start, it loads from the top and when I scroll it calls the following method.
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL){
Log.d(TAG,"Hello I am scrolling screen ");
isScrolling = true;
}
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
currentVisible = layoutManager.getChildCount();
TotalItems = layoutManager.getItemCount();
scrolledOutItems = layoutManager.findFirstVisibleItemPosition();
int check = TotalItems - currentVisible+scrolledOutItems;
Log.d(TAG,"Current Visible = "+currentVisible+" Total = "+TotalItems+" Scrolled Out Items = "+scrolledOutItems+" Check = "+check);
if (isScrolling && TotalItems == currentVisible + scrolledOutItems ){
Log.d(TAG,"Fetch Data Now "+databaseChatValue);
if (chatCount > databaseChatValue){
Log.d(TAG,"Total item count is more than database = "+chatCount +" "+databaseChatValue);
new databaseAsync().execute();
isScrolling = false;
}
}
}
});
And loads another into screen.
Issues In Code
When Set layoutManager.setStackFromEnd(true); without layoutManager.setReverseLayout(true); pagination works at bottom of last message and also new message adds at bottom
When Set layoutManager.setReverseLayout(true); pagination works as required but new message stacked at top of the oldest message.
Update :I notice my newest message has index number in array [0] and my last message after scrolls to oldest message [21]. So when i typed new message into my chat box it assign to [22] in array and shows above oldest message at top not at the bottom of the screen.
Whenever you add new item into your chat screen(arraylist) dont go with message.add(Video); instead add item at 0 position like this message.add(0,Video); which will add items at bottom of screen(top of array list) than adding top of the screen(last of array list). It will be add into start position of array i.e. [0] in your case.
Update
Now I think, the problem is with your database query. I assume that whenever a new message is typed, it is stored in the database with the highest ID that makes it the latest in your database table organization. Hence, the getLimitUserChat should never return the latest message if you are setting the offset correctly.
I do not know what the dataBaseAsync.execute function do. I assume, it adds more elements into the ArrayList and calls the notifyDateSetChanged.
Hence the data should be populated in your RecyclerView in proper order if the addition of the newly fetched data to the ArrayList is correct. However, you might also notice that each time new paginated data is adding, your RecyclerView might scroll to the bottom as the reverse layout is enabled - which you might not want for a chat application.
Hence to keep the RecyclerView at the same place before loading new data you need to save the state of the RecyclerView and restore the state when the new data is fetched. Here is the code for storing the RecyclerView state and getting back into the same state.
// Save state
private Parcelable recyclerViewState;
recyclerViewState = recyclerView.getLayoutManager().onSaveInstanceState();
// Restore state
recyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);
And I would like to suggest you set the LayoutManager like the following.
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
Please double check if the ArrayList is populated correctly (i.e. new data at the top of the list and older data at the bottom).
Hope that helps!
Please remove the following from your onPostExecute.
if (message.size() > 2){
recyclerView.smoothScrollToPosition(message.size()-1);
} else {
recyclerView.smoothScrollToPosition(layoutManager.getItemCount());
}
And also add necessary null checkings in the preExecute.

how to get information of music files from assets folders?

I have following code which reads my phone and gets information of the music files in my phone
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String[] projectionSongs = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DURATION};
Cursor cursor = mContext.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projectionSongs,
selection, null, null);
Now I want to same information from assets folder itself.
I am trying
Uri uri = Uri.parse("file:///assets/");
Cursor cursor = mContext.getContentResolver().query(uri, projectionSongs,
selection, null, null);
But it is not working. There is no information in cursor. However I have some mp3 files in assets folder.
public void playBeep() {
try {
if (m.isPlaying()) {
m.stop();
m.release();
m = new MediaPlayer();
}
AssetFileDescriptor descriptor = getAssets().openFd("your_audio_file_name.mp3");
m.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
m.prepare();
m.setVolume(1f, 1f);
m.setLooping(true);
m.start();
} catch (Exception e) {
e.printStackTrace();
}
}
Hope this will help u. Thank you

How to get a list of all audio files using android.database.Cursor?

I came across the solution of this problem through Cursor,
public class MainActivity extends AppCompatActivity {
void example() {
List<File> localAudioFiles = new LinkedList<>();
Uri contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.Images.Media.DATA};
// The problem is that it contains a list of files as of the moment
// when the operating system has been rebooted.
try (Cursor cursor = getContentResolver().query(
contentUri,
projection,
MediaStore.Audio.Media.IS_MUSIC + " != 0",
null,
null);) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
while (cursor.moveToNext()) {
String audio = cursor.getString(column_index);
localAudioFiles.add(new File(audio));
}
} catch (RuntimeException ex) {
}
}
}
but as far as I understand, its state needs to be updated compulsorily, because files added through the AVD file manager are not detected in this way until the device is rebooted.
How can I forcefully update the state of the entity Cursor?
Is the solution worth it, or is it not better than recursively traversing all the directories?

android.database.CursorIndexOutOfBoundsException...once again

So, I have this error in user crash report for my app. Here's the report:
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at rs.androidaplikacije.zastaveigradovi.Kviz10Hard.nextQuestionGrad(Kviz10Hard.)
at rs.androidaplikacije.zastaveigradovi.Kviz10Hard.access$1(Kviz10Hard.)
at rs.androidaplikacije.zastaveigradovi.Kviz10Hard$2.run(Kviz10Hard.java:68)
at android.os.Handler.handleCallback(Handler.)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.)
at android.app.ActivityThread.main(ActivityThread.)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.)
at dalvik.system.NativeStart.main(Native Method)
And here's the mentioned code:
private void nextQuestionGrad() {
flag.setVisibility(View.GONE);
dodatnoPitanje.setVisibility(View.VISIBLE);
TestAdapter mDbHelper = new TestAdapter(this);
DataBaseHelper myDbHelper = new DataBaseHelper(this);
if(!myDbHelper.checkDataBase()){
mDbHelper.createDatabase();
}
try{
mDbHelper.open();
Cursor c = mDbHelper.getTestDataGradovi(mCurrentID);
List<Answer> labels = new ArrayList<Answer>();
labels.add(new Answer(c.getString(2), true));
labels.add(new Answer(c.getString(3), false));
labels.add(new Answer(c.getString(4), false));
labels.add(new Answer(c.getString(5), false));
tacanOdg = c.getString(2);
Collections.shuffle(labels);
dodatnoPitanje.setText(c.getString(1));
bOdgovor1.setText(labels.get(0).option);
bOdgovor1.setTag(labels.get(0));
bOdgovor1.setOnClickListener(clickListenerGrad);
bOdgovor2.setText(labels.get(1).option);
bOdgovor2.setTag(labels.get(1));
bOdgovor2.setOnClickListener(clickListenerGrad);
bOdgovor3.setText(labels.get(2).option);
bOdgovor3.setTag(labels.get(2));
bOdgovor3.setOnClickListener(clickListenerGrad);
bOdgovor4.setText(labels.get(3).option);
bOdgovor4.setTag(labels.get(3));
bOdgovor4.setOnClickListener(clickListenerGrad);
score.setText("Score: " + brojacTacnihOdgovora);
}finally{
mDbHelper.close();
}
}
And part of my adapter class I'm using for this class:
public Cursor getTestDataGradovi(long id){
try{
String sql ="SELECT * FROM tblGradovi WHERE _ID = " + id;
Cursor c = mDb.rawQuery(sql, null);
if (c!=null){
c.moveToNext();
}
return c;
}catch (SQLException mSQLException){
Log.e(TAG, "getTestData >>"+ mSQLException.toString());
throw mSQLException;
}
}
Can someone tell me where's the problem? The error line (Kviz10Hard.) is here:
labels.add(new Answer(c.getString(2), true));
Before trying to get any data from a Cursor object you need to iterate the result or get the first one if the cursor contains only one value. For example to iterate between all result you should do something like this :
String sql = "SELECT * FROM myTable";
Cursor cursor = mDbHelper.rawQuery(sql, null);
if(cursor != null){
if(cursor.getCount() > 0){
for(cursor.move(0);cursor.moveToNext();cursor.isAfterLast()){
// get your data here
}
}
cursor.close(); // close your cursor when you don't need it anymore
}
or another way to do this it :
String sql = "SELECT * FROM myTable";
Cursor cursor = mDbHelper.rawQuery(sql, null);
if(cursor != null){
if(cursor.getCount() > 0){
do {
// get value
} while(cursor.moveToNext());
}
cursor.close(); // close your cursor when you don't need it anymore
}
or if you are pretty sure that the result should be only one object depending on your sql statement you should call cursor.moveToFirst(); before accessing anything from your Cursor like this :
String sql = "SELECT * FROM myTable";
Cursor cursor = mDbHelper.rawQuery(sql, null);
if(cursor != null){
cursor.moveToFirst();
String myString = cursor.getString(cursor.getColumnIndex("title"));
cursor.close(); // close your cursor when you don't need it anymore
}
Edit:
If you really want to create function like this in your database class I would do something like this :
public static Cursor getUserTitles(int userId){
String sql = "SELECT * FROM myTable WHERE userId=?";
return mDbHelper.rawQuery(sql, new String[]{Integer.toString(userId)});
}
Second Edit:
Change your getTestDataGradovi to :
public Cursor getTestDataGradovi(long id){
String sql ="SELECT * FROM tblGradovi WHERE _ID=?";
return mDb.rawQuery(sql, new String[]{Integer.toString(id)});
}
and add c.moveToFirst(); , after Cursor c = mDbHelper.getTestDataGradovi(mCurrentID);
You are not checking that the Cursor contains any results. You need to check the return value of c.moveToNext(), or check c.getCount() or something similar.

Categories

Resources