Android Open Source - sqlite_mod Sqlite_mod Activity






From Project

Back to project page sqlite_mod.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project sqlite_mod 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.zhufeng.sqlite;
//from  w  ww .j a  v a 2 s .com
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

// ------------??????????--------?SQLite?---------
/**
 * @author Himi
 * @???????SQLite ???????????
 * @??? ????????????????????????????? ?????????????? ??SQLite???????????????SD????
 * @???1?????????????(????)????????????????????? ????????????????
 * @???2 apk????????????????????????/??, ????????????????sdcard???????????.
 * @???3 ????id????????????????????????SQLite?????????
 *      ????????????????????id??SQLite???????id???????????????
 * @???4 android ? ?SQLite ?????????!!!!!????????? String UPDATA_DATA =
 *      "UPDATE himi SET text='??SQL?????????????'  WHERE id=1"; ???? ?????????? String
 *      UPDATA_DATA = "updata himi set text='??SQL?????????????'  where id=1";
 */
public class Sqlite_modActivity extends Activity implements OnClickListener {
  private Button btn_addOne, btn_deleteone, btn_check, btn_deleteTable,
      btn_edit, btn_newTable;
  private TextView tv;
  private SqliteHelper myOpenHelper;// ??????SQLiteOpenHelper???
  private SQLiteDatabase mysql;

  // ---------------????????????????SD???????????????
  // private File path = new File("/sdcard/himi");// ????
  // private File f = new File("/sdcard/himi/himi.db");// ????
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);
    tv = (TextView) findViewById(R.id.tv_title);
    btn_addOne = (Button) findViewById(R.id.sql_addOne);
    btn_check = (Button) findViewById(R.id.sql_check);
    btn_deleteone = (Button) findViewById(R.id.sql_deleteOne);
    btn_deleteTable = (Button) findViewById(R.id.sql_deleteTable);
    btn_newTable = (Button) findViewById(R.id.sql_newTable);
    btn_edit = (Button) findViewById(R.id.sql_edit);
    btn_edit.setOnClickListener(this);
    btn_addOne.setOnClickListener(this);
    btn_check.setOnClickListener(this);
    btn_deleteone.setOnClickListener(this);
    btn_deleteTable.setOnClickListener(this);
    btn_newTable.setOnClickListener(this);
    myOpenHelper = new SqliteHelper(this);// ????????????
    // ??1 ----???????????????????SD??????????????mysql??????
    // if (!path.exists()) {// ??????false
    // path.mkdirs();// ??????
    // }
    // if (!f.exists()) {// ??????false
    // try {
    // f.createNewFile();//????
    // } catch (IOException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    // }
  }

  @Override
  public void onClick(View v) {
    try {
      // ??2----???????????????????SD??????????????mysql??????
      // mysql = SQLiteDatabase.openOrCreateDatabase(f, null);
      // ??3--- ??????????????????,?????????mysql??????
      mysql = myOpenHelper.getWritableDatabase(); // ???????
      if (v == btn_addOne) {// ??????
        // ---------------------- ?????????????---------
        // ContentValues ?????????HashMap? key?????????
        // Value??????????? ?? ContentValues ? put ???????
        // ???????ContentValues??????????????!
        ContentValues cv = new ContentValues();
        cv.put(SqliteHelper.TEXT, "????????");
        mysql.insert(SqliteHelper.TABLE_NAME, null, cv);
        // inser() ??????? ?????????????????
        // ??????? :???null??????
        // ?????????????
        // ---------------------- SQL????????--------------
        // String INSERT_DATA =
        // "INSERT INTO himi (id,text) values (1, '??SQL????????')";
        // db.execSQL(INSERT_DATA);
        tv.setText("??????????????????????");
      } else if (v == btn_deleteone) {// ??????
        // ---------------------- ???????????
        mysql.delete("himi", SqliteHelper.ID + "=1", null);
        // ??????? ???????????
        // ???????? id+?????? ????????null???????
        // ??????????null??????
        // ----------------------- SQL?????????
        // String DELETE_DATA = "DELETE FROM himi WHERE id=1";
        // db.execSQL(DELETE_DATA);
        tv.setText("??????????????????????");
      } else if (v == btn_check) {// ????????
      // ??4------
        Cursor cur = mysql.rawQuery("SELECT * FROM "
            + SqliteHelper.TABLE_NAME, null);
        if (cur != null) {
          String temp = "";
          int i = 0;
          while (cur.moveToNext()) {// ????false????????????
            temp += cur.getString(0);
            // ????0 ???????,???0???id?
            temp += cur.getString(1);
            // ???0????????????text??
            i++;
            temp += "  "; // ????????????? ,??~
            if (i % 3 == 0) // ????????????? ,??~
              temp += "\n";// ????????????? ,??~
          }
          tv.setText(temp);
        }
      } else if (v == btn_edit) {// ??????
        // ------------------------???????????? -------------
        ContentValues cv = new ContentValues();
        cv.put(SqliteHelper.TEXT, "??????????");
        mysql.update("himi", cv, "id " + "=" + Integer.toString(3),
            null);
        // ------------------------SQL????????? -------------
        // String UPDATA_DATA =
        // "UPDATE himi SET text='??SQL?????????????'  WHERE id=1";
        // db.execSQL(UPDATA_DATA);
        tv.setText("??????????????????????");
      } else if (v == btn_deleteTable) {// ???
        mysql.execSQL("DROP TABLE himi");
        tv.setText("???????????????????");
      } else if (v == btn_newTable) {// ???
        String TABLE_NAME = "himi";
        String ID = "id";
        String TEXT = "text";
        String str_sql2 = "CREATE TABLE " + TABLE_NAME + "(" + ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT," + TEXT
            + " text );";
        mysql.execSQL(str_sql2);
        tv.setText("???????????????????");
      }
      // ???????:
      // this.deleteDatabase("himi.db");
    } catch (Exception e) {
      tv.setText("???????");
    } finally {// ??try?????????????????
      mysql.close();
    }
  }
}




Java Source Code List

com.zhufeng.sqlite.SqliteHelper.java
com.zhufeng.sqlite.Sqlite_modActivity.java