Android Growth Diary-sqlite[2 of data storage]

Source: Internet
Author: User

Part One:

First look at this piece of code

1Sqlitedatabase db=openorcreatedatabase ("Sqldemo.db", Mode_private,NULL);2         //Create a table3String create_sql= "CREATE table if not exists SQLTB (_id integer primary key autoincrement,name text not null,sex text not Null,age integer NOT NULL) ";4 Db.execsql (create_sql);5Db.execsql ("INSERT into SQLTB (name,sex,age) VALUES (' Zhang San ', ' Male ', ' 19 ')");6Db.execsql ("INSERT into SQLTB (name,sex,age) VALUES (' John Doe ', ' Male ', ' 20 ')");7         8Cursor c=db.rawquery ("SELECT * from SQLTB",NULL);9          if(c!=NULL)Ten          { One              while(C.movetonext ()) A             { -LOG.I ("info", "_id" +c.getint (C.getcolumnindex ("_id"))); -LOG.I ("info", "name" +c.getstring (C.getcolumnindex ("name"))); theLOG.I ("info", "Sex" +c.getstring (C.getcolumnindex ("Sex")))); -LOG.I ("Info", "Age" +c.getint (The C.getcolumnindex ("Age"))); -LOG.I ("info", "-------------------------------------------------"); -             } + c.close (); -          } + db.close (); A}

Ps:

①:openorcreatedatabase (name, mode, factory): CREATE DATABASE Database with a return value of sqlitedatabase

Name: Database names Mode: Database type factory: In order to be able to customize the cursor creation

②:execsql () This method is the statement that executes the database

③:rawquery () This method is used to query the database for information, and the return value is a cursor object

④: It needs to be closed after traversing with MoveToNext () , and it needs to be closed after the database is used to save memory.

Cons: This approach requires some knowledge of the database.

---------------------------------------Ornate Split Line---------------------------------------------------

Part:

1Sqlitedatabase db=openorcreatedatabase ("Stu.db", Mode_private,NULL);2String create= "CREATE table if not exists STUTB (_id integer primary key autoincrement,name text not null,age integer not n Ull,sex text NOT NULL) ";3Db.execsql (create);//Create a table4         /*5 * Inserting data into tables in a database6          */7Contentvalues values=Newcontentvalues ();8Values.put ("name", "Zhang San");//Key-value pair form key: Database column Name value: The contents of the corresponding column9Values.put ("Age", 18);TenValues.put ("Sex", "male"); OneDb.insert ("Stutb",NULL, values);//returns a value of type Long that is the ID of the row AValues.clear ();//clearing data for key-value pairs -Values.put ("name", "John Doe"); -Values.put ("Age", 19); theValues.put ("Sex", "female"); -Db.insert ("Stutb",NULL, values); - values.clear (); -Values.put ("name", "Harry"); +Values.put ("Age", 21); -Values.put ("Sex", "male"); +Db.insert ("Stutb",NULL, values); A values.clear (); at         /* - * Update data in the database -          */ -Values.put ("Sex", "female"); -         //db.update (table, values, Whereclause, Whereargs) -Db.update ("Stutb", Values, "_ID>?",Newstring[]{"2"});//to change all id>2 sex into a woman. in         /* - * Delete data in the database to          */ +         //db.delete (table, Whereclause, Whereargs) -Db.delete ("Stutb", "name like?",Newstring[]{"% three"});//Delete the data with three in the name the         /* * * Querying data in the database $          */Panax Notoginseng         //db.query (table, columns, selection, Selectionargs, GroupBy, having, by-and-by) -Cursor c=db.query ("Stutb",NULL, "_ID>?",Newstring[]{"0"},NULL,NULL, "name");//sort the id>0 data by name the         if(c!=NULL) +         { AString[] Columns=c.getcolumnnames (); the              while(C.movetonext ()) +             { -                 //get a row of data $                  for(String columnsname:columns) $                 { -LOG.I ("Info", C.getstring (C.getcolumnindex (Columnsname))); -                 } the             } - c.close ();Wuyi         } the db.close (); -}

Ps:

① Insert: Db.insert (table, Nullcolumnhack, values) //Parameters correspond to: Table name, default value, values value

The values are stored in the contentvalues type of data, so the object should be created: contentvalues values = new Contentvalues ();

Values.put ("Column name", "content");

② Update:db.update (table, values, Whereclause, Whereargs) //parameters corresponding to: Table name, updated content, condition, condition value (in array type new string[]{"content"})

③ Delete:db.delete (table, Whereclause, Whereargs) //parameters corresponding to: Table name condition, condition value

④ query:db.query (table, columns, selection, Selectionargs, groupBy, having, order by) //Parameters correspond to: Table name, number of columns queried (null is all), condition, condition value (in array type new string[]{"content"}), group,< default to null>, rule sort------>> return value to cursor type data

⑤ Note: The database information should be closed in time for the end of use

Android Growth Diary-sqlite[2 of data storage]

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.