Android的SQLite数据库操作

今天学习数据库操作。
先写个类继承SQLiteOpenHelper类:


package com.db;

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 {
	public static final int VERSION=1;
	public DBHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}

	public DBHelper(Context context, String name, int version) {
		this(context, name, null, version);
		// TODO Auto-generated constructor stub
	}
	public DBHelper(Context context, String name) {
		this(context,name,VERSION);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		System.out.println("创建数据库");
		db.execSQL("create table fav (id integer primary key autoincrement,title text not null,url text not null,thumb text not null);");
		
		
	}

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

}

下面是在程序中增,改,删,查的方法


package com.db;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;

public class main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv=(TextView)findViewById(R.id.tv);
        DBHelper dbHelper=new DBHelper(this,"youku");//youku为数据库名
        //执行onCreate(),如果参数加版本号则执行onUpgrade()
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        //getReadableDatabase或getWriteableDatabase后才创建数据库
        //插入
        ContentValues cv=new ContentValues();
        cv.put("title","标题");
        cv.put("url","http://url");
        cv.put("thumb","http://thumb");
        db.insert("fav", null, cv); 
        //insert into fav('title','url','thumb') values('标题','http://url','http://thumb');
        //更新
        cv.clear();
        cv.put("title", "English");
        db.update("fav",cv,"id=?",new String[]{"1"});
        //update fav set title='english' where id='1'
        //表名、ContentValues对象、where子句的条件加占位符,一个占位符对应后面的数组的一个元素,相当于id=1
        //查询 select id,title,url,thumb from fav
        Cursor cursor=db.query("fav",new String[]{"id","title","url","thumb"},null,null,null,null,null);
        String s="";
        while(cursor.moveToNext()){
        	s+=cursor.getString(0)+":"+cursor.getString(1)+"\n";
        }
        tv.setText(s);
        //删除
        db.delete("fav","id=?",new String[]{"3"});
        //相当 于 delete from fav where id='3';
        
        
    }
}

© 2011, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记