在下面的代码中,我尝试从数据库中获取和设置数据但在执行期间我无法在数据库中设置数据......
mWirelessRouters = WalletWirelessRouter.get(getActivity()).getWirelessRouter(uuid);
mBaseStationName = (EditText) v.findViewById(R.id.base_station_name);
mBaseStationName.setText(mWirelessRouters.getBaseStationName());
mBaseStationName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged
(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged
(CharSequence s, int start, int before, int count) {
mWirelessRouters.setBaseStationName(s.toString());
}
@Override
public void afterTextChanged
(Editable s) {
}
});
注意: - 在上面的代码中我尝试从用户那里获取基站名称并在数据库中更新它。没有问题相关的视图
public WirelessRouter getWirelessRouter(UUID id)
{
WalletCursorWrapper cursor = queryWireless(
WalletDbSchema.WirelessRouter.Cols.UUID + " =? "
, new String[]
{
id.toString()
}
);
try {
if (cursor.getCount() == 0) {
return null;
}
cursor.moveToFirst();
return cursor.getWirelessRouter();
} finally {
cursor.close();
}
}
private WalletCursorWrapper queryWireless (String whereClause, String[] whereArgs)
{
Cursor cursor = mDatabase.query(
WalletDbSchema.WirelessRouter.NAME,
null, // Colums - null select all colums
whereClause,
whereArgs,
null, //groupBy
null, //having
null // order
);
return new WalletCursorWrapper(cursor);
}
public void setBaseStationName(String baseStationName) {
mBaseStationName = baseStationName;
}
public void addWireless (WirelessRouter w)
{
ContentValues values = getContentValues(w);
mDatabase.insert(WalletDbSchema.WirelessRouter.NAME, null, values);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_item_new:
WirelessRouter wirelessRouter = new WirelessRouter();
WalletWirelessRouter.get(getActivity()).addWireless(wirelessRouter);
Intent intent = WirelessRouterPagerActivity
.newIntent(getActivity(), wirelessRouter.getUUID());
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
注意: - 我在创建片段时将空字符串数据插入数据库
public void upDateWireless(WirelessRouter wirelessRouter)
{
String uuidString = wirelessRouter.getUUID().toString();
ContentValues values = getContentValues(wirelessRouter);
mDatabase.update(WalletDbSchema.WirelessRouter.NAME, values,
WalletDbSchema.WirelessRouter.Cols.UUID + " =?"
, new String[]{uuidString});
}
public WalletWirelessRouter (Context context)
{
mContext = context.getApplicationContext();
mDatabase = new WalletBaseHelper(mContext)
.getWritableDatabase();
}
如果您需要更多数据,请告诉我们
答案 0 :(得分:0)
如果您希望在完成时保存值,则需要处于事务(并且COMMIT)。
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction()
...插入,更新,删除等
if (ok) {
db.setTransactionSucessful();
}
db.endTransaction();
db.close();
此总是对我有用。