Android Open Source - DataPersistence S Q Lite Activity






From Project

Back to project page DataPersistence.

License

The source code is released under:

Apache License

If you think the Android project DataPersistence listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.datapersistencedemo;
/* ww  w . j  a  va  2s  .c  om*/
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class SQLiteActivity extends Activity {
  
  private Button createButton,insertButton,deleteButton,queryButton;
  private MyDatabaseHelp dbHelp;

  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sqlite);
    
    init();
    createButton.setOnClickListener(new View.OnClickListener() {
      
      @Override
      public void onClick(View arg0) {
        dbHelp.getWritableDatabase();  //?????????Create DB????????????????????BookStore.db????????
                                    //??????????????????MyDatabaseHelp???onCreate()??????????
      }
    });
    insertButton.setOnClickListener(new View.OnClickListener() {
      
      @Override
      public void onClick(View arg0) {
        SQLiteDatabase db = dbHelp.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("account", "wusp");
        contentValues.put("password", "123456");
        contentValues.put("phone", "123456789");
        db.insert("User", null, contentValues);
        contentValues.clear();
        /*db.execSQL("insert into table User(account,password,phone) values(?,?,?)  ",
                  new String[] {"wusp","123456","123456789"});*/
      }
    });
    deleteButton.setOnClickListener(new View.OnClickListener() {
      
      @Override
      public void onClick(View arg0) {
        //???????
        SQLiteDatabase db = dbHelp.getWritableDatabase();
        db.beginTransaction();
        try {
          db.delete("User", "acccount = ?", new String[] {"wusp"});
          /*db.execSQL("delete from User where account = ?",new String[] {"wusp"});*/
          db.setTransactionSuccessful();
        } catch (Exception e) {
        }finally{
          db.endTransaction();
        }
      }
    });
    queryButton.setOnClickListener(new View.OnClickListener() {
      
      @Override
      public void onClick(View arg0) {
        SQLiteDatabase db = dbHelp.getWritableDatabase();
        //???Cursor????????????????????????
        Cursor cursor = db.query("User", null, null, null, null, null, null);
        /*cursor = db.rawQuery("select * from User",null);*/
        if (cursor.moveToFirst()) {
          //???????????cursor???????
          do {
            String account = cursor.getString(cursor.getColumnIndex("account"));
            String password = cursor.getString(cursor.getColumnIndex("password"));
            String phone = cursor.getString(cursor.getColumnIndex("phone"));
            Log.d("test", account);
            Log.d("test", password);
            Log.d("test", phone);
          } while (cursor.moveToNext());
        }
        cursor.close();    //???cursor??
      }
    });
  }
  private void init() {
    createButton = (Button) findViewById(R.id.create);
    insertButton = (Button) findViewById(R.id.insert);
    deleteButton = (Button) findViewById(R.id.delete);
    queryButton = (Button) findViewById(R.id.query);
    dbHelp = new MyDatabaseHelp(this, "BookStore.db", null, 1);  
  }
}




Java Source Code List

com.example.datapersistencedemo.MainActivity.java
com.example.datapersistencedemo.MyDatabaseHelp.java
com.example.datapersistencedemo.SQLiteActivity.java
com.example.datapersistencedemo.SharedPreferencesActivity.java