SQLite學習筆記

來源:互聯網
上載者:User

標籤: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學習筆記

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.