データベースにデータを挿入できないようで、どこに問題があるのかわかりません。データベースコードは次のとおりです。
public class Database {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "Name";
public static final String KEY_PERIOD = "Period";
public static final String KEY_FREQUENCY = "Frequency";
public static final String KEY_RECIEVINGTIME = "Recieving_time";
public static final String KEY_STATS = "Stats";
private static final String DATABASE_NAME = "Reports";
private static final String DATABASE_TABLE = "Table1";
private static final int DATABASE_VERSION = 1;
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 " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_PERIOD + " TEXT NOT NULL, " +
KEY_FREQUENCY + " TEXT NOT NULL, " +
KEY_RECIEVINGTIME + " TEXT NOT NULL, " +
KEY_STATS + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public Database(Context c){
myContext = c;
}
public Database open(){
myHelper = new DbHelper(myContext);
myDatabase = myHelper.getWritableDatabase();
return this;
}
public void close(){
myDatabase.close();
}
public void writeEntry(String name, long period, long frequency, long recievingTime, String stats){
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_PERIOD, period);
cv.put(KEY_FREQUENCY, frequency);
cv.put(KEY_RECIEVINGTIME, recievingTime);
cv.put(KEY_STATS, stats);
myDatabase.insert(DATABASE_TABLE, null, cv);
}
public Cursor query(String[] columns, String selection, String[] selectionArgs, String groupBy) {
// TODO Auto-generated method stub
Cursor c = myDatabase.query(DATABASE_TABLE, columns, selection, selectionArgs, groupBy, null, null);
return c;
}
}
私はこの方法でデータを挿入しようとします:
public void save(){
sharedPrefs = context.getSharedPreferences(SPNAME, 0);
Log.v(" info", sharedPrefs.getAll().toString());
Database database = new Database(context);
database.open();
database.writeEntry(
sharedPrefs.getString(NAME, "No data was found"),
sharedPrefs.getLong(PERIOD, 0),
sharedPrefs.getLong(FREQUENCY, 0),
sharedPrefs.getLong(RECIEVINGTIME, 0),
sharedPrefs.getString(STATS, "No data was found")
);
database.close();
editor.remove(NAME);
editor.remove(FREQUENCY);
editor.remove(RECIEVINGTIME);
editor.remove(PERIOD);
editor.remove(FREQUENCY);
editor.remove(STATS);
editor.commit();
}
そして、私はこの方法でデータを取得します:
public ReportItem getReportInfo(String rowId){
ReportItem item = new ReportItem();
database = new Database(context);
database.open();
cursor = database.query(new String[]{
Database.KEY_NAME,
Database.KEY_PERIOD,
Database.KEY_FREQUENCY,
Database.KEY_RECIEVINGTIME,
Database.KEY_STATS}, Database.KEY_ROWID +" = "+rowId, null, null);
if(cursor.moveToFirst()){
item.setName(cursor.getString(cursor.getColumnIndex(Database.KEY_NAME)));
item.setPeriod(cursor.getLong(cursor.getColumnIndex(Database.KEY_PERIOD)));
item.setFrequency(cursor.getLong(cursor.getColumnIndex(Database.KEY_FREQUENCY)));
item.setReceivingTime(cursor.getLong(cursor.getColumnIndex(Database.KEY_RECIEVINGTIME)));
item.setStats(cursor.getString(cursor.getColumnIndex(Database.KEY_STATS)));
}
cursor.close();
database.close();
Log.v("Data from database", item.toString());
return item;
}
データをdbログに挿入する前のsave()メソッドでは、次のように表示されます。
09-27 17:19:54.973: V/info(31638): <!>com.dailyreports.ainius.Report 75<!> {Period=86400000, Name=report1, Stats=23 24 26 , Frequency=86400000, RecievingTime=62340000}
しかし、dbログから同じ情報を取得した後のgetReportInfo(String)メソッドでは、次のように表示されます。
09-27 17:20:06.263: V/Data from database(31638): <!>com.dailyreports.ainius.Report 117<!> 0 0 0
助言がありますか?前もって感謝します。