0

I am new to SqlLite Database. I have created Database using SqlLite Database Browser and saved it in Android App Project Folder in path src/com/example/hello/databases/

I have also created tables already. Now, When I am trying to run code, It is showing error no such table found.
My code is as below :

public class DatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 3;
    private static final String DATABASE_NAME = "db_admin";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

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

    }

    // Adding User Response When He/She attempts Questions...
    void addUserResponse(UserResponse response) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put("QId", response.getQId());
        values.put("OptionId", response.getOptionId());
        values.put("Responsedate", response.getResponseDate());

        // Inserting Row
        db.insert("tblUserResponse", null, values);
        db.close();
    }
}

Inserting values as :

        DatabaseHandler db = new DatabaseHandler(this);

        db.addUserResponse(new UserResponse("1", "1", "hello"));
        db.addUserResponse(new UserResponse("2", "1", "hello"));
        db.addUserResponse(new UserResponse("3", "3", "hello"));
        db.addUserResponse(new UserResponse("4", "2", "hello"));
Jeeten Parmar
  • 5,568
  • 15
  • 62
  • 111
  • where is `Create Table tblUserResponse` ? – user3301551 Feb 14 '14 at 12:57
  • @user3301551 I have already created table in Database using SqlLite Database Browser, wont it work ??? – Jeeten Parmar Feb 14 '14 at 12:58
  • Yes, it will. He didn't read carefully – Phantômaxx Feb 14 '14 at 12:59
  • I know. I read your question, I was answering the other user (who made my same assumption, before rereading the question) – Phantômaxx Feb 14 '14 at 13:00
  • If you want to do it this way, put the database into the assets folder of your project they create a copyDatabase() method which will copy it from the assets table and put it into the correct path that you mention. There are plenty of tutorials that show this way. Remember: If you are creating it in the SQLite browser, you need to add the locale table as well. Any tutorial should cover this. – RED_ Feb 14 '14 at 13:00
  • @ArtooDetoo, thank you. what is the problem with my code ? – Jeeten Parmar Feb 14 '14 at 13:01
  • @RED_ It's enough if you put it in **/data/data/your.app.name/databases** – Phantômaxx Feb 14 '14 at 13:02
  • the problem seems 2 be that creating the DB outside app is a challenge... – cosmincalistru Feb 14 '14 at 13:02
  • @ArtooDetoo Ah ok. I've always done it the other way. My mistake. – RED_ Feb 14 '14 at 13:03
  • @RED_ No mistake, you can do that too – Phantômaxx Feb 14 '14 at 13:06
  • @Jeeten Parmar I suspect that NULL... see [here](http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#insert%28java.lang.String,%20java.lang.String,%20android.content.ContentValues%29). Do you have any NULLable columns? because the "NullHack" you're doing only works if there's at least 1 NULLable column – Phantômaxx Feb 14 '14 at 13:06
  • nope, there is no one nullable columns. – Jeeten Parmar Feb 14 '14 at 13:08
  • So, I guess you must give the column names before the column values - I don't use this way of managing dbs.. I prefer the old school "INSERT INTO [TABLE NAME ] ([COLUMN NAMES]) VALUES ([VALUES LIST])". – Phantômaxx Feb 14 '14 at 13:12
  • Refer this [copying database from assets to data-data folder android](http://stackoverflow.com/questions/18505903/copying-database-file-from-assets-to-data-data-folder-in-file-explorer-andro/18506387#18506387) – Chirag Ghori Feb 14 '14 at 13:22

1 Answers1

0

The databases are stored in /data/data/your.app.name/databases/. So probably try running your code keeping your externally created db in this location. But I think you cant access this location if you are not rooted.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Skd_2014
  • 61
  • 2