ankita gahoi
ankita gahoi

Reputation: 1562

Sqlite android no such table exists

I am using sqlite database in Android. I'm not creating any database I'm using created database and it has successfully opened but when I am using

Cursor cursor= db.query(tablename,  null, null, null, null, null, null);

then it gives this table does not exists. Please let me know complete procedure for accessing sqlite databases in Android.

Upvotes: 0

Views: 5864

Answers (1)

Anju
Anju

Reputation: 9479

Create a class DatabaseHelper...

public class DBHelper extends SQLiteOpenHelper {

private static String dbPath = "ur database path";
private static String dbName = "ur database name";
private static SQLiteDatabase db;
private static DBHelper databaseHelper = null;
public final Context context;

public DBHelper(Context context) {
    super(context, dbName, null, 1);
    this.context = context;
}
public static DBHelper getInstance(Context context) {
    if (databaseHelper == null) {
        databaseHelper = new DBHelper(context);
        databaseHelper.openDataBase();

        if (db == null) {
            try {
                db = databaseHelper.getWritableDatabase();
                databaseHelper.copyDataBase();
            } 
            catch (Exception e) {
                Log.i(LOGTAG, "Error in database creation");
            }

            databaseHelper.openDataBase();
        }
    }
    return databaseHelper;
}

/** 
 * To return the database. 
 */
public SQLiteDatabase getDatabase() {
    return db;
}

/** 
 * To open the database. 
 */
private boolean openDataBase() throws SQLException {

    String path = dbPath + dbName;
    try {

        db = SQLiteDatabase.openDatabase(path, null,
                SQLiteDatabase.OPEN_READWRITE);
    } 
    catch (SQLiteException e) {
        Log.i(LOGTAG, "Error in openDataBase method");
        // TODO: handle exception
    }
    return db != null ? true : false;
}
private void copyDataBase() throws IOException {

    String outFileName = dbPath + dbName;
    OutputStream output = null;

    try {
        output = new FileOutputStream(outFileName);
    } 
    catch (IOException e) {
        Log.i(LOGTAG, "Error in copyDataBase method");
    }

    output.flush();
    output.close();

    SchemaCreator schemaCreator = new SchemaCreator(db);
    try {

        schemaCreator.createTables();

    }
    catch (SQLiteException e) {
        Log.i(LOGTAG, "Error in createTables method");
    }
}
@Override
public synchronized void close() {
    if (db != null)
        db.close();
    super.close();
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

After that create a class for fetching details from the database...

public class DBFetch {

private SQLiteDatabase db = null;
private Cursor cursor = null;
DBHelper myDbHelper;

public DBFetch(Context context) {
    myDbHelper = DBHelper.getInstance(context);
    db = myDbHelper.getDatabase();
}   

private void fetchDetails() {
    String sql = "SELECT * FROM employees";
    cursor = db.rawQuery(sql, null);
        if (cursor.getCount() > 0) {
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                  //************do your operations*********

            }
       }
}

Upvotes: 1

Related Questions