绝望的回答 Needed:Android Sqlite Table 没有创建
Desperate Answer Needed:Android Sqlite Table is not creating
社区是我在 android 中的新成员,所以为了按照指导从 udacity 中实现 sqlity 的概念,我构建了一个小应用程序...如下所示,类别为 activity(主要)即使我在 main activity(目录)的 onCreate 中的打开助手 class 上调用了 getReadeableDatabase(),清除数据,卸载应用程序,找不到解决方案?
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 的数据库,但没有创建 table。
您必须在 onCreate 方法中的目录活动中提及这一点
sqdb = new sqlitedbhelper (this);
这对你有帮助..
社区是我在 android 中的新成员,所以为了按照指导从 udacity 中实现 sqlity 的概念,我构建了一个小应用程序...如下所示,类别为 activity(主要)即使我在 main activity(目录)的 onCreate 中的打开助手 class 上调用了 getReadeableDatabase(),清除数据,卸载应用程序,找不到解决方案?
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 的数据库,但没有创建 table。
您必须在 onCreate 方法中的目录活动中提及这一点
sqdb = new sqlitedbhelper (this);
这对你有帮助..