If a database in an application does not need to provide external access, implement a database help class that inherits from Sqliteopenhelper to support database creation and version updating. These sqlitedatabase can not be achieved. But Sqlitedatabase has some very important methods of database operation, the creation of data table deletion, data additions and deletions are implemented through it.
Implement and modify the Operation method: Db.execsql (SQL); or Db.insert (), Db.delete (), Db.update (), and including the creation and deletion of data tables, etc. can also be achieved through EXECSQL
Copy Code code as follows:
Create a table
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;
}
}
Add data
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;
}
}
Modify
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;
}
}
Execute data Query method: Db.rawquery (SQL, Selectionargs); or db.query (table, columns, selection, Selectionargs, GroupBy, having, by);
Copy Code code as follows:
//query public void Selectdata () {
Sqlitedatabase db= Dbhelper.getreadabledatabase ();
String[] columns={"name"};
Cursor cursor=db.query (table_name, columns, NULL, NULL, NULL, NULL, NULL);
String names= "";
while (Cursor.movetonext ()) {
int c=cursor.getcolumnindexorthrow ("Name");
String name=cursor.getstring (c);
//< = >
//string name=cursor.getstring (0);//Only one column of
if (names== "") {
Names=name is queried;
}else{
names=names+ "\ n" +name;
}
}
Tvname.settext (names);
//Another query method
//string sql= "select name from" +table_name;
//CUROSR cursor=db.rawquery (sql, NULL);
}