2013-01-17 21 views
9

Myślę, że stworzyłem bazę danych sqllite z Androidem, ale za każdym razem, gdy robię wstawkę, twierdzi ona, że ​​kolumna nie istnieje. Jak mogę wyświetlić schemat i czy metoda onCreate jest wywoływana przy tworzeniu obiektu?Schemat bazy danych Android Sqlite

Odpowiedz

9

Możesz to zrobić za pomocą kodu. Sqlite ma tabelę o nazwie "sqlite_master", która przechowuje informacje o schemacie.

/** 
    * 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; 
    } 

Najprostszym sposobem jest uruchomienie go na emulatorze.

  1. otwarte DDMS perspektywiczne
  2. Znajdź data/data/packageName/database/twój_plik
  3. Kliknij ściąganie z przycisku db w prawym górnym rogu pokazany na rysunku.

Otwarte enter image description here

+0

Dzięki za odpowiedź, chciałbym spróbować i zobaczyć za pomocą emulatora. Jak to zrobić? – Paul

+0

Dzięki za pomoc, która wygląda świetnie. – Paul

+0

@Paul zaktualizował odpowiedź ze zdjęciem. – wtsang02

1

Innym sposobem na zbadanie schemat bazy danych jest adb shell do urządzenia (lub emulator). Uruchom wiersz poleceń sqlite za pomocą: sqlite3 <path to your database> i wpisz .schema w wierszu polecenia.

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

+1

Z jakiegoś powodu z uruchomionym "sqlite3" dostałem komunikat "/ system/bin/sh: sqlite3: not found", ale udało mi się znaleźć program w folderze przeglądarki root (który kupiłem i zainstalowałem) wykonując polecenie "find | grep sqlite3 "(po uruchomieniu" su "- co mogę zrobić, ponieważ mój telefon jest zrootowany) –

+0

Wierzę, że sqlite3 jest niedostępny na urządzeniu, to tylko na emulatorze. –

0

można uzyskać nazwy tabeli w następujący sposób:

/** 
    * 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; 
    } 
Powiązane problemy