用sql语句实现数据库的基本增删改查操作
public class SqliteActivity extends Activity {
MySqliteOpenHelper helper = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sqlite);
helper = new MySqliteOpenHelper(this, "mengk.db", null, 5);
}
public void createDb(View view) {
MySqliteOpenHelper helper = new MySqliteOpenHelper(this, "mengk.db", null, 5);
helper.getWritableDatabase();
}
public void add(View view) {
SQLiteDatabase db = helper.getWritableDatabase();
String sql = "insert into info(name,phone) values(?,?)";
String name = "mengk";
String phone = "17777777777";
db.execSQL(sql,new Object[]{name,phone});
db.close();
}
public void delete(View view) {
SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL("delete from info where name=?", new Object[]{"mengk"});
db.close();
}
public void update(View view) {
String sql = "update info set phone=? where name=?";
String name = "mengk";
String phone = "18888888888";
SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL(sql, new Object[]{phone,name});
db.close();
}
public void query(View view) {
String sql = "select * from info";
SQLiteDatabase db = helper.getWritableDatabase();
Cursor cursor = db.rawQuery(sql, null);
if (cursor != null && cursor.getColumnCount() > 0) {
while (cursor.moveToNext()) {
String name = cursor.getString(1);
String phone = cursor.getString(2);
System.out.println("name:"+name+"----"+phone);
}
cursor.close();
}
db.close();
}
}