我现在已经建立数据库我想向数据库添加一些数据如何通过code.anyone来帮助我存储我的数据库的价值。提前致谢。 我尝试插入但未能这样做
这是我的dbhelper类。
public class FoodDbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "PKFM.DB";
private static final int DATABASE_VERSION = 1;
private static final String CREATE_QUERY =
"CREATE TABLE "+ Food.NewDishInfo.TABLE_NAME+"("
+ Food.NewDishInfo.DISH_NAME+" TEXT NOT NULL,"
+ Food.NewDishInfo.DISH_QUANTITY+" TEXT NOT NULL,"
+ Food.NewDishInfo.DISH_CALORIE+" INTEGER,"
+ Food.NewDishInfo.DISH_FAT+" TEXT NOT NULL,"
+ Food.NewDishInfo.DISH_PROTEIN+" TEXT NOT NULL,"
+ Food.NewDishInfo.DISH_SUGAR+" TEXT NOT NULL,"
+ Food.NewDishInfo.DISH_CARBOHYDRATES+" TEXT NOT NULL);";
public FoodDbHelper(Context context)
{
super(context,DATABASE_NAME,null,DATABASE_VERSION);
Log.e("DATABASE OPERATION","Database created / opened...");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_QUERY);
Log.e("DATABASE OPERATION","Table created...");
}
public void addInformations(String name ,String quantity, Integer calorie, String fat ,
String protein,String sugar,String carbohydrates, SQLiteDatabase db){
ContentValues contentValues = new ContentValues();
contentValues.put(Food.NewDishInfo.DISH_NAME,name);
contentValues.put(Food.NewDishInfo.DISH_QUANTITY,quantity);
contentValues.put(Food.NewDishInfo.DISH_CALORIE,calorie);
contentValues.put(Food.NewDishInfo.DISH_FAT,fat);
contentValues.put(Food.NewDishInfo.DISH_PROTEIN,protein);
contentValues.put(Food.NewDishInfo.DISH_SUGAR,sugar);
contentValues.put(Food.NewDishInfo.DISH_CARBOHYDRATES,carbohydrates);
db.insert(Food.NewDishInfo.TABLE_NAME, null, contentValues);
Log.e("DATABASE OPERATION","one row inserted...");
}
我做了这样的事情,但没有插入数据
foodDbHelper = new FoodDbHelper(context);
foodDbHelper.addInformations("Paratha", "1 piece", 260,"8.99 g","5.16 g","2.18 g","38.94 g",sqLiteDatabase);
答案 0 :(得分:0)
使用this.getWritableDatabase();喜欢这个功能希望它能帮到你..
//Declare variable like this
private static final String TABLE_FRIEND_MASTER = "friend_master";
private static final String CREATE_TABLE_FRIEND_MASTER = "CREATE TABLE IF NOT EXISTS " + TABLE_FRIEND_MASTER + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_FRIEND_ID + " TEXT," + KEY_FRIEND_FNAME + " TEXT," + KEY_FRIEND_LNAME + " TEXT," + KEY_FRIEND_EMAIL + " TEXT," + KEY_FRIEND_MOBILE + " TEXT," + KEY_FRIEND_USERNAME + " TEXT,"
+ KEY_FRIEND_COUNTRY_CODE + " TEXT);";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_FRIEND_REQUEST_MASTER);
}
public int addFriend(String friendId, String friendFname, String friendLname, String friendEmail, String friendMobile, String friendUsername, String friendCountryCode) {
synchronized (Lock) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_FRIEND_ID, "" + friendId);
values.put(KEY_FRIEND_FNAME, "" + friendFname);
values.put(KEY_FRIEND_LNAME, "" + friendLname);
values.put(KEY_FRIEND_EMAIL, "" + friendEmail);
values.put(KEY_FRIEND_MOBILE, "" + friendMobile);
values.put(KEY_FRIEND_USERNAME, "" + friendUsername);
values.put(KEY_FRIEND_COUNTRY_CODE, "" + friendCountryCode);
// insert row
int comment_row_id = (int) db.insert(TABLE_FRIEND_MASTER, null, values);
return comment_row_id;
}
}