android sqlite

可通过adb shell进入模拟android linux模式

 

image

 

image

 

 

//DatabaseHelper作为一个访问SQLite的助手类,提供两个方面的功能,
//第一,getReadableDatabase(),getWritableDatabase()可以获得SQLiteDatabse对象,通过该对象可以对数据库进行操作
//第二,提供了onCreate()和onUpgrade()两个回调函数,允许我们在创建和升级数据库时,进行自己的操作

public class DatabaseHelper extends SQLiteOpenHelper {
    
    private static final int VERSION = 1;
    //在SQLiteOepnHelper的子类当中,必须有该构造函数
    public DatabaseHelper(Context context, String name, CursorFactory factory,
            int version) {
        //必须通过super调用父类当中的构造函数
        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);
    }

    //该函数是在第一次创建数据库的时候执行,实际上是在第一次得到SQLiteDatabse对象的时候,才会调用这个方法
    @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 db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        System.out.println("update a Database");
    }

}

 

 

public class SQLiteActivity extends Activity {
    /** Called when the activity is first created. */
    private Button createButton;
    private Button insertButton;
    private Button updateButton;
    private Button updateRecordButton;
    private Button queryButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        createButton = (Button)findViewById(R.id.createDatabase);
        updateButton = (Button)findViewById(R.id.updateDatabase);
        insertButton = (Button)findViewById(R.id.insert);
        updateRecordButton = (Button)findViewById(R.id.update);
        queryButton = (Button)findViewById(R.id.query);
        createButton.setOnClickListener(new CreateListener());
        updateButton.setOnClickListener(new UpdateListener());
        insertButton.setOnClickListener(new InsertListener());
        updateRecordButton.setOnClickListener(new UpdateRecordListener());
        queryButton.setOnClickListener(new QueryListener());
    }
    class CreateListener implements OnClickListener{
        @Override
        public void onClick(View v) {
            //创建一个DatabaseHelper对象
            DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,"test_mars_db");
            //只有调用了DatabaseHelper对象的getReadableDatabase()方法,或者是getWritableDatabase()方法之后,才会创建,或打开一个数据库
            SQLiteDatabase db = dbHelper.getReadableDatabase();
        }
    }
    class UpdateListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,"test_mars_db",2);
            SQLiteDatabase db = dbHelper.getReadableDatabase();
        }
        
    }
    class InsertListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            //生成ContentValues对象
            ContentValues values = new ContentValues();
            //想该对象当中插入键值对,其中键是列名,值是希望插入到这一列的值,值必须和数据库当中的数据类型一致
            values.put("id", 1);
            values.put("name","zhangsan");
            DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,"test_mars_db",2);
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            //调用insert方法,就可以将数据插入到数据库当中
            db.insert("user", null, values);
        }
    }
    //更新操作就相当于执行SQL语句当中的update语句
    //UPDATE table_name SET XXCOL=XXX WHERE XXCOL=XX...
    class UpdateRecordListener implements OnClickListener{

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            //得到一个可写的SQLiteDatabase对象
            DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,"test_mars_db");
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("name", "zhangsanfeng");
            //第一个参数是要更新的表名
            //第二个参数是一个ContentValeus对象
            //第三个参数是where子句
            db.update("user", values, "id=?", new String[]{"1"});
        }
    }
    class QueryListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            System.out.println("aaa------------------");
            Log.d("myDebug", "myFirstDebugMsg");
            
            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);
            }
        }
    }
    
}
posted on 2012-07-08 08:42  马德华  阅读(188)  评论(0编辑  收藏  举报