我是android数据库开发的新手。下面的代码是我的第一个代码。执行此代码后,我看到在我的数据库表中没有插入任何内容。我的代码有什么问题?
这是我的 dbHelper 类:
public class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, "shit.db", null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table users (userID integer primary key autoincrement,name text,family text,age integer);";
Log.d("Ehsan", sql);
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
这是我的主要活动的 onCreate 方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("Ehsan", "Started");
DbHelper db = new DbHelper(this);
Log.d("Ehsan", "DB Created");
SQLiteDatabase mydb =db.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("name", "Ehsan");
cv.put("family", "Shirzadi");
cv.put("age", 29);
Log.d("Ehsan", "Record inserting");
long id = mydb.insert("users",null, cv);
Log.d("Ehsan", "Record inserted");
}