Shashank Shekhar Shringi Shashank Shekhar Shringi - 5 days ago 4
Android Question

Getting SQLiteException while calling getReadableDatabase

Above code is the part of intent service where I am accessing GinisTable class which is actually a DBHelper class.

protected void onHandleIntent(Intent intent) {
if (intent != null) {
DeviceInfoTable deviceInfoTable = new DeviceInfoTable(UploadGiniData.this);
DeviceInfo deviceInfo = deviceInfoTable.getDeviceInfo();

String generatedDeviceInfo = deviceInfo.getGeneratedDeviceId();

GinisTable ginisTable = new GinisTable(UploadGiniData.this);
List<GiniInfo> giniInfoList = ginisTable.getGiniInfoList();
}


My GinisTable class is as below:

package com.mastishka.pocketgini.database.tables;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

import com.mastishka.pocketgini.Config;
import com.mastishka.pocketgini.database.beans.GiniInfo;

import java.util.ArrayList;
import java.util.List;

public class GinisTable extends SQLiteOpenHelper{

public static final String TABLE_GINI = "ginis";
public static final String FIELD_GINI_ID = "gini_id";
public static final String FIELD_NAME = "name";
public static final String FIELD_PROFESSION = "profession";
public static final String FIELD_GINI_PIC_PATH = "gini_pic_path";
public static final String FIELD_EXPERIENCE = "experience";
public static final String FIELD_PRIMARY_MOBILE = "primary_mobile";
public static final String FIELD_ISD_CODE = "isd_code";
public static final String FIELD_GENDER = "gender";
public static final String FIELD_DOB = "dob";
public static final String FIELD_LATITUDE = "latitude";
public static final String FIELD_LONGITUDE = "longitude";
public static final String FIELD_ADDRESS_LINE1 = "address_line1";
public static final String FIELD_ADDRESS_LINE2 = "address_line2";
public static final String FIELD_CITY = "city";
public static final String FIELD_STATE = "state";
public static final String FIELD_COUNTRY = "country";
public static final String FIELD_PHOTO_ID = "photo_id";
public static final String FIELD_PHOTO_ID_NUMBER = "photo_id_number";
public static final String FIELD_PHOTO_ID_PIC_PATH = "photo_id_pic_path";

public Context context;

public GinisTable(Context context) {
super(context, Config.DB_NAME, null, 1);
this.context = context;
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS "
+GinisTable.TABLE_GINI+"("
+GinisTable.FIELD_GINI_ID+" INTEGER PRIMARY KEY, "
+GinisTable.FIELD_NAME+" VARCHAR(128), "
+GinisTable.FIELD_PROFESSION +" VARCHAR(128), "
+GinisTable.FIELD_GINI_PIC_PATH+" VARCHAR(128), "
+GinisTable.FIELD_EXPERIENCE+" INT, "
+GinisTable.FIELD_PRIMARY_MOBILE+" VARCHAR(15), "
+GinisTable.FIELD_ISD_CODE+" VARCHAR(15),"
+GinisTable.FIELD_GENDER+" TINYINT,"
+GinisTable.FIELD_DOB+" VARCHAR(15),"
+GinisTable.FIELD_LATITUDE+" DOUBLE,"
+GinisTable.FIELD_LONGITUDE+" DOUBLE,"
+GinisTable.FIELD_ADDRESS_LINE1+" VARCHAR(64),"
+GinisTable.FIELD_ADDRESS_LINE2+" VARCHAR(128),"
+GinisTable.FIELD_CITY+" VARCHAR(128),"
+GinisTable.FIELD_STATE+" VARCHAR(64),"
+GinisTable.FIELD_COUNTRY+" VARCHAR(64),"
+GinisTable.FIELD_PHOTO_ID+" VARCHAR(64), "
+GinisTable.FIELD_PHOTO_ID_NUMBER+" VARCHAR(128), "
+GinisTable.FIELD_PHOTO_ID_PIC_PATH+" VARCHAR(128));");
}

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

}

