我对 sqlite 数据库有问题。我想用现有记录在 android 中创建数据库。我创建数据库:
public class PrzyslowiaArabskie {
private static final String TABLE_NAME = "tabela_przyslowia";
private static final String DATABASE_NAME = "przyslowiadb";
private static final int DATABASE_VERSION = 1;
public static final String KEY_ID = "id";
public static final String KEY_PRZYSLOWIE = "przyslowie";
private DbHelper myHelper;
private final Context myContext;
private SQLiteDatabase myDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_PRZYSLOWIE + " TEXT NOT NULL);" );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
public PrzyslowiaArabskie(Context c) {
myContext = c;
}
public PrzyslowiaArabskie open() {
myHelper = new DbHelper(myContext);
myDatabase = myHelper.getWritableDatabase();
return this;
}
public void close() {
myHelper.close();
}
public long createEntry(String przyslowie) {
ContentValues cv = new ContentValues();
cv.put(KEY_PRZYSLOWIE, przyslowie);
return myDatabase.insert(TABLE_NAME, null, cv);
}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ID, KEY_PRZYSLOWIE };
Cursor c = myDatabase.query(TABLE_NAME, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndexOrThrow(KEY_ID);
int iPrzyslowie = c.getColumnIndexOrThrow(KEY_PRZYSLOWIE);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result = result + c.getString(iRow) + " " + c.getString(iPrzyslowie) + "\n";
}
return result;
}
}
如果我想用现有记录创建数据库,我该怎么办。如何将记录与创建数据库一起添加?