標籤:sqlite
1:建立一個繼承自SQLiteOpenHelper的DB.java
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 ;private 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() ;//開始測試所寫函數//首先放入五條資料for(int i = 0 ;i<5 ;i++) {ContentValues values = new ContentValues() ;values.put("name", "插入資料");values.put("age", i+20);insert("person",values) ;}delete("delete from person where age>23"); //將大於23歲的都刪除Cursor cursor = query("select * from person") ;//showData(cursor); //展示資料ContentValues values = new ContentValues() ;values.put("name", "測試update方法");values.put("age", 6666);update("person", values, "age=?",new String[]{"21"}); //將全部年齡為21的設定成6666showData(cursor);cursor.close();database.close(); }/* * @param tableName 表名稱 * @param values 資料集 */private void insert(String tableName,ContentValues values) {if(database != null) {database.insert(tableName, null, values) ;//當然也可以藉助database.exeSQL(sql語句執行)}else {Toast.makeText(this,"尚未執行個體化資料庫物件!!", Toast.LENGTH_LONG).show(); }}/* * 根據SQL語句刪除資料 */private void delete(String sql) {if(database != null) {database.execSQL(sql);//當然也可以藉助database.exeSQL(sql語句執行)}else {Toast.makeText(this,"尚未執行個體化資料庫物件!!", Toast.LENGTH_LONG).show(); }}/* * 查詢語句,返回一個Cursor遊標 */private Cursor query(String sql) {Cursor cursor = database.rawQuery(sql, null) ;return cursor ;}/* * 更改資料 */private void update(String table,ContentValues values,String whereClause,String[] whereArgs) {if(database != null) {database.update(table, values, whereClause, whereArgs) ;//當然也可以藉助database.exeSQL(sql語句執行)}else {Toast.makeText(this,"尚未執行個體化資料庫物件!!", Toast.LENGTH_LONG).show(); }}/* * 展示所有資料 */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+" 年齡:"+age+"\n");}this.text.append("所有資料查詢完畢!");}}
SQLite學習筆記