2

I am developing an Android application that creates a SQLite onCreate and queries records. All is working on my Android Emulator but when I deploy / debug on my Android Device (Samsung Galaxy 7 Edge) the database seems empty.

I have tested it on my emulator and works fine. Why is the SQLite not seems to be created / queried on my phone? Do I need additional permissions in my manifest?

Please help. Below is my code

package com.example.android.cebuanotagalogtranslator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    Button btn_clear;
    Button btn_translate_to_ceb;
    Button btn_translate_to_fil;
    EditText txt_input;
    EditText txt_output;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //HABAGAT CONTROLS BEGIN
        btn_clear = (Button) findViewById(R.id.btn_clear);
        btn_translate_to_ceb = (Button) findViewById(R.id.btn_trans_to_ceb);
        btn_translate_to_fil = (Button) findViewById(R.id.btn_trans_to_fil);

        txt_input = (EditText) findViewById(R.id.input);
        txt_output = (EditText) findViewById(R.id.output);

        //HABAGAT : CLEAR BOTH TEXT
        btn_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                txt_input.setText("");
                txt_output.setText("");
            }
        });

        //: FILIPINO -> CEBUANO
        btn_translate_to_ceb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                try {
                    String textinput = txt_input.getText().toString();
                    textinput = "\""+ textinput +"\"";
                    String filToCebQuery = "SELECT ceb FROM filtoceb WHERE fil = "+ textinput+" "+"COLLATE NOCASE";
                    SQLiteDatabase DB = openOrCreateDatabase("filtoceb", MODE_PRIVATE, null);
                    //Cursor c = DB.rawQuery("SELECT * FROM filtoceb", null);
                    Cursor c = DB.rawQuery(filToCebQuery, null);


                    if (c!=null)
                    {
                        System.out.println("RESULT FOUND : FIL -> CEB");
                        try {
                            c.moveToFirst();
                            while(!c.isAfterLast()){
                                String result = c.getString(c.getColumnIndex("ceb"));
                                txt_output.setText(result);
                                c.moveToNext();
                            }
                        }
                        finally
                        {
                            c.close();
                        }
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

        //: CEBUANO -> FILIPINO
        btn_translate_to_fil.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                try {
                    String textinput = txt_input.getText().toString();
                    textinput = "\""+ textinput +"\"";
                    String filToCebQuery = "SELECT fil FROM filtoceb WHERE ceb = "+ textinput+" "+"COLLATE NOCASE";
                    SQLiteDatabase DB = openOrCreateDatabase("filtoceb", MODE_PRIVATE, null);

                    Cursor c = DB.rawQuery(filToCebQuery, null);


                    if (c!=null)
                        System.out.println("RESULT FOUND : CEB -> FIL ");
                    {
                        try {
                            c.moveToFirst();
                            while(!c.isAfterLast()){
                                String result = c.getString(c.getColumnIndex("fil"));
                                txt_output.setText(result);
                                c.moveToNext();
                            }
                        }
                        finally
                        {
                            c.close();
                        }
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

        // CREATE DB OPEN IF NOT CREATED YET
        try {
            SQLiteDatabase eventsDB = this.openOrCreateDatabase("filtoceb", MODE_PRIVATE, null);

            //DELETE INITIALIZE SETUP START CLEAN
            eventsDB.delete("filtoceb", "1", null);

            eventsDB.execSQL("CREATE TABLE IF NOT EXISTS filtoceb (fil VARCHAR, ceb VARCHAR)");
            eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Kumusta ka?','Kumusta ka?')");
            eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Maayo, salamat', 'Mabuti naman, salamat')");
            eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Unsay imong pangalan?',  'Ano pangalan mo?')");
            eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Unsay ngalan mo?', 'ano pangalan mo?')");
            eventsDB.execSQL("INSERT INTO filtoceb (CEB, FIL) values ('Maayo nga nagka-ila ta', 'Ikinagagalak kitang makilala')");
            eventsDB.execSQL("INSERT INTO filtoceb (CEB, FIL) values ('Palihug','Please')");          

            eventsDB.close();

        } catch (Exception e) {
            e.printStackTrace();
        }


    }


}

below is my manifest:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
royjavelosa
  • 2,048
  • 6
  • 30
  • 46
  • share your dbhelper class – Bek Nov 30 '17 at 13:31
  • 3
    Please edit your question and provide a [mcve]. That would include your code that works with SQLite and your test case that illustrates how you are determining that "the database seems empty". – CommonsWare Nov 30 '17 at 13:31
  • Which api level is on your emulator? – Bek Nov 30 '17 at 13:33
  • My emulator is using api 26 – royjavelosa Nov 30 '17 at 13:36
  • Should I add .db to the db name? – royjavelosa Nov 30 '17 at 13:37
  • @commonsware I already tested it over and over on my emulator. The issue is when launching/debugging it on my device it does not return any record – royjavelosa Nov 30 '17 at 13:44
  • @Bek my android emulator is using api 26. Is there additional configurations if its api 23 or above ? – royjavelosa Nov 30 '17 at 13:45
  • try to use SQLiteOpenHelper https://developer.android.com/training/data-storage/sqlite.html – Bek Nov 30 '17 at 13:58
  • Your code is... unusual. You might want to examine LogCat, in case you are getting exceptions somewhere along the line. You might also consider setting breakpoints and seeing what is going on in your various bits of database code. – CommonsWare Nov 30 '17 at 14:04
  • Try moving `eventsDB.delete("filtoceb", "1", null);` to after the line that creates the table i.e. after `eventsDB.execSQL("CREATE TABLE IF NOT EXISTS filtoceb (fil VARCHAR, ceb VARCHAR)");` but before any of the inserts. – MikeT Nov 30 '17 at 19:11
  • @MikeT please create an answer so I can mark it as the CORRECT ANSWER!!! It took me hours to catch that thank you brother. – royjavelosa Nov 30 '17 at 19:42

1 Answers1

1

I believe that your issue may be due to the location of the following line.

        eventsDB.delete("filtoceb", "1", null);

In the case, that a database does not exist, this would result in a failure as at the time when you execute this line the tabled wouldn't exist.

The solution is to move the line until after until after creating the table.

So instead of :-

    try {
        SQLiteDatabase eventsDB = this.openOrCreateDatabase("filtoceb", MODE_PRIVATE, null);

        //DELETE INITIALIZE SETUP START CLEAN
        eventsDB.delete("filtoceb", "1", null);

        eventsDB.execSQL("CREATE TABLE IF NOT EXISTS filtoceb (fil VARCHAR, ceb VARCHAR)");
        eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Kumusta ka?','Kumusta ka?')");
        eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Maayo, salamat', 'Mabuti naman, salamat')");
        eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Unsay imong pangalan?',  'Ano pangalan mo?')");
        eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Unsay ngalan mo?', 'ano pangalan mo?')");
        eventsDB.execSQL("INSERT INTO filtoceb (CEB, FIL) values ('Maayo nga nagka-ila ta', 'Ikinagagalak kitang makilala')");
        eventsDB.execSQL("INSERT INTO filtoceb (CEB, FIL) values ('Palihug','Please')");          

        eventsDB.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

use :-

    try {
        SQLiteDatabase eventsDB = this.openOrCreateDatabase("filtoceb", MODE_PRIVATE, null);

        eventsDB.execSQL("CREATE TABLE IF NOT EXISTS filtoceb (fil VARCHAR, ceb VARCHAR)");
        //DELETE INITIALIZE SETUP START CLEAN
        eventsDB.delete("filtoceb", "1", null);
        eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Kumusta ka?','Kumusta ka?')");
        eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Maayo, salamat', 'Mabuti naman, salamat')");
        eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Unsay imong pangalan?',  'Ano pangalan mo?')");
        eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Unsay ngalan mo?', 'ano pangalan mo?')");
        eventsDB.execSQL("INSERT INTO filtoceb (CEB, FIL) values ('Maayo nga nagka-ila ta', 'Ikinagagalak kitang makilala')");
        eventsDB.execSQL("INSERT INTO filtoceb (CEB, FIL) values ('Palihug','Please')");          

        eventsDB.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
MikeT
  • 51,415
  • 16
  • 49
  • 68