Android 存储学习之SQLite数据库的基本操作

本节学习SQLite数据库,SQLite是Android内置的一个简单的轻量级的数据库。关于SQLite的简介我这里不做过多的介绍。

既然我们要学习数据库的基本操纵,那就离不开,数据库的创建,增删改查等操作。

再学习数据库的相关操作前,我先给大家介绍一个类:SQLiteOpenHelper。SQLiteOpenHelper是Android系统为方便程序的开发引入的一个对数据库管理的工具类。可用于关于数据库的创建和版本更新。一般的用法是创建SQLiteOpenHelper的子类,并实现它的OnCreate方法和OnUpdate方法。


比如:下面是我定义的一个SQLiteOpenHelper的子类:

public class MySQLiteOpenHelper extends SQLiteOpenHelper {

	/*
	 * context: 上下文,通常是Activity
	 * name: 数据库的文件的名字
	 * factory:通常为默认是null
	 * version:数据库的版本号,通常从1开始,且必须大于0
	 */
	public MySQLiteOpenHelper(Context context, String name,
			CursorFactory factory, int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}

	//数据库创建时调用
	@Override
	public void onCreate(SQLiteDatabase arg0) {
		Log.i("MySQLiteOpenHelper", "onCreate-----被调用了!");
	}

	//数据库升级时调用
	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		Log.i("MySQLiteOpenHelper", "onUpgrade-------被调用了");
	}
}
我们必须要实现子类的构造方法。

既然我们大概认识了SQLiteOpenHelper,那我们就先开始行动。


1: 先创建一个关于天气的数据库

	private SQLiteDatabase db;
	public void create()
	{
		MySQLiteOpenHelper oh = new MySQLiteOpenHelper(getContext(), "weather.db", null, 1);
		
		/*如果数据库不存在,先创建数据库,再获取可读可写的数据库对象。如果数据库存在,直接打开。
		 * 一般情况下:getWritableDatabase和getReadableDatabase返回的都是可读可写的数据库
		 * 但是出错情况下:比如磁盘已满,则调用getReadableDatabase返回的只是只读 的数据库。具体见API说明
		 */
		db = oh.getWritableDatabase();
		//oh.getReadableDatabase();
	}
当第一次没有数据库时,就会调用MySQLiteOpenHelper中的create方法

//数据库创建时调用
	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.i("MySQLiteOpenHelper", "onCreate-----被调用了!");
		db.execSQL("create table weather(_id integer primary key autoincrement, city char(10), temp integer(3), pm integer(5))");
	}

当我测试后:

此时在包名下面的databasesx下数据库就已经存在了


打开后发现:

这时候数据库已经存在了,此时如果再次调用oh.getWritableDatabase();或者oh.getReadableDatabase();就直接打开数据库,不会再调用OnCreate方法了。


2:给数据库中插入几条数据

public void insert()
{
	//得到数据库对象
	MySQLiteOpenHelper oh = new MySQLiteOpenHelper(getContext(), "weather.db", null, 1);
	db = oh.getWritableDatabase();
	
	//插入4条记录
	db.execSQL("insert into weather(city, temp, pm) values(?, ?, ?)", new Object[]{"北京",37,280});
	db.execSQL("insert into weather(city, temp, pm) values(?, ?, ?)", new Object[]{"西安",35,200});
	db.execSQL("insert into weather(city, temp, pm) values(?, ?, ?)", new Object[]{"上海",37,100});
	db.execSQL("insert into weather(city, temp, pm) values(?, ?, ?)", new Object[]{"哈尔滨",30,80});
	
	//关闭数据库
	db.close();
}

导出数据库:

3:删除一条记录

public void delete()
{
	//得到数据库对象
	MySQLiteOpenHelper oh = new MySQLiteOpenHelper(getContext(), "weather.db", null, 1);
	db = oh.getWritableDatabase();
	
	//删除城市名为哈尔滨
	db.execSQL("delete from weather where city = ?", new Object[]{"哈尔滨"});
	
	//关闭数据库
	db.close();	
}
导出后显示为:

4:修改西安的温度到36度

public void update()
{
	//得到数据库对象
	MySQLiteOpenHelper oh = new MySQLiteOpenHelper(getContext(), "weather.db", null, 1);
	db = oh.getWritableDatabase();
	
	//修改西安的温度为36度
	db.execSQL("update weather set temp = ? where city = ?", new Object[]{36, "西安"});
	
	//关闭数据库
	db.close();		
}

导出显示为:

5:查询温度大于36度的城市

public void query()
{
	//得到数据库对象
	MySQLiteOpenHelper oh = new MySQLiteOpenHelper(getContext(), "weather.db", null, 1);
	db = oh.getWritableDatabase();
	
	//查询温度大于36的城市
	Cursor cursor = db.rawQuery("select * from weather where temp > ?",  new String[]{"36"});
	while(cursor.moveToNext())
	{
		String city = cursor.getString(cursor.getColumnIndex("city"));
		String temp = cursor.getString(cursor.getColumnIndex("temp"));
		String pm = cursor.getString(cursor.getColumnIndex("pm"));
		
		System.out.println(city + ";" + temp + ";" + pm);
	}
	
	//关闭数据库
	db.close();		
}

打印为:

关于数据库的增删改查就到这里。大家有木有发现直接写SQL语句是不是很麻烦,同时也可能会写错一个字母。所以Google给我们提供了一套API,可以很方便的操作数据库。关于使用API我们下节再说。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值