我在资源文件夹中有SQLite数据库。我想使用此数据库来读取(选择)和编写(更新)目的。我能够读取记录但无法更新记录。
我使用以下代码访问数据库。
DBHelper类代码:
public class DBHelper extends SQLiteOpenHelper {
private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window
//destination path (location) of our database on device
private static String DB_PATH = "";
private static String DB_NAME ="";// Database name
private SQLiteDatabase mDataBase;
private final Context mContext;
public DBHelper(Context context,String DB_NAME)
{
super(context, DB_NAME, null, 1);// 1? its Database Version
this.DB_NAME=DB_NAME;
// DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
this.mContext = context;
DB_PATH = mContext.getDatabasePath(DB_NAME).getPath();
}
public void createDataBase() throws IOException
{
//If database not exists copy it from the assets
boolean mDataBaseExist = checkDataBase();
if(!mDataBaseExist)
{
this.getWritableDatabase();
// this.close();
try
{
//Copy the database from assests
copyDataBase();
Log.e(TAG, "createDatabase database created");
}
catch (IOException mIOException)
{
throw new Error("ErrorCopyingDataBase");
}
}
}
//Check that the database exists here: /data/data/your package/databases/Db Name
private boolean checkDataBase()
{
File dbFile = new File(DB_PATH + DB_NAME);
//Log.v("dbFile", dbFile + " "+ dbFile.exists());
return dbFile.exists();
}
//Copy the database from assets
private void copyDataBase() throws IOException
{
InputStream mInput = mContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
//OutputStream mOutput = new FileOutputStream(outFileName);
OutputStream mOutput = new FileOutputStream(DB_PATH);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0)
{
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
public SQLiteDatabase openDataBase() throws SQLException{
//String myPath = DB_PATH + DB_NAME;
String myPath = getWritableDatabase().getPath();
mDataBase = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
return mDataBase;
}
}
2。 TestAdapter类代码
public class TestAdapter {
protected static final String TAG = "DataAdapter";
private final Context mContext;
private SQLiteDatabase mDb;
private com.gre.db.DBHelper mDbHelper;
public TestAdapter(Context context,String DB_NAME)
{
this.mContext = context;
mDbHelper = new com.gre.db.DBHelper(mContext,DB_NAME);
}
public SQLiteDatabase open() throws SQLException
{
try
{
mDb = mDbHelper.openDataBase();
// mDb = mDbHelper.getWritableDatabase();
}
catch (SQLException mSQLException)
{
Log.e(TAG, "open >>"+ mSQLException.toString());
throw mSQLException;
}
return mDb;
}
}
第3。用于执行数据库操作的活动类
TestAdapter adapter = new TestAdapter(this, "GREDB");
SQLiteDatabase db = adapter.open();
String whereClause = where_clause="WORDID like '"+compare_wordid+"'";
ContentValues cv= new ContentValues();
cv.put("RATING",ref_rate);
int n=db.update("WORD_A", cv, where_clause, null);
Toast.makeText(getApplicationContext(), "Rating updated to "+ref_rate,
Toast.LENGTH_LONG).show();
提供评分更新的吐司信息。但它没有在数据库中更新。
任何人都可以帮助我。非常感谢您的帮助......
谢谢....