Android SQLite API usage (non-original), androidsqlite

Source: Internet
Author: User

Android SQLite API usage (non-original), androidsqlite

1. Use the SQLite API to add, delete, modify, and query databases.

Package com. example. sqlitedatabase. test; import android. content. contentValues; import android. database. cursor; import android. database. sqlite. SQLiteDatabase; import android. test. androidTestCase; import com. example. sqlitedatabase. myOpenHelper; public class JunitTest2 extends AndroidTestCase {private MyOpenHelper helper; private SQLiteDatabase db; @ Overrideprotected void setUp () throws Exception {// TODO Auto -Generated method stubsuper. setUp (); helper = new MyOpenHelper (getContext (), "emp. db ", null, 1); db = helper. getWritableDatabase () ;}@ Overrideprotected void tearDown () throws Exception {// TODO Auto-generated method stubsuper. tearDown (); db. close ();} public void test () {} public void insertAction () mongodb.exe cSQL ("insert into Emp (name, salary) values ('zhang Wuji ', '123 ') "mongodb.exe cSQL (" insert into Emp (name, salary) Values ('zhao min', '000000') "comment into db.exe cSQL (" insert into Emp (name, salary) values ('shessent', '20170101 ')");} public void deleteAction () mongodb.exe cSQL ("delete from Emp where name = 'zhao min'");} public void updateAction () mongodb.exe cSQL ("update Emp set salary = '000000' where name =? ", New Object [] {" Xie Xun "});} public void selectAction () {Cursor c = db. rawQuery ("select * from Emp", null); while (c. moveToNext () {String id = c. getString (c. getColumnIndex ("id"); String name = c. getString (c. getColumnIndex ("name"); String salary = c. getString (c. getColumnIndex ("salary"); System. out. println (id + "," + name + "," + salary) ;}} public void insertAPI () {// Add ContentValues values = new Cont EntValues (); // equivalent to map // when adding the key, it must be the Field values in the Emp table. put ("name", "Hong qigong"); values. put ("salary", "5000"); // insert (String table, String nullColumnHack, ContentValues values) db. insert ("Emp", null, values);} public void deleteAPI () {// delete/** delete (String table, String whereClause, String [] whereArgs) * whereClause * the optional WHERE clause to apply when deleting. passing null will delete all rows. * wher EArgs * You may include? S in the where clause, which will be replaced by the values from whereArgs. the values will be bound as Strings. */int columns = db. delete ("Emp", "name =? ", New String [] {" Xie Xun "}); System. out. println ("number of rows:" + columns);} public void updateAPI () {// modify ContentValues values = new ContentValues (); values. put ("salary", 500); // update (String table, ContentValues values, String whereClause, String [] whereArgs) int columns = db. update ("Emp", values, "name =? ", New String [] {" Zhang Wuji "}); System. out. println ("number of rows:" + columns);} public void selectAPI () {// query (String table, String [] columns, String selection, String [] selectionArgs, string groupBy, String having, String orderBy) Cursor c = db. query ("Emp", new String [] {"name", "salary"}, "id>? ", New String [] {" 1 "}, null); System. out. println ("persons with IDs greater than 1:" + c. getCount () + "personal"); while (c. moveToNext () {String name = c. getString (c. getColumnIndex ("name"); // obtain the subscript String salary = c. getString (c. getColumnIndex ("salary"); System. out. println (name + "," + salary) ;}} public void transaction () {try {db. beginTransaction (); // start the transaction ContentValues values = new ContentValues (); values. pu T ("salary", 300); db. update ("Emp", values, "name =? ", New String [] {" Zhang Wuji "}); values. clear (); int r = 4/0; // simulate the error values. put ("salary", 5200); db. update ("Emp", values, "name =? ", New String [] {" Hong qigong "}); db. setTransactionSuccessful ();} catch (Exception e) {System. out. println ("transaction rollback");} finally {db. endTransaction (); // the transaction will be committed when the transaction ends }}}

  

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.