微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sqlite数据库中SQLiteOpenHelper类的onUpgrade方法

很多时候我们需要在项目进行中为项目添加一张表,今天终于搞清楚了。

代码如下

package com.zy.dao;

import android.R.integer;
import android.R.string;
import android.content.Context;
import android.database.sqlite.sqliteDatabase;
import android.database.sqlite.sqliteDatabase.CursorFactory;
import android.database.sqlite.sqliteOpenHelper;

public class DbHelper extends sqliteOpenHelper
{
	private static final int version = 2;
	private static final String dbnAME = "db_fa.db";

	public DbHelper(Context context)
	{
		super(context,dbnAME,null,version);
	}

	@Override
	public void onCreate(sqliteDatabase db)
	{
		//创建收入表
		db.execsql("create table if not exists tb_IncomeInfo  (income_id integer primary key autoincrement," +
				"income_price varchar(10),income_date varchar(20),income_account varchar(10)," +
				"income_payer varchar(10),income_category varchar(10),income_remark varchar(100))");
		
		//创建支出表
		db.execsql("create table if not exists tb_ConsumeInfo (consume_id integer primary key autoincrement," +
				"consume_price varchar(10),consume_time varchar(20),consume_account varchar(10)," +
				"consume_address varchar(20),consume_category varchar(10),consume_remark varchar(100))");

	}

	@Override
	public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion)
	{	
		onCreate(db);
	}

}

添加新表后要在onUpgrade执行onCreate方法,并且要把版本号更新(这个很重要!),这里我把版本号改为了。不然是无法添加进去表的!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