Android Operation SQLite Additions and deletions to implement code _android

Source: Internet
Author: User
Tags gettext sqlite
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);
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.