看来我无法将数据插入我的数据库,我不知道问题出在哪里。这是数据库代码:
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;
}
在将数据插入数据库日志之前的 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}
但是在从数据库日志中检索相同信息后的 getReportInfo(String) 方法中显示:
09-27 17:20:06.263: V/Data from database(31638): <!>com.dailyreports.ainius.Report 117<!> 0 0 0
有什么建议么?提前致谢。