我创建了一个名为“enigmeDB”的数据库和一个表“table_enigme”:
public class EnigmeDatabase extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "enigmeBD";
private static final String TABLE_ENIGME = "table_enigme";
// fields
private static final String KEY_ID = "id";
private static final String KEY_DESCRIPTION = "description";
private static final String KEY_REPONSE="reponse";
private static final String KEY_CATEGORIE="categorie";
public EnigmeDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// creation of the table
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_ENIGME_TABLE =
"CREATE TABLE " + TABLE_ENIGME
+ "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KEY_DESCRIPTION + " TEXT,"
+ KEY_REPONSE + " TEXT,"
+ KEY_CATEGORIE + "TEXT"
+ ")";
db.execSQL(CREATE_ENIGME_TABLE);
}
// Update
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ENIGME);
onCreate(db);
}....
当我打开我的 DDMS 透视图并打开文件资源管理器时:/data/data/com.android.enigme(我的包)我找到“enigmeDB”但我没有找到“table_enigme”。
虽然在 Sqlite 数据库浏览器中我的表看起来是空的:
我从 Activity 插入数据,这是我的课程:
公共类 EnigmeActivity 扩展 Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//recuperation des champs depuis main.xml
final EditText description=(EditText) this.findViewById(R.id.description);
final EditText reponse=(EditText)this.findViewById(R.id.reponse);
final EditText categorie=(EditText)this.findViewById(R.id.categorie);
Button ajouter = (Button)this.findViewById(R.id.ajouter);
final EnigmeDatabase db=new EnigmeDatabase(this);
ajouter.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("Etape : " , "Recuperation");
Log.i("description : ", description.getText().toString());
Log.i("reponse : ", reponse.getText().toString());
Log.i("categorie : ", categorie.getText().toString());
//add a new Enigme
Log.d("Etape : ","Insertion" );
db.ajouterEnigme(new EnigmeBean(description.getText().toString(),reponse.getText().toString(),categorie.getText().toString()));
Toast.makeText(EnigmeActivity.this, "Ajout avec succes!", Toast.LENGTH_SHORT).show();
}
});
}