Android under the database operation--adding and deleting changes

Source: Internet
Author: User

Android under the first way to delete and change the database

1. Create an object for the helper class, call the Getreadabledatabase method, and return a Sqlitedatebase object

2. Use Sqlitedatebase object Call Execsql () to do additions and deletions, call rawquery method to do the query.

Features: Additions and deletions without a return value, you cannot determine whether the SQL statement was executed successfully. SQL statements written manually, easy to write wrong



private Mysqliteopenhelper Mysqliteopenhelper;
Public Infodao (context context) {
//Create a helper class object
mysqliteopenhelper = new Mysqliteopenhelper (context);

        
    }

Public void Add (Infobean bean) {

//Execute SQL statement requires Sqlitedatabase object
//Call the Getreadabledatabase method to initialize the creation of the database
Sqlitedatabase db = Mysqliteopenhelper.getreadabledatabase ();
//sql:sql Statement, the value of the placeholder in the Bindargs:sql statement
Db.execsql ("INSERT into info (name,phone) values (?,?);", New Object[]{bean.name,bean.phone});
//Close database objects
db.close ();
    }

Public void del (String name) {


//Execute SQL statement requires Sqlitedatabase object
//Call the Getreadabledatabase method to initialize the creation of the database
Sqlitedatabase db = Mysqliteopenhelper.getreadabledatabase ();
//sql:sql Statement, the value of the placeholder in the Bindargs:sql statement
db.execsql ("delete from info where name=?;", New Object[]{name});
//Close database objects
db.close ();

    }
Public void Update (Infobean bean) {

//Execute SQL statement requires Sqlitedatabase object
//Call the Getreadabledatabase method to initialize the creation of the database
Sqlitedatabase db = Mysqliteopenhelper.getreadabledatabase ();
//sql:sql Statement, the value of the placeholder in the Bindargs:sql statement
db.execsql ("Update info set phone=?") where name=?; ", New Object[]{bean.phone,bean.name});
//Close database objects
db.close ();

    }
Public void Query (String name) {
        
//Execute SQL statement requires Sqlitedatabase object
//Call the Getreadabledatabase method to initialize the creation of the database
Sqlitedatabase db = Mysqliteopenhelper.getreadabledatabase ();
//sql:sql Statement, Selectionargs: The value of the query condition placeholder, returns a cursor object
cursor cursor = db.rawquery ("Select _id, name,phone from info where name =?", New String []{name});
//Parse data in cursor
if (cursor! = NULL && cursor.getcount () >0) {//Determine if data exists in the cursor
            
//Iterate through the result set to get the contents of each row
while (Cursor.movetonext ()) {//condition, whether the cursor can be positioned to the next row
//Get Data
int id = cursor.getint (0);
String name_str = cursor.getstring (1);
String phone = cursor.getstring (2);
System.out.println ("_id:" +id+ "; Name:" +name_str+ ";p Hone:" +phone);
            }
cursor.close ();//close result set
            
        }
//Close database objects
db.close ();

    }
    


another way to change and delete under Android
    
1. Create an object for the helper class, call the Getreadabledatabase method, and return a Sqlitedatebase object

2. Use Sqlitedatebase object call Insert,update,delete, query method to do additions and deletions to change.

features: Adding and deleting the return value, you can determine whether the SQL statement is successful, but the query is not flexible enough to do a long table query. So in the company of General people to increase the deletion like the second way, query in the first way.

private Mysqliteopenhelper Mysqliteopenhelper;
Public Infodao (context context) {
//Create a helper class object
mysqliteopenhelper = new Mysqliteopenhelper (context);
    }

Public Boolean Add (Infobean Bean) {

//Execute SQL statement requires Sqlitedatabase object
//Call the Getreadabledatabase method to initialize the creation of the database
Sqlitedatabase db = Mysqliteopenhelper.getreadabledatabase ();
        
        
contentvalues values = new Contentvalues ();//is a map-encapsulated object used to store values
values.put ("name", Bean.name);
values.put ("Phone", bean.phone);
        
//table: Table name, Nullcolumnhack: Can be empty, Mark adds a blank line, values: The value of the data row, the return value: Represents the ID of the new row added, 1 for add failed
Long result = Db.insert ("info", null, values);//The underlying is the SQL statement being assembled
    
//Close database objects
db.close ();
        
if (Result! =-1) {//-1 represents add failure
return true;
}else{
return false;
        }
    }

public int del (String name) {

//Execute SQL statement requires Sqlitedatabase object
//Call the Getreadabledatabase method to initialize the creation of the database
Sqlitedatabase db = Mysqliteopenhelper.getreadabledatabase ();
        
//table: Table name, Whereclause: Delete condition, Whereargs: The parameter of the placeholder for the condition; return value: How many rows were successfully deleted
int result = Db.delete ("info", "name =?", new String[]{name});
//Close database objects
db.close ();
        
return result;

    }
public int Update (Infobean bean) {

//Execute SQL statement requires Sqlitedatabase object
//Call the Getreadabledatabase method to initialize the creation of the database
Sqlitedatabase db = Mysqliteopenhelper.getreadabledatabase ();
contentvalues values = new Contentvalues ();//is a map-encapsulated object used to store values
values.put ("Phone", bean.phone);
//table: Table name, Values: Updated value, Whereclause: Updated condition, Whereargs: Update the value of the placeholder for the condition, return value: How many rows were successfully modified
int result = db.update ("info", values, "name =?", new String[]{bean.name});
//Close database objects
db.close ();
return result;

    }
Public void Query (String name) {
    
//Execute SQL statement requires Sqlitedatabase object
//Call the Getreadabledatabase method to initialize the creation of the database
Sqlitedatabase db = Mysqliteopenhelper.getreadabledatabase ();
        
//table: Table name, columns: The column name of the query, if NULL represents the query for all columns, selection: Query condition, Selectionargs: The parameter value of the conditional placeholder,
//groupby: Grouped by what field, having: grouping conditions, ORDER by: What field to sort
cursor cursor = db.query ("info", new string[]{"_id", "name", "Phone"}, "name =?", new String[]{name}, NULL, NUL L, "_id desc");
//Parse data in cursor
if (cursor! = NULL && cursor.getcount () >0) {//Determine if data exists in the cursor
            
//Iterate through the result set to get the contents of each row
while (Cursor.movetonext ()) {//condition, whether the cursor can be positioned to the next row
//Get Data
int id = cursor.getint (0);
String name_str = cursor.getstring (1);
String phone = cursor.getstring (2);
System.out.println ("_id:" +id+ "; Name:" +name_str+ ";p Hone:" +phone);
                
                
            }
cursor.close ();//close result set
            
        }
//Close database objects
db.close ();

    }

Android under the database operation--adding and deleting changes

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.