Skip to content Skip to sidebar Skip to footer

How To Check If The Database Connection Is Successful Or Not In Android?

I have tried several codes on using database. i am trying to use an existing database which i copy to assets folder. I want my pop up to display the outcome of an sql query. The co

Solution 1:

It is easy to test the database connectivity by sending some trivial SQL like

select1;

This should return exactly 1 and nothing else, and it will definitely fail when there is no connection to the database. Such query does not assume rights to create or access any tables, does not modify anything and works independently from the actual database content.

Solution 2:

The error messages you get indicate that a database file /data/data/com.example.singlepop/databases/MyDatabase does not exist. Can you check that?

Also, once this works, the Cursor represents all the results you get from the query. More than one record can be returned. You first position the cursor to the row you want to read and then you have access to the individual columns. For example (leaving out all error handling and assuming that your table has a column named col1):

check.moveToFirst();
Stringcol1= check.getString(check.getColumnIndex("col1"));

Solution 3:

I'm not sure I understand your question, but here's how you would setup and read from a database:

publicclassSQLTestextendsSQLiteOpenHelper {
privatestaticfinalStringDATABASE_NAME="TestDatabase.db";
privatestaticfinalintDATABASE_VERSION=1;
private Context context;

publicstaticfinalStringTABLENAME="mytable";
publicstaticfinalStringCOL_ID="_id";
publicstaticfinalStringCOL_NAME="name";


publicSQLSimple(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.context = context;
}

@OverridepublicvoidonCreate(SQLiteDatabase db) {
    StringSTATEMENT_CREATE="create table if not exists '"+TABLENAME+"' ("
        +COL_ID+" integer primary key autoincrement, "
        +COL_NAME+" text not null, "
    db.execSQL(STATEMENT_CREATE);
}

@OverridepublicvoidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    StringSTATEMENT_DROP="drop table if exists '"+TABLENAME+"';";
    db.execSQL(STATEMENT_DROP);

    onCreate(db);
}

public String readName(int id) {
    SQLiteDatabasedb= getReadableDatabase();
    Cursorcursor=null;
    try {
        //Return these two columns only. Here, we only have two of course, but in a bigger table this is good for performance.
        String[] projection = newString[]{COL_ID, COL_NAME};

        //The where part of the query that defines what we are looking forStringselection= COL_ID + " = ?";

        //The data that will replace the ? in the query before. Doing it this way is to prevent injection and escaping errors
        String selectionArgs[] = newString[]{id+""};
        cursor = db.query(TABLENAME, projection, selection, selectionArgs, null, null, null);

        Stringname=null;

        if(cursor.moveToFirst()) {
            Stringname= cursor.getString(cursor.getColumnIndex(COL_NAME));
        }

        return name;
    } finally {
        //Close the database before returning item, even in the case of an exceptionif (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
        if (db.isOpen()) {
            db.close();
        }
    }
}

I hope that this answers your question. If not, please ask.

Solution 4:

Your database file is in assets folder. And you are calling createDataBase() in which you first check for existence of database using checkDataBase().And then you are calling copyDataBase();

So when you try to open the database at /data/data/com.example.singlepop/databases/MyDatabase. in checkDataBase() you get that exception because the database is still in the /assets folder until you call copyDatabase()

ie.

  1. error opening trace file: No such file or directory
  2. android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database

You must first copy the database into the appropriate location before trying to open it.

I would suggest you to use SQLiteAssetHelper recommended by CommonsWare himself.

Post a Comment for "How To Check If The Database Connection Is Successful Or Not In Android?"