Search code examples
javaandroid-sqlitesqliteopenhelper

Desperate Answer Needed:Android Sqlite Table is not creating


Community I m new in android so to just implement the concept of sqlity from udacity as guided,I built a small app...as show below code with category activity(main) EVEN I CALLED getReadeableDatabase() on open helper class in onCreate of main activity(catalog),clear data,uninstalled app,no solution found?

 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);
}
}

Below is the contract 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;
}

}

And finally the 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) {


}

}

Now the problem is when I run it database named shelter is created but no table is created.


Solution

  • You have to mention this at your CatalogActivity inside onCreat method

      sqdb = new sqlitedbhelper (this);
    

    This will help you..