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
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 .
- open ddms perspective
- Find data/data/PACKAGENAME/database/YOURFILE
- Click the pull from db button on top right shown in picture.
Open 
2 Comments
Paul
Thanks for the reply, I would like to try and see using the emulator. How can this be done?
Gaurav Ojha
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.
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.
2 Comments
Divisible by Zero
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)
artem_p
I believe that sqlite3 is unavailable on device, it is only on an emulator.
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;
}