SQLite learning notes, sqlite Learning

Source: Internet
Author: User

SQLite learning notes, sqlite Learning

1: Create a DB. java inherited from SQLiteOpenHelper

package com.example.sqlite;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;public class DB extends SQLiteOpenHelper {private String sql = "create table person("+"id integer primary key autoincrement,"+"name text,"+"age integer)" ;public DB(Context context, String name, CursorFactory factory,int version) {super(context, name, factory, version);}@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL(sql);}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {// TODO Auto-generated method stub}}


MainActivity. java

Package com. example. sqlite; import android. content. contentValues; import android. database. cursor; import android. database. sqlite. SQLiteDatabase; import android. database. sqlite. SQLiteOpenHelper; import android. OS. bundle; import android. support. v7.app. actionBarActivity; import android. widget. textView; import android. widget. toast; public class MainActivity extends ActionBarActivity {private TextView text; priv Ate SQLiteDatabase database = null; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); this. text = (TextView) findViewById (R. id. text); SQLiteOpenHelper helper = new DB (this, "test. db ", null, 1); database = helper. getWritableDatabase (); // start to test the written function. // first, add five pieces of data for (int I = 0; I <5; I ++) {ContentValues values = new ContentV Alues (); values. put ("name", "insert data"); values. put ("age", I + 20); insert ("person", values);} delete ("delete from person where age> 23 "); // Delete Cursor cursor = query ("select * from person"); // showData (cursor ); // display data ContentValues values = new ContentValues (); values. put ("name", "test update method"); values. put ("age", 6666); update ("person", values, "age =? ", New String [] {" 21 "}); // set 6666 showData (cursor); cursor for all ages 21. close (); database. close ();}/** @ param tableName table name * @ param values dataset */private void insert (String tableName, ContentValues values) {if (database! = Null) {database. insert (tableName, null, values); // Of course, you can also use database.exe SQL (SQL statement execution)} else {Toast. makeText (this, "database object has not been instantiated !! ", Toast. LENGTH_LONG). show () ;}/ ** delete data based on SQL statements */private void delete (String SQL) {if (database! = Null) Export database.exe cSQL (SQL); // Of course, you can also use database.exe SQL (SQL statement execution)} else {Toast. makeText (this, "the database object has not been instantiated !! ", Toast. LENGTH_LONG ). show () ;}}/** query statement, returns a Cursor */private Cursor query (String SQL) {cursor Cursor = database. rawQuery (SQL, null); return cursor;}/** Change Data */private void update (String table, ContentValues values, String whereClause, String [] whereArgs) {if (database! = Null) {database. update (table, values, whereClause, whereArgs); // you can also use database.exe SQL (SQL statement execution)} else {Toast. makeText (this, "the database object has not been instantiated !! ", Toast. LENGTH_LONG ). show () ;}}/** show all data */private void showData (Cursor cursor) {while (cursor. moveToNext () {int nameColumn = cursor. getColumnIndex ("name"); int ageColumn = cursor. getColumnIndex ("age"); String name = cursor. getString (nameColumn); int age = cursor. getInt (ageColumn); this. text. append ("name:" + name + "age:" + age + "\ n");} this. text. append ("all data query is complete! ");}}



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.