Android SQLite basic operations, androidsqlite

Source: Internet
Author: User

Android SQLite basic operations, androidsqlite

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 autoinre MENT, "+" [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) findView ById (R. id. createTable); 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. setOnClickListener (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, DATABA SE_NAME, null, 1); mySQLiteDatabase = db. getWritableDatabase (); try using mysqlitedatabase.exe cSQL (SQL); Log. I ("create table", SQL);} catch (SQLException e) {e. printStackTrace (); Log. I ("Create a table --", "failed to create a 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. put ("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 cu Rsor = mySQLiteDatabase. query (TABLE_NAME, 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. getColum NIndex ("phoneNumber"); Log. I ("query -->", "id:" + id + "userName:" + username + "password" + password + "phoneNumber" + 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 ("phone Number "," 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 WhereClause1 = "id" + "=? "; String [] whereArgs1 = {" 1 "}; int num = mySQLiteDatabase. delete (TABLE_NAME, whereClause1, whereArgs1); Log. I ("delete record -->", num + ""); mySQLiteDatabase. close (); break;} default: break ;}}}
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = "com. example. sqlconnecttest. mainActivity "> <TextView android: id =" @ + id/textView "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text = "SQLite Database"/> <Button android: id = "@ + id/createDatabase" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_below = "@ id/textView" android: text = "Create a database"/> <Button android: id = "@ + id/createTable" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_below = "@ id/createDatabase" android: text = "Create a table"/> <Button android: id = "@ + id/insert" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_below = "@ id/createTable" android: text = "insert data"/> <Button android: id = "@ + id/select" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_below = "@ id/insert" android: text = ""/> <Button android: id = "@ + id/update" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_below = "@ id/select" android: text = "update data"/> <Button android: id = "@ + id/delete" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_below = "@ id/update" android: text = "delete data"/> </RelativeLayout>

Running result

 

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.