数据存储(四)

数据库是Android存储方案的核心,在Adnroid系统中使用了SQLite数据库,这种数据库是非常轻量的数据库。下面就该如何创建数据库和如何操作数据库来整理一一些资料。

一、使用SQLiteOpenHelper的子类来完成创建、打开数据库及各种数据库的操作。

使用SQLiteOpenHelper类,必须实现两个方法onCrete 方法和onUpgrade。

public void onCrete(SQLiteDatabase db)

如果数据库文件不存在,SQLiteOpenHelper在自动创建数据库后只会调用onCreate方法,在该方法一般需要创建数据库中的表,视图等组件。

public void onUpgrade(SQLiteDatabase db, int oldVerison,int newVersion)

如果数据库存在,并且当前的版本号高于上次创建或升级时的版本号时,就会调用这个方法更新数据库版本号。

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 DatabaseHelper extends SQLiteOpenHelper{

	private final static int VERSION = 1;
	public DatabaseHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}
	public DatabaseHelper(Context context,String name){
		this(context,name,VERSION);
	}
	public DatabaseHelper(Context context,String name,int version){
		this(context,name,null,version);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		System.out.println("create SQLiteDatabase");
		db.execSQL("create table user(id int,name varchar(20))");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		System.out.println("updata SQLiteDatabase");
	}

}

下面实现了数据库的增删改查功能

package com.example.activity_61;

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.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.example.sqlite.DatabaseHelper;

public class SQLiteActivity extends Activity {

	private Button createDatabaseButton=null;
	private Button updataDatabaseButton=null;
	private Button updataButton = null;
	private Button queryButton = null;
	private Button insertButton = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sqlite);
		createDatabaseButton  = (Button)findViewById(R.id.createDatabaseButton);
		createDatabaseButton.setOnClickListener(new CreateDatabaseListener());
		updataDatabaseButton = (Button)findViewById(R.id.updataDatabaseButton);
		updataDatabaseButton.setOnClickListener(new UpdataDatabaseListener());
		insertButton = (Button)findViewById(R.id.insertButton);
		insertButton.setOnClickListener(new InsertButtonListener());
		updataButton = (Button)findViewById(R.id.updataButton);
		updataButton.setOnClickListener(new UpdataButtonListener());
		queryButton = (Button)findViewById(R.id.queryButton); 
		
	}

	class CreateDatabaseListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Log.d("mydebug", "MyfirstBug");
			DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,"test_mars_db");
			SQLiteDatabase db = dbHelper.getReadableDatabase();
		}	
	}
	
	class UpdataDatabaseListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,"test_mars_db",2);
			SQLiteDatabase db = dbHelper.getReadableDatabase();
		}	
	}
	
	class InsertButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			ContentValues values = new ContentValues();
			values.put("id", 1);
			values.put("name", "xiebangyuan");
			DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,"test_mars_db");
			SQLiteDatabase db = dbHelper.getWritableDatabase();
			db.insert("user", null, values);
		}
	}
	
	class UpdataButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,"test_mars_db");
			SQLiteDatabase db = dbHelper.getWritableDatabase();
			ContentValues values = new ContentValues();
			values.put("name", "xieyurou");
			db.update("user", values, "id=?", new String[]{"1"});
		}
	}
	
	class QueryButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,"test_mars_db");
			SQLiteDatabase db = dbHelper.getReadableDatabase();
			Cursor cursor = db.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("query--->"+name);
			}
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.sqlite, menu);
		return true;
	}
}

还可以将表中的数据显示在ListView等控件中,Android SDK提供了一个专用于数据绑定的Adapter类:SimpleCursorAdapter

	DBService dbService = new DBService(this);
		Cursor cursor = dbService.query("select * from t_test",null);
		@SuppressWarnings("deprecation")
		SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,
				android.R.layout.simple_expandable_list_item_1, cursor,
				new String[]
				{"name" }, new int[]
				{ android.R.id.text1});
		
		setListAdapter(simpleCursorAdapter);
	}


你可能感兴趣的:(数据存储(四))