0

I've created a table in android. when i try to add or take any data it crashes .. what could be the problem ??

code on which crashes:

return db.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_STRING,KEY_rewID }, 
                null, null, null, null, null);

i add here an row in table :

database.insert("1", encodedImageString, "8");
                System.out.println(sqlite_obj.getAllData());

log cat :

04-24 01:23:58.110: E/AndroidRuntime(9766): android.database.sqlite.SQLiteException: no such table: photos (code 1): , while compiling: SELECT id, photostring, reviewID FROM photos
04-24 01:23:58.110: E/AndroidRuntime(9766):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-24 01:23:58.110: E/AndroidRuntime(9766):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
04-24 01:23:58.110: E/AndroidRuntime(9766):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
04-24 01:23:58.110: E/AndroidRuntime(9766):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
04-24 01:23:58.110: E/AndroidRuntime(9766):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
04-24 01:23:58.110: E/AndroidRuntime(9766):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
04-24 01:23:58.110: E/AndroidRuntime(9766):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
04-24 01:23:58.110: E/AndroidRuntime(9766):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
04-24 01:23:58.110: E/AndroidRuntime(9766):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
04-24 01:23:58.110: E/AndroidRuntime(9766):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
04-24 01:23:58.110: E/AndroidRuntime(9766):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
04-24 01:23:58.110: E/AndroidRuntime(9766):     at com.project.sqlite.PhoDB.getAllData(PhoDB.java:95)
04-24 01:23:58.110: E/AndroidRuntime(9766):     at com.project.simplify.Photo_upload$3.onClick(Photo_upload.java:133)
04-24 01:23:58.110: E/AndroidRuntime(9766):     at android.view.View.performClick(View.java:4438)
04-24 01:23:58.110: E/AndroidRuntime(9766):     at android.view.View$PerformClick.run(View.java:18422)
04-24 01:23:58.110: E/AndroidRuntime(9766):     at android.os.Handler.handleCallback(Handler.java:733)
04-24 01:23:58.110: E/AndroidRuntime(9766):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-24 01:23:58.110: E/AndroidRuntime(9766):     at android.os.Looper.loop(Looper.java:136)
04-24 01:23:58.110: E/AndroidRuntime(9766):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-24 01:23:58.110: E/AndroidRuntime(9766):     at java.lang.reflect.Method.invokeNative(Native Method)
04-24 01:23:58.110: E/AndroidRuntime(9766):     at java.lang.reflect.Method.invoke(Method.java:515)
04-24 01:23:58.110: E/AndroidRuntime(9766):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-24 01:23:58.110: E/AndroidRuntime(9766):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-24 01:23:58.110: E/AndroidRuntime(9766):     at dalvik.system.NativeStart.main(Native Method)









 public class PhoDB {

    public static final String KEY_ID = "id";
    public static final String KEY_STRING = "photostring";
    public static final String KEY_rewID = "reviewID";

    private static final String TAG = "DBAdapter";
    private static final String DATABASE_NAME = "PhotoDB";

    private static final String DATABASE_TABLE = "photos";
    private static final int DATABASE_VERSION = 99;

    private static final String DATABASE_CREATE = 
            "create table sample (id text primary key,reviewID text not null  ,photostring text not null);";

    private final Context context;
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public PhoDB(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) {

            try {
                db.execSQL(DATABASE_CREATE);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        @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 sample");
            onCreate(db);
        }
    }

    //---open SQLite DB---
    public PhoDB open() throws SQLException {

        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---close SQLite DB---
    public void close() {

        DBHelper.close();
    }

    //---insert data into SQLite DB---
    public long insert(String id, String photo_string, String reviewID) {

        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_ID, id);
        initialValues.put(KEY_STRING, photo_string);
        initialValues.put(KEY_rewID, reviewID);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    //---Delete All Data from table in SQLite DB---
    public void deleteAll() {

        db.delete(DATABASE_TABLE, null, null);
    }

    //---Get All Contacts from table in SQLite DB---
    public Cursor getAllData() {

        return db.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_STRING,KEY_rewID }, 
                null, null, null, null, null);
    }


}
3
  • Your database table doesn't exist Commented Apr 23, 2014 at 22:32
  • i call these methods in onCreate : database = new PhoDB(Photo_upload.this); database.open(); Commented Apr 23, 2014 at 22:34
  • Your create statement is creating a table named sample, not photos. Commented Apr 23, 2014 at 22:44

1 Answer 1

1

You are creating table with different name .

Change :

private static final String DATABASE_CREATE = 
            "create table sample (id text primary key,reviewID text not null  ,photostring text not null);";

with :

private static final String DATABASE_CREATE = 
            "create table " + DATABASE_TABLE +" (id text primary key,reviewID text not null  ,photostring text not null);";
Sign up to request clarification or add additional context in comments.

2 Comments

You might want to point out that the onUpgrade() method references the sample table, too.
@MikeM. yes good point . user3416113 you should basicly use one approach , use your static DATABASE_TABLE variable instead of typing your datebase table everytime

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.