android中操纵sqlite数据库

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

public class DBOpenHelper extends SQLiteOpenHelper {
	private static final int VERSION = 1;// 定义数据库版本号
	private static final String DBNAME = "account.db";// 定义数据库名

	public DBOpenHelper(Context context){// 定义构造函数
	
		super(context, DBNAME, null, VERSION);// 重写基类的构造函数
	}
///public abstract void onCreate(SQLiteDatabase db)Called when the database is created for the first time. 
	@Override
	public void onCreate(SQLiteDatabase db){// 创建数据库
	
		db.execSQL("create table tb_outaccount (_id integer primary key,money decimal,time varchar(10),"
				+ "type varchar(10),address varchar(100),mark varchar(200))");// 创建支出信息表
		db.execSQL("create table tb_inaccount (_id integer primary key,money decimal,time varchar(10),"
				+ "type varchar(10),handler varchar(100),mark varchar(200))");// 创建收入信息表
		db.execSQL("create table tb_pwd (password varchar(20))");// 创建密码表
		db.execSQL("create table tb_flag (_id integer primary key,flag varchar(200))");// 创建便签信息表
	}
///public abstract void onUpgrade(SQLiteDatabase db,
//    int oldVersion,
//    int newVersion)Called when the database needs to be upgraded. 
//	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)// 覆写基类的onUpgrade方法,以便数据库版本更新
	{
	}
	//////////
}
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.xiaoke.accountsoft.model.*;

public class PwdDAO {
	private DBOpenHelper helper;// 创建DBOpenHelper对象
	private SQLiteDatabase db;// 创建SQLiteDatabase对象

	public PwdDAO(Context context)// 定义构造函数
	{
		helper = new DBOpenHelper(context);// 初始化DBOpenHelper对象
	}

	/**
	 * 添加密码信息
	 * 
	 * @param tb_pwd
	 */
	public void add(Tb_pwd tb_pwd) {
		/// SQLiteDatabase getWritableDatabase() 
      //  Create and/or open a database that will be used for reading and writing. 
		db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
		// 执行添加密码操作
		db.execSQL("insert into tb_pwd (password) values (?)",
				new Object[] { tb_pwd.getPassword() });
	}

	/**
	 * 设置密码信息
	 * 
	 * @param tb_pwd
	 */
	public void update(Tb_pwd tb_pwd) {
		db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
		// 执行修改密码操作
		db.execSQL("update tb_pwd set password = ?",
				new Object[] { tb_pwd.getPassword() });
	}

	/**
	 * 查找密码信息
	 * 
	 * @return
	 */
	public Tb_pwd find() {
		db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
		// 查找密码并存储到Cursor类中
//		public Cursor rawQuery(String sql,
//                String[] selectionArgs)Runs the provided SQL and returns a Cursor over the result set. 
		Cursor cursor = db.rawQuery("select password from tb_pwd", null);
		if (cursor.moveToNext())// 遍历查找到的密码信息
		{
			// 将密码存储到Tb_pwd类中
		//	String getString(int columnIndex)Returns the value of the requested column as a String. 
		//	int getColumnIndex(String columnName)Returns the zero-based index for the given column name, or -1 if the column doesn't exist.
			return new Tb_pwd(cursor.getString(cursor
					.getColumnIndex("password")));
		}
		return null;// 如果没有信息,则返回null
	}

	public long getCount() {
		db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
		Cursor cursor = db.rawQuery("select count(password) from tb_pwd", null);// 获取密码信息的记录数
		if (cursor.moveToNext())// 判断Cursor中是否有数据
		{
		//	long getLong(int columnIndex)Returns the value of the requested column as a long. 
			return cursor.getLong(0);// 返回总记录数
		}
		return 0;// 如果没有数据,则返回0
	}// // getCount()
}///PwdDAO


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