2016-09-22 44 views
0

社区我在Android的新手,所以只是作为指导实施从udacity sqlity的概念,我建立了一个小应用程序...如下所示代码与类别活动(主)即使我在主活动(目录)的onCreate上打开帮助器类上的getReadeableDatabase(),清除数据,卸载应用程序,找不到解决方案?绝望的答案需要:Android的Sqlite表不创建

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

下面是合同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; 
} 

}

最后的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) { 


} 

}

现在的问题是,当我运行它数据库名为shelter创建,但没有创建表。

+0

[SQLiteOpenHelper onCreate()/ onUpgrade()运行时什么时候可能重复?](http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade -跑) –

回答

0

你必须在你CatalogActivity onCreat方法里面提到这个

sqdb = new sqlitedbhelper (this); 

这将帮助你..

0

好,thax概率是actlly解决一切都f9..mine pH值不那么根深蒂固无法正确访问数据库..尽管