Android Sqliteopenhelper Database operations

Source: Internet
Author: User

A connection is created or opened only after the Databasehelper getwritabledatabase () method or the Getreadabledatabase () method is called

    • Sqlitedatabase db = Dbhelper.getreadabledatabase();

1. //execsql for executing SQL statements

    • Db.execsql ("CREATE TABLE User (id int,name varchar)");

2,//Call the Insert method, you can insert data into the database

  • ///First parameter: Table name
  • ///second parameter: SQL does not allow an empty column, if Contentvalues is empty, then this column is explicitly indicated as a null value
  •             //  Third parameter: Contentvalues object   
  • Db.insert ("user", null, values);

3.//Call the update method

  • ///First parameter string: Table name
  • ///second parameter Contentvalues:contentvalues object
  • ///The third parameter string:where sentence, equivalent to the statement in the SQL statement where? Number is a placeholder
  • ///Fourth parameter string[]: The value of a placeholder
  •             db.update (" Id=? " , new  string[] { " 1 "  });  

4,//Call the Query method of the Sqlitedatabase object for querying, return a cursor object: Result set object returned by the database query

  • ///First parameter string: Table name
  • ///second parameter string[]: column name to query
  • ///Third parameter string: Query condition
  • ///Fourth parameter string[]: parameters of the query condition
  • //Fifth parameter string: Groups The results of a query
  • ///Sixth parameter string: Limits The results of a grouping
  • //Seventh parameter string: Sort the results of a query
  •             cursor  cursor = db.query (, new string[] {  "id" ,  
  •                       "name"  } ,   "id=?" , new  string[] { " 1 "  }, null, null, null
  • //Moves the cursor to the next line to determine if the result set has the next data, returns True if any, and false if none

5.//Call the Delete method of the Sqlitedatabase object for deletion

///First parameter string: Table name

Second argument string: Conditional statement

Third parameter string[]: condition value

Sqlitedatabase.delete ("user", "id=?")  , new string[]{"1"});

System.out.println ("----------Delete----------");

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.