android sqlite 实例

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

 

package hhh.com;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class bd extends SQLiteOpenHelper {


	public static String DB_NAME = "S.db";
	public static String ID = "_id";
	public static String T_NAME = "stu_list";
	public static int VERSION = 1;
	public static String NAME = "s_name";
	public static String IDCODE = "s_id";
	public static String SEX = "s_sex";


	public bd(Context context) {
		super(context, DB_NAME, null, VERSION);
	}


	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		String sql = "create table " + T_NAME + " ( " + ID
				+ " integer primary key ," + IDCODE + " integer," + NAME
				+ " text," + SEX + " text)";
		db.execSQL(sql);
	}


	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL("drop table if exists" + T_NAME);
	}


	public long insert(int idcode, String name, String sex) {
		SQLiteDatabase db = getWritableDatabase();
		long row = 0;
		ContentValues cv = new ContentValues();
		cv.put(IDCODE, idcode);
		cv.put(NAME, name);
		cv.put(SEX, sex);
		row = db.insert(T_NAME, null, cv);
		return row;
	}
	public void delete(int id) {
		SQLiteDatabase db = getWritableDatabase();
		db.execSQL("delete from " + T_NAME + " where _id =" + id);
	}

	public Cursor qurey(String name) {
		SQLiteDatabase db = getReadableDatabase();
		String sql = "select * from " + T_NAME + " where s_name = ?";
		Cursor cursor = db.rawQuery(sql, new String[] { name });
		return cursor;
	}

	public int update(int _id, int id, String name, String sex) {
		SQLiteDatabase db = getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put(IDCODE, id);
		cv.put(NAME, name);
		cv.put(SEX, sex);
		int flag = db.update(T_NAME, cv, ID + " = ? ",
				new String[] { Integer.toString(_id) });
		return flag;
		// db.execSQL("update " + T_NAME
		// + " set s_id=? , s_name=? , s_sex=? where _id=?", new Object[] {
		// id, name, sex, _id });
	}
}


本文原创,转载请注明出处,谢谢合作!

转载于:https://my.oschina.net/zjbpku/blog/81034

你可能感兴趣的:(android sqlite 实例)