日期:2014-05-16 浏览次数:20452 次
package android.sqlite; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteDatabase.CursorFactory; /** * SQLiteOpenHelper是一个辅助类,用来管理数据库的创建和版本他,它提供两个方面的功能 * 第一,getReadableDatabase()、getWritableDatabase()可以获得SQLiteDatabase对象,通过该对象可以对数据库进行操作 * 第二,提供了onCreate()、onUpgrade()两个回调函数,允许我们再创建和升级数据库时,进行自己的操作 */ public class DatabaseHelper extends SQLiteOpenHelper { private static final int VERSION = 1; /** * 在SQLiteOpenHelper的子类当中,必须有该构造函数 * @param context 上下文对象 * @param name 数据库名称 * @param factory * @param version 当前数据库的版本,值必须是整数并且是递增的状态 */ public DatabaseHelper(Context context, String name, CursorFactory factory, int version) { //必须通过super调用父类当中的构造函数 super(context, name, factory, version); } public DatabaseHelper(Context context, String name, int version){ this(context,name,null,version); } public DatabaseHelper(Context context, String name){ this(context,name,VERSION); } //该函数是在第一次创建的时候执行,实际上是第一次得到SQLiteDatabase对象的时候才会调用这个方法 @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub System.out.println("create a database"); //execSQL用于执行SQL语句 db.execSQL("create table user(id int,name varchar(20))"); } @Override public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { // TODO Auto-generated method stub System.out.println("upgrade a database"); } }
package android.sqlite; 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 createDatabaseButton = null; private Button updateDatabaseButton = null; private Button insertButton = null; private Button updateButton = null; private Button selectButton = null; private Button deleteButton = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 根据控件id获得相应的控件对象 createDatabaseButton = (Button) findViewById(R.id.createDatabase); updateDatabaseButton = (Button) findViewById(R.id.updateDatabase); insertButton = (Button) findViewById(R.id.insert); updateButton = (Button) findViewById(R.id.update); selectButton = (Button) findViewById(R.id.select); deleteButton = (Button) findViewById(R.id.delete); // 为按钮设置监听器 createDatabaseButton .setOnClickListener(new CreateDatabaseOnClickListener()); updateDatabaseBut