Basic Android SQLite operations

Source: Internet
Author: User

Basic Android SQLite operations

Add, delete, modify, and query related to SQLite are displayed in log.

Package com. example. sqlconnecttest; import android. content. context; import android. database. sqlite. SQLiteDatabase; import android. database. sqlite. SQLiteDatabase. cursorFactory; import android. database. sqlite. SQLiteOpenHelper; import android. util. log; public class DBHelper extends SQLiteOpenHelper {/** required constructors */public DBHelper (Context context, String name, CursorFactory factory, int version) {super (context, name, factory, version);}/** this method is called when a database is created for the first time */@ Overridepublic void onCreate (SQLiteDatabase db) {Log. I (createDatabases, create database --->) ;}/ ** call this method when updating the database */@ Overridepublic void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {Log. I (updateDatabase, update database ---> );}}

 

Package com. example. sqlconnecttest; import android. app. activity; import android. content. contentValues; import android. database. cursor; import android. database. SQLException; import android. database. sqlite. SQLiteDatabase; import android. OS. bundle; import android. util. log; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class MainActivity extends Activity Implements OnClickListener {private Button createDatabase; private Button createTable; private Button insert; private Button select; private Button update; private Button delete; private final String DATABASE_NAME = myDatabase; private SQLiteDatabase mySQLiteDatabase = null; private final String TABLE_NAME = user; private String SQL = CREATE TABLE [user] (+ [id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + [Username] varchar not null, + [password] varchar not null, + [phoneNumber] varchar not null); private DBHelper db; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); createView (); setListener ();} private void createView () {createDatabase = (Button) findViewById (R. id. createDatabase); createTable = (Button) findViewById (R. id. crea TeTable); insert = (Button) findViewById (R. id. insert); select = (Button) findViewById (R. id. select); update = (Button) findViewById (R. id. update); delete = (Button) findViewById (R. id. delete);} // Add the listener private void setListener () {createDatabase. setOnClickListener (this); createTable. setOnClickListener (this); insert. setOnClickListener (this); select. setOnClickListener (this); update. setOnClickListener (this); delete. s EtOnClickListener (this);} // various listening Methods @ Overridepublic void onClick (View view) {switch (view. getId () {case R. id. createDatabase: {db = new DBHelper (MainActivity. this, DATABASE_NAME, null, 1); mySQLiteDatabase = db. getWritableDatabase (); Log. I (database object, mySQLiteDatabase. toString (); // System. out. print (Database Object + mySQLiteDatabase); break;} case R. id. createTable: {db = new DBHelper (MainActivity. this, DATABASE_NAME, null, 1 ); MySQLiteDatabase = db. getWritableDatabase (); try using mysqlitedatabase.exe cSQL (SQL); Log. I (create a table, SQL);} catch (SQLException e) {e. printStackTrace (); Log. I (CREATE TABLE --> failed to create table ----------------);} break;} case R. id. insert: {db = new DBHelper (MainActivity. this, DATABASE_NAME, null, 1); mySQLiteDatabase = db. getWritableDatabase (); ContentValues cv = new ContentValues (); cv. put (username, Dad); cv. put (password, 123456); cv. pu T (phoneNumber, 134756658888); long n = mySQLiteDatabase. insert (TABLE_NAME, null, cv); Log. I (insert data, n +); mySQLiteDatabase. close (); break;} case R. id. select: {db = new DBHelper (MainActivity. this, DATABASE_NAME, null, 1); mySQLiteDatabase = db. getReadableDatabase ();/*** parameter 1: Table name * parameter 2: column to be displayed * parameter 3: where statement * parameter 4: where statement condition value * parameter 5: grouping Mode * parameter 6: having condition * parameter 7: Sorting mode */Cursor cursor = mySQLiteDatabase. query (TABLE_NA ME, new String [] {id, username, password, phoneNumber}, null, null); while (cursor. moveToNext () {int id = cursor. getInt (cursor. getColumnIndex (id); String username = cursor. getString (cursor. getColumnIndex (username); String password = cursor. getString (cursor. getColumnIndex (password); String phoneNumber = cursor. getString (cursor. getColumnIndex (phoneNumber); Log. I (query -->, id: + id + user Name: + username + password + phoneNumber);} mySQLiteDatabase. close (); break;} case R. id. update: {db = new DBHelper (MainActivity. this, DATABASE_NAME, null, 1); mySQLiteDatabase = db. getWritableDatabase (); ContentValues cv1 = new ContentValues (); cv1.put (username, admin); cv1.put (password, admin); cv1.put (phoneNumber, 1388888 ); string whereClause = id + = ?; String [] whereArgs = {8};/** parameter 1: Table name * parameter 2: A ContextValue object, which is the updated value * parameter 3: where statement condition * parameter 4: where condition value */int index = mySQLiteDatabase. update (TABLE_NAME, cv1, whereClause, whereArgs); Log. I (update -->, index +); break;} case R. id. delete: {db = new DBHelper (MainActivity. this, DATABASE_NAME, null, 1); mySQLiteDatabase = db. getWritableDatabase ();/***** parameter 1: Table name * parameter 2: where statement field * parameter 3: where statement field value */String whereCla Use1 = id + = ?; String [] whereArgs1 = {1}; int num = mySQLiteDatabase. delete (TABLE_NAME, whereClause1, whereArgs1); Log. I (delete record -->, num +); mySQLiteDatabase. close (); break;} default: break ;}}}
     
  
   
   
   
   
   
   
  
 

Running result

 

 

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.