android的sqlite使用

SQLiteOpenHelper是一个抽象类

getReadableDatabase创建或打开一个可读数据库

getWritableDatabase创建或打开一个可写数据库

onCreate数据库第一次被建立时调用

onOpen数据库打开时调用

onUpdated数据库修改时被调用

 

建立一个SQLiteOpenHelper必须写一个构造函数 public DataBaseHelper(Context context, String name, CursorFactory factory,int version)

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,int version){
		this(context,name,null,version);
	}
	public DataBaseHelper(Context context,String name){
		this(context,name,VERSION);
	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		String sql = "CREATE TABLE user(id int,name vchar(20))";
		System.out.println("create table");
		db.execSQL(sql);
	}

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

}

创建主activity

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.widget.Button;

public class sqliteActivity extends Activity {
    /** Called when the activity is first created. */
	private Button btnCreateDb = null;//创建数据库
	private Button btnUpdateDb = null;//更新数据库
	private Button btnInsert = null;
	private Button btnUpdate = null;
	private Button btnSelect = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnCreateDb = (Button) findViewById(R.id.btnCreateDB);
        btnCreateDb.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				DataBaseHelper dbh = new DataBaseHelper(sqliteActivity.this,"test db name");
				dbh.getWritableDatabase();
			}
		});
        
        btnUpdateDb = (Button) findViewById(R.id.btnUpdateDB);
        btnUpdateDb.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				DataBaseHelper dbh = new DataBaseHelper(sqliteActivity.this,"test db name",2);
				dbh.getWritableDatabase();
			}
		});
        btnInsert = (Button) findViewById(R.id.btnInsert);
        btnInsert.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 建立键值对为数据表插入准备,必须保持与数据表一致
				ContentValues values = new ContentValues();
				values.put("id",1);
				values.put("name", "xiaoqiang");
				//连接数据库辅助
				DataBaseHelper dbh = new DataBaseHelper(sqliteActivity.this,"test db name");
				//建立数据库
				SQLiteDatabase db = dbh.getWritableDatabase();
				//插入表格内容
				db.insert("user", null, values);
				
			}
		});
    	btnUpdate = (Button) findViewById(R.id.btnUpdate);
    	btnUpdate.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ContentValues values = new ContentValues();
				values.put("name", "chenqiang");
				//连接数据库辅助
				DataBaseHelper dbh = new DataBaseHelper(sqliteActivity.this,"test db name");
				//建立数据库
				SQLiteDatabase db = dbh.getWritableDatabase();
				//更新数据库
				//第一个参数是table名字
				//第二个参数是要修改的参数键值对
				//第三个参数where条件 ?代表占位符号
				//第四个参数条件的值
				
				db.update("user", values, "id=?", new String[]{"1"});
			}
		});
    	btnSelect = (Button) findViewById(R.id.btnSelect);
    	btnSelect.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//连接数据库辅助
				DataBaseHelper dbh = new DataBaseHelper(sqliteActivity.this,"test db name");
				//建立数据库
				SQLiteDatabase db = dbh.getWritableDatabase();
				//第一参数表名
				//第二参数查询的列名
				//第三参数where条件
				//第四参数where条件的值
				//第五参数分组条件
				//第六参数
				//第七参数排序
				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(name);
				}
			}
		});
    }
}



 

你可能感兴趣的:(Android,sqlite,android,数据库,button,string,null)