db.execsql(sql); 或者db.insert()、db.delete()、db.update(),並且包括數據表的創建和刪除等等也可以通過execsql實現,實例代碼如下:
- //創建表
- public boolean createtable(){
- sqlitedatabase db=dbhelper.getwritabledatabase();
- string sql="create table if not exists "+table_name+"(id integer primary key,name varchar,age integer)";
- try{
- db.execsql(sql);
- return true;
- }catch(sqlexception ex){
- log.d(tag, "create table failure");
- return false;
- }
- }
- //添加數據
- public boolean adddata(){
- string name=etname.gettext().tostring();
- string age=etage.gettext().tostring();
- sqlitedatabase db=dbhelper.getwritabledatabase();
- string sql="insert into "+table_name+"(name,age) values ('"+name+"','"+age+"')";
- try{
- db.execsql(sql);
- return true;
- }catch(sqlexception ex){
- log.d(tag, "add data failure");
- return false;
- }
- }
- //修改
- public boolean updatedata(){
- sqlitedatabase db=dbhelper.getwritabledatabase();
- string sql="update "+table_name+" set age='2' where name like 'cb'";
- object[] bindargs={"cb"};
- try{
- db.execsql(sql, bindargs);
- return true;
- }catch(sqlexception ex){
- log.d(tag, "update data failure");
- return false;
- }
- }