问题
Everything goes successful in my app in higher versions, but in the lower versions like 2.2 the app crashes while fetching data from database with this error
07-17 21:46:36.361: I/Database(520): sqlite returned: error code = 1, msg = no such table: Schedules
07-17 21:46:36.371: W/System.err(520): android.database.sqlite.SQLiteException: no such table: Schedules: , while compiling: SELECT DISTINCT * FROM Schedules
07-17 21:46:36.391: W/System.err(520): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
07-17 21:46:36.391: W/System.err(520): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
07-17 21:46:36.401: W/System.err(520): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
07-17 21:46:36.462: W/System.err(520): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
07-17 21:46:36.462: W/System.err(520): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
07-17 21:46:36.485: W/System.err(520): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
07-17 21:46:36.492: W/System.err(520): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
07-17 21:46:36.492: W/System.err(520): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1315)
07-17 21:46:36.502: W/System.err(520): at com.example.indianconstitution.DBAdapter.getSchedules(DBAdapter.java:148)
07-17 21:46:36.502: W/System.err(520): at com.example.indianconstitution.SchedulesFragment.onCreate(SchedulesFragment.java:37)
07-17 21:46:36.502: W/System.err(520): at android.support.v4.app.Fragment.performCreate(Fragment.java:1477)
07-17 21:46:36.512: W/System.err(520): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
07-17 21:46:36.512: W/System.err(520): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
07-17 21:46:36.512: W/System.err(520): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-17 21:46:36.521: W/System.err(520): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
07-17 21:46:36.521: W/System.err(520): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
07-17 21:46:36.521: W/System.err(520): at android.os.Handler.handleCallback(Handler.java:587)
07-17 21:46:36.521: W/System.err(520): at android.os.Handler.dispatchMessage(Handler.java:92)
07-17 21:46:36.521: W/System.err(520): at android.os.Looper.loop(Looper.java:123)
07-17 21:46:36.521: W/System.err(520): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-17 21:46:36.521: W/System.err(520): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 21:46:36.552: W/System.err(520): at java.lang.reflect.Method.invoke(Method.java:521)
07-17 21:46:36.552: W/System.err(520): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-17 21:46:36.571: W/System.err(520): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-17 21:46:36.571: W/System.err(520): at dalvik.system.NativeStart.main(Native Method)
07-17 21:46:36.661: D/AndroidRuntime(520): Shutting down VM
07-17 21:46:36.661: W/dalvikvm(520): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-17 21:46:36.691: E/AndroidRuntime(520): FATAL EXCEPTION: main
07-17 21:46:36.691: E/AndroidRuntime(520): java.lang.NullPointerException
07-17 21:46:36.691: E/AndroidRuntime(520): at com.example.indianconstitution.SchedulesAdapter.<init>(SchedulesAdapter.java:22)
07-17 21:46:36.691: E/AndroidRuntime(520): at com.example.indianconstitution.SchedulesFragment.onCreateView(SchedulesFragment.java:58)
07-17 21:46:36.691: E/AndroidRuntime(520): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
07-17 21:46:36.691: E/AndroidRuntime(520): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
07-17 21:46:36.691: E/AndroidRuntime(520): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
07-17 21:46:36.691: E/AndroidRuntime(520): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-17 21:46:36.691: E/AndroidRuntime(520): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
07-17 21:46:36.691: E/AndroidRuntime(520): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
07-17 21:46:36.691: E/AndroidRuntime(520): at android.os.Handler.handleCallback(Handler.java:587)
07-17 21:46:36.691: E/AndroidRuntime(520): at android.os.Handler.dispatchMessage(Handler.java:92)
07-17 21:46:36.691: E/AndroidRuntime(520): at android.os.Looper.loop(Looper.java:123)
07-17 21:46:36.691: E/AndroidRuntime(520): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-17 21:46:36.691: E/AndroidRuntime(520): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 21:46:36.691: E/AndroidRuntime(520): at java.lang.reflect.Method.invoke(Method.java:521)
07-17 21:46:36.691: E/AndroidRuntime(520): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-17 21:46:36.691: E/AndroidRuntime(520): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-17 21:46:36.691: E/AndroidRuntime(520): at dalvik.system.NativeStart.main(Native Method)
07-17 21:46:36.892: D/dalvikvm(520): GC_FOR_MALLOC freed 3629 objects / 277352 bytes in 181ms
This link http://www.anddev.org/networking-database-problems-f29/missing-table-in-sqlite-with-specific-version-of-desire-hd-t50364.html suggests that there are some problems in phones like htc desire but I am facing this problem in spice mi-270(2.2) & also in emulators 2.1,2.2 !
I am using this code as my DBAdapter class
public class DBAdapter extends SQLiteOpenHelper
{
//CustomAdapter adapter;
static String name = "law6.sqlite";
static String path = "";
static ArrayList<GS> gs;
static SQLiteDatabase sdb;
@Override
public void onCreate(SQLiteDatabase db)
{
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
}
private DBAdapter(Context v)
{
super(v, name, null, 1);
path = "/data/data/" + v.getApplicationContext().getPackageName() + "/databases";
}
public boolean checkDatabase()
{
SQLiteDatabase db = null;
try
{
db = SQLiteDatabase.openDatabase(path + "/" + name, null, SQLiteDatabase.OPEN_READONLY);
} catch (Exception e)
{
e.printStackTrace();
}
if (db == null)
{
return false;
}
else
{
db.close();
return true;
}
}
public static synchronized DBAdapter getDBAdapter(Context v)
{
return (new DBAdapter(v));
}
public void createDatabase(Context v) throws IOException
{
this.getReadableDatabase();
try
{
InputStream myInput = v.getAssets().open(name);
// Path to the just created empty db
String outFileName = path +"/"+ name;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0)
{
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
} catch (IOException e)
{
System.out.println(e);
}
}
public void openDatabase()
{
try
{
sdb = SQLiteDatabase.openDatabase(path + "/" + name, null,
SQLiteDatabase.OPEN_READWRITE);
} catch (Exception e)
{
System.out.println(e);
}
}
public ArrayList<GS> getSchedules()
{
try{
Cursor c1 = sdb.query(true,"Schedules",null,null,null,null,null,null,null);//rawQuery("SELECT DISTINCT * FROM Schedules", null);
gs = new ArrayList<GS>();
while (c1.moveToNext())
{
GS q1 = new GS();
q1.setS_name(c1.getString(1));
q1.setP_name(c1.getString(2));
q1.setDesc(c1.getString(3));
gs.add(q1);
}
}
catch (Exception e) {
e.printStackTrace();
}
return gs;
}
}
I have tried using query()
instead of rawQuery()
but no help !
I think the problem could be in createDatabase()
or openDatabase()
relating getReadableDatabase()
or I dont know what?
I am fetching this data using this
DBAdapter db = DBAdapter.getDBAdapter(getActivity());
if (!db.checkDatabase())
try {
db.createDatabase(getActivity());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
db.openDatabase();
s = db.getSchedules();
Any Idea...?
Thanks in advance
UPDATE This is the logging I found in Jelly bean, It is showing exception but workking fine
07-25 10:24:35.721: E/Trace(14495): error opening trace file: No such file or directory (2)
07-25 10:24:37.351: I/Adreno200-EGL(14495): <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build: Nondeterministic AU_msm8625_REFS/TAGS/JB_REL_RB5_QRD_D20130221_CL3357771_release_AU (CL3357771)
07-25 10:24:37.351: I/Adreno200-EGL(14495): Build Date: 02/22/13 Fri
07-25 10:24:37.351: I/Adreno200-EGL(14495): Local Branch:
07-25 10:24:37.351: I/Adreno200-EGL(14495): Remote Branch: m/refs/tags/jb_rel_rb5_qrd_D20130221
07-25 10:24:37.351: I/Adreno200-EGL(14495): Local Patches: NONE
07-25 10:24:37.351: I/Adreno200-EGL(14495): Reconstruct Branch: NOTHING
07-25 10:24:38.031: I/Choreographer(14495): Skipped 50 frames! The application may be doing too much work on its main thread.
07-25 10:24:45.961: E/SQLiteLog(14495): (14) cannot open file at line 30178 of [00bb9c9ce4]
07-25 10:24:45.961: E/SQLiteLog(14495): (14) os_unix.c:30178: (2) open(/data/data/com.example.indianconstitution/databases/law6.sqlite) -
07-25 10:24:45.981: E/SQLiteDatabase(14495): Failed to open database '/data/data/com.example.indianconstitution/databases/law6.sqlite'.
07-25 10:24:45.981: E/SQLiteDatabase(14495): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
07-25 10:24:45.981: E/SQLiteDatabase(14495): at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at com.vivekwarde.indianconstitution.DBAdapter.checkDatabase(DBAdapter.java:44)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at com.vivekwarde.indianconstitution.ContentsFragment.onCreate(ContentsFragment.java:35)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at android.support.v4.app.Fragment.performCreate(Fragment.java:1477)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at android.os.Handler.handleCallback(Handler.java:615)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at android.os.Handler.dispatchMessage(Handler.java:92)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at android.os.Looper.loop(Looper.java:137)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at android.app.ActivityThread.main(ActivityThread.java:4794)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at java.lang.reflect.Method.invoke(Method.java:511)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-25 10:24:45.981: E/SQLiteDatabase(14495): at dalvik.system.NativeStart.main(Native Method)
07-25 10:24:45.981: W/System.err(14495): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
07-25 10:24:45.991: W/System.err(14495): at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
07-25 10:24:45.991: W/System.err(14495): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
07-25 10:24:45.991: W/System.err(14495): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
07-25 10:24:45.991: W/System.err(14495): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
07-25 10:24:45.991: W/System.err(14495): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
07-25 10:24:45.991: W/System.err(14495): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
07-25 10:24:45.991: W/System.err(14495): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
07-25 10:24:45.991: W/System.err(14495): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
07-25 10:24:45.991: W/System.err(14495): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
07-25 10:24:45.991: W/System.err(14495): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
07-25 10:24:45.991: W/System.err(14495): at com.vivekwarde.indianconstitution.DBAdapter.checkDatabase(DBAdapter.java:44)
07-25 10:24:45.991: W/System.err(14495): at com.vivekwarde.indianconstitution.ContentsFragment.onCreate(ContentsFragment.java:35)
07-25 10:24:45.991: W/System.err(14495): at android.support.v4.app.Fragment.performCreate(Fragment.java:1477)
07-25 10:24:45.991: W/System.err(14495): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
07-25 10:24:45.991: W/System.err(14495): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
07-25 10:24:46.001: W/System.err(14495): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-25 10:24:46.001: W/System.err(14495): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
07-25 10:24:46.001: W/System.err(14495): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
07-25 10:24:46.001: W/System.err(14495): at android.os.Handler.handleCallback(Handler.java:615)
07-25 10:24:46.001: W/System.err(14495): at android.os.Handler.dispatchMessage(Handler.java:92)
07-25 10:24:46.001: W/System.err(14495): at android.os.Looper.loop(Looper.java:137)
07-25 10:24:46.001: W/System.err(14495): at android.app.ActivityThread.main(ActivityThread.java:4794)
07-25 10:24:46.001: W/System.err(14495): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 10:24:46.001: W/System.err(14495): at java.lang.reflect.Method.invoke(Method.java:511)
07-25 10:24:46.001: W/System.err(14495): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-25 10:24:46.001: W/System.err(14495): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-25 10:24:46.001: W/System.err(14495): at dalvik.system.NativeStart.main(Native Method)
07-25 10:24:49.081: I/Choreographer(14495): Skipped 174 frames! The application may be doing too much work on its main thread.
回答1:
I had a similar issue, however the issue turned out not be related to the database but rather memory usage in the app. Older 2.3 devices understandably have less memory on them as well and depending on the environment users are running their devices on, your app may come up against memory constraints.
If I read the comments properly, you're coping a bundled sqlite db from your assets folder, if you can't reproduce the issue, it may be because your users don't have enough RAM available to your app or even have enough disk space to copy your DB.
largeHeap
in your manifest only works on Android 3.0+ I believe so even if you have that set it won't solve the problem for older phones. I know the conventional wisdom is that Android app have 40mb of heap space initially, but my research suggests that this is a recommended size and not guaranteed, I've come across pre-honeycomb devices (sony in particular) that have as little as 20MB available to them.
In short, I suspect this is a memory issue and not one with the database. Try to come up with an alternate lighter weight strategy for 2.3 and below devices and see if the problem continues.
回答2:
You can try to change extension of you database to mp3 to avoid compression problems. Read this for further info. This behavior changes since 2.3 so it looks like it could help
来源:https://stackoverflow.com/questions/24406326/no-such-table-in-api-2-2