我刚创建了一个插入数据库的方法。但它仍然显示相同的结果。我,仅限于unit1。代码如下 DbAdapter.java
private static final String DATABASE_NAME="bible1";
private static final String DATABASE_TABLE="test1";
private static final int DATABASE_VERSION=1;
public static final String KEY_ID="_id";
public static final String UNITS="units";
public static final String CHAPTERS="chapters";
private static final String CREATE_DATABASE="create table test1 (_id integer primary key autoincrement, units text not null, chapters text not null);";
private static SQLiteHelper sqLiteHelper;
private static SQLiteDatabase sqLiteDatabase;
private Context context;
//constructor
public DbAdapter(Context c){
context = c;
}
private static class SQLiteHelper extends SQLiteOpenHelper{
public SQLiteHelper(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_DATABASE);
MakeUnits();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
public DbAdapter open() throws SQLException{
try{
sqLiteHelper = new SQLiteHelper(context);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
}catch(NullPointerException ne){
Log.e("Database", "error in creating the table");
}
return this;
}
public void close(){
sqLiteHelper.close();
}
public static void MakeUnits()
{
createUnits("unit1","chapter1");
createUnits("unit2","chapter2");
}
public Cursor fetchAllNotes(){
return sqLiteDatabase.query(DATABASE_TABLE, new String[]{KEY_ID,UNITS,CHAPTERS}, null, null, null, null, null);
}
public static long createUnits(String units,String chapters){
ContentValues content= new ContentValues();
content.put(UNITS, units);
content.put(CHAPTERS, chapters);
return sqLiteDatabase.insert(DATABASE_NAME, null, content);
}
那么现在该怎么办?我认为我创建的方法是正确的。
答案 0 :(得分:0)
您需要修改插入内容,如下所示:
public static long MakeUnits(SQLiteDatabase db)
{
long total = 0;
ContentValues insert1 = new ContentValues();
insert1.put(UNITS, "Unit1");
insert1.put(CHAPTERS, "CHAPTER1");
total = db.insert(DATABASE_TABLE,null,insert1);
insert1.clear();
insert1.put(UNITS, "Unit2");
insert1.put(CHAPTERS, "CHAPTER2");
total += db.insert(DATABASE_TABLE,null,insert1);
return total;
}
最好,我建议您使用某种循环来完成多次插入。
答案 1 :(得分:0)
一个插入不能有多行。但是,您可以使用InsertHelper
http://developer.android.com/reference/android/database/DatabaseUtils.InsertHelper.html