10

I think I have created an sqllite database with android but every time I go to do an insert it claims a column does not exist. How can I view the schema and is the onCreate method called on object creation?

3 Answers 3

14

You can do it with code. Sqlite has a table called "sqlite_master " which holds the schema information.

    /**
     * Get all table Details from the sqlite_master table in Db.
     * 
     * @return An ArrayList of table details.
     */
    public ArrayList<String[]> getDbTableDetails() {
        Cursor c = db.rawQuery(
                "SELECT name FROM sqlite_master WHERE type='table'", null);
        ArrayList<String[]> result = new ArrayList<String[]>();
        int i = 0;
        result.add(c.getColumnNames());
        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
            String[] temp = new String[c.getColumnCount()];
            for (i = 0; i < temp.length; i++) {
                temp[i] = c.getString(i);
            }
            result.add(temp);
        }

        return result;
    }

A more simple way is to run it on the emulator .

  1. open ddms perspective
  2. Find data/data/PACKAGENAME/database/YOURFILE
  3. Click the pull from db button on top right shown in picture.

Open enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply, I would like to try and see using the emulator. How can this be done?
In the second part, how do I read the file I get? When I open it in a text editor it looks like its a binary file.
1

Another way to examine the schema of your database is to adb shell into the device (or emulator). Launch sqlite's command line with: sqlite3 <path to your database> and type .schema at the command prompt.

See: http://developer.android.com/tools/help/sqlite3.html

2 Comments

For some reason running "sqlite3" got me the message "/system/bin/sh: sqlite3: not found", but I managed to find the program in a folder of root browser (which I have bought&installed) by executing "find | grep sqlite3" (after running "su" - which I can do because my phone is rooted)
I believe that sqlite3 is unavailable on device, it is only on an emulator.
0

You can get table names as follows:

    /**
     * Get all table Details from the sqlite_master table in Db.
     * 
     * SQlite has a table called "sqlite_master " which holds schema information.
     * 
     * @return An ArrayList of table names.
     */
    public ArrayList<String> getDbTableNames(SQLiteDatabase db) {
        ArrayList<String> result = new ArrayList<String>();
        Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);

            if (c.moveToFirst()) {
                while ( !c.isAfterLast() ) {
                    result.add( c.getString(c.getColumnIndex("name")) );
                    c.moveToNext();
                }
            }

        return result;
    }

Comments

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.