2016-09-22 2 views
0

Community Ich bin neu in Android, um nur das Konzept von SQLITY aus Udacity als geführt zu implementieren, baute ich eine kleine App ... wie unten Code mit Kategorie Aktivität (Haupt) zeigen Auch ich rief getReadeableDatabase() auf offene Helferklasse in onCreate der Hauptaktivität (Katalog), löschen Sie Daten, deinstallierte App, keine Lösung gefunden?Desperate Antwort benötigt: Android Sqlite Tabelle erstellt nicht

public class CatalogActivity extends AppCompatActivity { 
sqlitedbhelper sqdb; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_catalog); 

    // Setup FAB to open EditorActivity 
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(CatalogActivity.this, EditorActivity.class); 
      startActivity(intent); 
     } 
    }); 

    sqdb = new sqlitedbhelper(this); 
    Log.v("my tag", "0"); 

    displaydata(); 

} 

public void displaydata() { 
    SQLiteDatabase mdb = sqdb.getReadableDatabase(); 
    String[] projection = { 
      PetContract.petdata.col_id, 
      PetContract.petdata.col_name, 
      PetContract.petdata.col_breed, 
      PetContract.petdata.col_sex, 
      PetContract.petdata.col_weight}; 


    // Perform a query on the pets table 
    Cursor cursor = mdb.query(
      PetContract.petdata.pet_table, // The table to query 
      projection,   // The columns to return 
      null,     // The columns for the WHERE clause 
      null,     // The values for the WHERE clause 
      null,     // Don't group the rows 
      null,     // Don't filter by row groups 
      null);     // The sort order 


    TextView txt = (TextView) findViewById(R.id.text_view_pet); 
    try { 

     txt.setText("no of columns:"); 
     txt.append(String.valueOf(cursor.getCount())); 
    } finally { 
     cursor.close(); 
    } 


} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu options from the res/menu/menu_catalog.xml file. 
    // This adds menu items to the app bar. 
    getMenuInflater().inflate(R.menu.menu_catalog, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // User clicked on a menu option in the app bar overflow menu 
    switch (item.getItemId()) { 
     // Respond to a click on the "Insert dummy data" menu option 
     case R.id.action_insert_dummy_data: 

      // Do nothing for now 
      return true; 
     // Respond to a click on the "Delete all entries" menu option 
     case R.id.action_delete_all_entries: 
      // Do nothing for now 
      return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
} 

Unten ist der Vertrag class

public final class PetContract { 
private PetContract() {} 


public static final class petdata implements BaseColumns{ 

    public static final String pet_table="pet"; 
    public static final String col_name="name"; 
    public static final String col_weight="weight"; 
    public static final String col_breed="breed"; 
    public static final String col_id=BaseColumns._ID; 
    public static final String col_sex="sex"; 

    //constants starts here 
    public static final int sex_male=1; 
    public static final int sex_female=2; 
    public static final int sex_unknown=0; 
} 

}

und schließlich die sqlitedbhelper class

public class sqlitedbhelper extends SQLiteOpenHelper { 
public static final int database_ver=1; 

public static final String database_nAME="shelter"; 



public sqlitedbhelper(Context context) { 
    super(context, database_nAME, null, database_ver); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    Log.v("my tag","in on create db helper"); 
    String SQL_CREATE_PETS_TABLE = "CREATE TABLE " + petdata.pet_table + " (" 
      + petdata.col_id + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
      + petdata.col_name + " TEXT NOT NULL, " 
      + petdata.col_breed + " TEXT, " 
      + petdata.col_sex + " INTEGER NOT NULL, " 
      + petdata.col_weight + " INTEGER NOT NULL DEFAULT 0);"; 
    Log.v("my tag"," b4 passed sql create db helper"); 
    db.execSQL(SQL_CREATE_PETS_TABLE); 
    Log.v("my tag","passed sql create db helper"); 

} 

@Override 
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { 


} 

}

Nun ist das Problem, wenn ich es Datenbank ausgeführt benannte Schutz wird erstellt, aber keine Tabelle erstellt.

+0

Mögliches Duplikat von [Wann wird SQLiteOpenHelper onCreate()/onUpgrade() ausgeführt?] (http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade -Lauf) –

Antwort

0

Sie haben diese in onCreat Methode bei Ihrer CatalogActivity

sqdb = new sqlitedbhelper (this); 

erwähnen Dies wird Ihnen helfen ..

0

gut, wurde Thax prob gelöst actlly alles f9..mine ph nicht so wurzelte konnte nicht korrekt auf die Datenbank zugreifen..thax

Verwandte Themen