Mert
Mert

Reputation: 6572

Android Sqlite no such table error

I have application on market. some people gave me this error;

no such table: UserInfo: , while compiling: SELECT Value FROM UserInfo WHERE key = 'guid'

but I have this code even;

    if(!this.dhn.isTableExists("UserInfo"))
    {
        updateDB();
    }

Update DB;

public void updateDB()
{
    try {
        InputStream myInput;

            myInput = getAssets().open("example.db");

        // Path to the just created empty db
        String outFileName = "/data/data/ko.tb/databases/"
                + "example.db";

        // 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();
        buffer = null;
        outFileName = null;
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}

Table exist ;

public boolean isTableExists(String tableName) {

    Cursor cursor = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"+tableName+"'", null);
    if(cursor!=null) {
        if(cursor.getCount()>0) {
            return true;
        }
    }
    return false;
}

all error;

java.lang.RuntimeException: Unable to start activity ComponentInfo{ko.tb/ko.tb.KOActivity}: android.database.sqlite.SQLiteException: no such table: UserInfo: , while compiling: SELECT Value FROM UserInfo WHERE key = 'guid'
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
at android.app.ActivityThread.access$1500(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:4385)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: no such table: UserInfo: , while compiling: SELECT Value FROM UserInfo WHERE key = 'guid'
at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:53)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1442)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1410)
at ko.tb.DataHelper.Guid(DataHelper.java:126)
at ko.tb.KOActivity.onCreate(KOActivity.java:202)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
... 11 more

Upvotes: 1

Views: 3963

Answers (2)

Naheed Akhtar
Naheed Akhtar

Reputation: 136

I faced similar problem sometime back, As far as i can remember I changed the database version in my database helper class to resolve the issue (see example below, you can change the version from 1 to 2 etc.).

 private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE =
        "create table titles (_id integer primary key autoincrement, "
        + "isbn text not null, title text not null, " 
        + "publisher text not null);";

    private final Context context; 

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
        int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion 
                    + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS titles");
            onCreate(db);
        }
    }    

Upvotes: 1

Bostone
Bostone

Reputation: 37126

If you are using multiple providers this link should help. As stated in description:

One of the issues you may run into when having mutiple ContentProviders in your Android app is that you will notice that the SQLiteOpenHelper.onCreate(db) is not called for each one of your providers. Therefore, you will end up with a database missing several tables.

Upvotes: 1

Related Questions