/** * 往Password表中添加记录 * * @param passwordInfo */ public void insert(PasswordInfo passwordInfo) throws SQLException { synchronized (dbHelper) { SQLiteDatabase db = dbHelper.getWritableDatabase(); // 为了防止SQL注入,输入的参数用?来占位符来指定 String addSql = "insert into password (password , question , answer , flag) values (?,?,?,?)"; try { db.execSQL( addSql, new Object[] { passwordInfo.getPassword(), passwordInfo.getQuestion(), passwordInfo.getAnswer(), passwordInfo.getFlag() }); } catch (Exception e) { e.printStackTrace(); } finally { db.close(); } Log.i(TAG, "insert"); } }
/** * 更新password表中的记录 * * @throws SQLException */ public void update(PasswordInfo passwordInfo) throws SQLException { synchronized (dbHelper) { SQLiteDatabase db = dbHelper.getWritableDatabase(); passwordInfo.set_id(1); String updateSql = "update " + PasswordTable.TABLE_NAME + " SET " + PasswordTable.PASSWORD + " = '" + passwordInfo.getPassword() + "'," + PasswordTable.QUESTION + " = '" + passwordInfo.getQuestion() + "'," + PasswordTable.ANSWER + " = '" + passwordInfo.getAnswer() + "'," + PasswordTable.FLAG + " = " + passwordInfo.getFlag() + " WHERE " + PasswordTable._ID + " = " + passwordInfo.get_id(); try { db.execSQL(updateSql); } catch (Exception e) { e.printStackTrace(); } finally { db.close(); } Log.i(TAG, "update"); } }