Android数据存储之SQLite
这一节比较重要,SQLite是一个轻量级的数据库。它的功能叶很全,里面的各种数据库语句也一样。
在Android都应用中都使用了SQLite数据库,比如:电话本。
Activity:
package com.ko8e;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.View.OnClickListener;import android.widget.Button;public class SQLiteActivity extends Activity {/** Called when the activity is first created. */private Button button1 = null;private Button button2 = null;private Button button3 = null;private Button button4 = null;private Button button5 = null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);button1 = (Button) findViewById(R.id.button1);button2 = (Button) findViewById(R.id.button2);button3 = (Button) findViewById(R.id.button3);button4 = (Button) findViewById(R.id.button4);button5 = (Button) findViewById(R.id.button5);button1.setOnClickListener(new CreateListener());button2.setOnClickListener(new UpdateRecordListener());button3.setOnClickListener(new InsertListener());button4.setOnClickListener(new QueryListener());button5.setOnClickListener(new UpdateListener());}private class CreateListener implements OnClickListener {@Overridepublic void onClick(View v) {DBhelper db = new DBhelper(SQLiteActivity.this, "ko8e_db");SQLiteDatabase sqld = db.getReadableDatabase();}}private class UpdateRecordListener implements OnClickListener {@Overridepublic void onClick(View v) {DBhelper db = new DBhelper(SQLiteActivity.this, "ko8e_db");SQLiteDatabase sqld = db.getReadableDatabase();ContentValues values = new ContentValues();values.put("name", "kobe bryant");sqld.update("user", values, "id=?", new String[]{"1"});}}private class InsertListener implements OnClickListener {@Overridepublic void onClick(View v) {ContentValues values = new ContentValues();values.put("id", 1);values.put("name", "ko8e");DBhelper db = new DBhelper(SQLiteActivity.this, "ko8e_db", 2);SQLiteDatabase sqld = db.getReadableDatabase();sqld.insert("user", null, values);}}private class QueryListener implements OnClickListener {@Overridepublic void onClick(View v) {DBhelper db = new DBhelper(SQLiteActivity.this, "ko8e_db");SQLiteDatabase sqld = db.getReadableDatabase();Cursor cursor = sqld.query("user", new String[]{"id","name"}, "id=?", new String[]{"1"}, null, null, null);while(cursor.moveToNext()) {String name = cursor.getString(cursor.getColumnIndex("name"));System.out.println("查询: " + name);}}}private class UpdateListener implements OnClickListener {@Overridepublic void onClick(View v) {DBhelper db = new DBhelper(SQLiteActivity.this, "ko8e_db", 2);SQLiteDatabase sqld = db.getReadableDatabase();}}}
封装类:
package com.ko8e;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.database.sqlite.SQLiteDatabase.CursorFactory;public class DBhelper extends SQLiteOpenHelper{private static final int VERSION = 1;public DBhelper(Context context, String name, CursorFactory factory,int version) {super(context, name, factory, version);}public DBhelper(Context context, String name) {this(context, name, VERSION);}public DBhelper(Context context, String name, int version) {this(context, name, null, version);}@Overridepublic void onCreate(SQLiteDatabase arg0) {System.out.println("create a DB");arg0.execSQL("create table user(id int, name varchar(20))");}@Overridepublic void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {System.out.println("update a DB");}}
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextViewandroid:id="@+id/view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /><Buttonandroid:id="@+id/button1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/create"/> <Buttonandroid:id="@+id/button2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/add"/><Buttonandroid:id="@+id/button3"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/insert"/><Buttonandroid:id="@+id/button4"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/query"/><Buttonandroid:id="@+id/button5"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/update"/></LinearLayout>
页:
[1]