社區我在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創建,但沒有創建表。
[SQLiteOpenHelper onCreate()/ onUpgrade()運行時什麼時候可能重複?](http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade -跑) –