public int insertGini(GiniInfo giniInfo) {

SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();

contentValues.put(GinisTable.FIELD_NAME, giniInfo.getName());
contentValues.put(GinisTable.FIELD_PROFESSION, giniInfo.getProfession());
contentValues.put(GinisTable.FIELD_GINI_PIC_PATH, giniInfo.getPicPath());
contentValues.put(GinisTable.FIELD_EXPERIENCE, giniInfo.getExperience());
contentValues.put(GinisTable.FIELD_PRIMARY_MOBILE, giniInfo.getPrimaryMobile());
contentValues.put(GinisTable.FIELD_ISD_CODE, giniInfo.getIsdCode());
contentValues.put(GinisTable.FIELD_GENDER, giniInfo.getGender());
contentValues.put(GinisTable.FIELD_DOB, giniInfo.getDob());
contentValues.put(GinisTable.FIELD_LATITUDE, giniInfo.getLatitude());
contentValues.put(GinisTable.FIELD_LONGITUDE, giniInfo.getLongitude());
contentValues.put(GinisTable.FIELD_ADDRESS_LINE1, giniInfo.getAddressLine1());
contentValues.put(GinisTable.FIELD_ADDRESS_LINE2, giniInfo.getAddressLine2());
contentValues.put(GinisTable.FIELD_CITY, giniInfo.getCity());
contentValues.put(GinisTable.FIELD_STATE, giniInfo.getState());
contentValues.put(GinisTable.FIELD_COUNTRY, giniInfo.getCountry());
contentValues.put(GinisTable.FIELD_PHOTO_ID, giniInfo.getPhotoID());
contentValues.put(GinisTable.FIELD_PHOTO_ID_NUMBER, giniInfo.getPhotoIDNum());
contentValues.put(GinisTable.FIELD_PHOTO_ID_PIC_PATH, giniInfo.getPhotoIDPicPath());

int giniID = (int) db.insert(GinisTable.TABLE_GINI, null, contentValues);
return giniID;
}

public List<GiniInfo> getGiniInfoList() {

List<GiniInfo> giniInfoList = new ArrayList<GiniInfo>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from "+GinisTable.TABLE_GINI, null);
/*if(cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();

while (cursor.isAfterLast() == false) {

GiniInfo giniInfo = new GiniInfo();
giniInfo.setGiniID(Integer.parseInt(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_GINI_ID))));
giniInfo.setName(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_NAME)));
giniInfo.setProfession(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_PROFESSION)));
giniInfo.setPicPath(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_GINI_PIC_PATH)));
giniInfo.setExperience(Integer.parseInt(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_EXPERIENCE))));
giniInfo.setPrimaryMobile(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_PRIMARY_MOBILE)));
giniInfo.setIsdCode(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_ISD_CODE)));
giniInfo.setGender(Integer.parseInt(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_GENDER))));
giniInfo.setDob(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_DOB)));
giniInfo.setLatitude(Double.parseDouble(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_LATITUDE))));
giniInfo.setLongitude(Double.parseDouble(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_LONGITUDE))));
giniInfo.setAddressLine1(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_ADDRESS_LINE1)));
giniInfo.setAddressLine2(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_ADDRESS_LINE2)));
giniInfo.setCity(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_CITY)));
giniInfo.setState(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_STATE)));
giniInfo.setCountry(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_COUNTRY)));
giniInfo.setPhotoID(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_PHOTO_ID)));
giniInfo.setPhotoIDNum(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_PHOTO_ID_NUMBER)));
giniInfo.setPhotoIDPicPath(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_PHOTO_ID_PIC_PATH)));

giniInfoList.add(giniInfo);
}
}*/
return giniInfoList;
}
}


My stack trace is as:

android.database.sqlite.SQLiteException: no such table: ginis (code 1): , while compiling: select * from ginis
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1339)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1278)
at com.mastishka.pocketgini.database.tables.GinisTable.getGiniInfoList(GinisTable.java:122)
at com.mastishka.pocketgini.services.UploadGiniData.onHandleIntent(UploadGiniData.java:99)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.os.HandlerThread.run(HandlerThread.java:61)


The above is the exception stack trace.

Answer

Put all the tables within the same database file into the same sqlite helper. The helper is for versioning database files, not tables

Comments