Initial use of Sqliteopenhelper

Source: Internet
Author: User

Inserting data

insert(table, nullColumnHack, values)

Table: Tables Name
Nullcolumnhack: Default value for empty columns
Values: A map that encapsulates column names and columns, representing a single piece of data

Delete data

db.delete(table, whereClause, whereArgs)

Table: Tables Name
Whereclause: Delete condition, can contain wildcard character "? ”
Whereargs: Delete a conditional parameter array to populate wildcard characters

Querying data

db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy)

Table: Tables Name
Columns: Column an array group
Selection: A conditional where clause, which can contain wildcards "? ”
GroupBy: Grouping columns
Having: grouping conditions
ORDER BY: Row sequence

modifying data

db.update(table, values, whereClause, whereArgs)

Table: Name of values: Updated record information
Whereclause: Updated condition that can contain wildcard characters "? ”
Whereargs: An updated array of conditional values used to populate wildcard characters

Refer to the code for specific actions:

 Public  class myhelper extends sqliteopenhelper {     Public StaticString db_name ="Note.db"; Public StaticString Tab_name ="Notes"; Public StaticString ID ="_id"; Public StaticString CONTENT ="Content"; Public StaticString DATE ="Date";PrivateSqlitedatabase DB; Public Myhelper(Context context) {Super(Context, Db_name,NULL,1); }/** * Called when creating a database * /    @Override     Public void onCreate(Sqlitedatabase db) { This. db = db; String sql =The CREATE TABLE notes (+ ID +"Integer primary key AutoIncrement,"+ CONTENT +"text,"+ DATE +"text)";//Build TableDb.execsql (SQL);//Insert some initial dataContentvalues values01 =NewContentvalues (); Values01.put (CONTENT,"I am the initial data one");        Values01.put (DATE, GETDATE ()); Contentvalues VALUES02 =NewContentvalues (); Values02.put (CONTENT,"I am the initial data two");        Values02.put (DATE, GETDATE ()); Db.insert (Tab_name,NULL, values01); Db.insert (Tab_name,NULL, VALUES02); }/** * Get current time * @return  * *     PublicStringgetdate() {Date dt =NewDate (); DateFormat DF =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String nowtime = df.format (DT);returnNowtime; }/** * Insert data * @param Values * *     Public void Insert(Contentvalues values)        {db = Getwritabledatabase (); Db.insert (Tab_name,NULL, values);    Db.close (); }/** * Delete data * @param Date * *     Public void Delete(String date)        {db = Getwritabledatabase (); Db.delete (Tab_name, DATE +"= ? ",NewString[] {date});    Db.close (); }/** * Query data * @return  * *     PublicCursorQuery() {db = Getwritabledatabase (); cursor cursor = db.query (Tab_name,NULL,NULL,NULL,NULL,NULL,NULL);returnCursor }/** * Update data * @param values * @param Date */     Public void Updata(Contentvalues values, String date)        {db = Getwritabledatabase (); Db.update (Tab_name, values, DATE +"= ? ",NewString[] {date}); }/** * Call */When updating several libraries    @Override     Public void Onupgrade(Sqlitedatabase DB,intOldversion,intNewVersion) {//TODO auto-generated method stub}}

Initial use of Sqliteopenhelper

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.