在以下几行中,我创建了一个图像路径并将该 pth 存储在 sqlite DB 中:
private void commitToDB() {
imgTitle = "img";
GenerateRandom gr = new GenerateRandom();
imgTitle +=gr.getRand()+".jpeg";
db = mpoh.getWritableDatabase();
cv = new ContentValues();
File dir = Environment.getExternalStorageDirectory();
File file = new File(dir,"/DCIM/"+imgTitle);
cv.put(imgName, imgTitle);
cv.put(path,file.getAbsolutePath());
Toast.makeText(getApplicationContext(),imgTitle, Toast.LENGTH_LONG).show();
long newID = db.insert("MPData", null, cv);
if (newID == -1) {
Toast.makeText(getBaseContext(), "Error Commiting Record(s)",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getBaseContext(), "Data Commited Successfully",
Toast.LENGTH_SHORT).show();
}
}
我的 SQLite DB 看起来像:
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(" CREATE TABLE " + MP_TABLE_NAME + " ( " +
BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
" name TEXT, " +
" lat REAL, " +
" lng REAL, " +
" date TEXT, " +
" time TEXT, " +
" path TEXT, " +
" imgName TEXT " +
");" );
}
我的问题是,由于 newID= -1,我收到“错误提交记录”
long newID = db.insert("MPData", null, cv);
if (newID == -1)
还要注意的是,数据库的一个实例是在 onCreate 内部实例化的。
任何解决此问题的建议将不胜感激。