好的,所以我按照一些教程,在完成教程后创建了一个databaseHelper类。但是,我不能完全理解或首先了解如何实际创建数据库。
在我的mainActivity中,我已将此添加到我的onCreate中,但它没有创建任何数据库,或者至少没有任何内容显示在我的Android设备监视器中。
基本上我如何实际创建数据库?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
sharedPreferences = getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
dbHelper = new databaseHelper(this);
}
爪哇
public class databaseHelper extends SQLiteOpenHelper {
static final String DATABASEHELPER = "DATABASE HELPER";
static final String dbName="chefs";
//User Table Fields
static final String userTable="user";
static final String userColID="userID";
static final String userColName="firstName";
static final String userColLast="lastName";
static final String userColDOB="DOB";
static final String userColGend="gender";
static final String userColAddr="address";
static final String userColPost="postcode";
static final String userColBio="bio";
static final String userColUser="username";
static final String userColEmail="email";
static final String userColPass="password";
static final String userColPic="picture";
static final String userColLastLog="lastlogin";
static final String userColLastLogIP="lastloginIP";
static final String userColRegisteredIP="registeredIP";
static final String userColCreatedOn="createdOn";
//Recipe Table Fields
static final String recipeTable="recipe";
static final String recipeColID="recipeID";
static final String recipeColName="name";
static final String recipeColCate="category";
static final String recipeColDesc="description";
static final String recipeColUserID="user_userID";
//Ingredient Table Fields
static final String ingredientTable="ingredient";
static final String ingredientColID="ingredientID";
static final String ingredientColRecipeID="recipe_recipeID";
static final String ingredientColName="name";
static final String ingredientColAmount="amount";
static final String ingredientColUnit="unit";
//Step Table Fields
static final String stepTable="step";
static final String stepColID="stepID";
static final String stepColRecipeID="recipe_recipeID";
static final String stepColNumber="stepNumber";
static final String stepColDesc="description";
public databaseHelper(Context context) {
super(context, dbName, null,1);
Log.d(DATABASEHELPER, "Database Created");
}
@Override
public void onCreate(SQLiteDatabase db) {
// Creating User Table
db.execSQL("CREATE TABLE "+userTable+" ("+userColID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+userColName+" TEXT, "+userColLast+" TEXT, "
+userColDOB+" NUMERIC, "+userColGend+" TEXT, "+userColAddr+" TEXT, "+userColPost+" TEXT, "+userColBio+" TEXT, "
+userColUser+" TEXT, "+userColEmail+" TEXT, "+userColPass+" TEXT, "+userColPic+" TEXT, "+userColLastLog+" NUMERIC, "
+userColLastLogIP+" TEXT, "+userColRegisteredIP+" TEXT, "+userColCreatedOn+" NUMERIC)");
// Creating Recipe Table
db.execSQL("CREATE TABLE "+recipeTable+" ("+recipeColID+" INTEGER AUTOINCREMENT, "+recipeColName+" INTEGER, "+recipeColCate+" TEXT, "
+recipeColDesc+" TEXT, "+recipeColUserID+" INTEGER");
// Creating Ingredient Table
db.execSQL("CREATE TABLE "+ingredientTable+" ("+ingredientColID+" INTEGER AUTOINCREMENT, "+ingredientColRecipeID+" TEXT ,"+ingredientColName+" TEXT, "
+ingredientColAmount+" INTEGER, "+ingredientColUnit+" TEXT");
db.execSQL("CREATE TABLE "+stepTable+" ("+stepColID+" INTEGER AUTOINCREMENT, "+stepColRecipeID+" INTEGER, "+stepColNumber+" INTEGER, "
+stepColDesc+" TEXT");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d(DATABASEHELPER, "Upgrading Database");
}
}
感谢任何帮助。
答案 0 :(得分:0)
如android文档中所述,在调用getWritableDatabase()或getReadableDatabase()之一之前,实际上并未创建或打开数据库。
链接:http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html