SQLite数据库操作

如果要使用SQLite数据库进行数据处理,首先需要建立一个SQLiteOpenHelper的子类,等同于JDBC中编写的DataBaseConnection类。

在该类中,需要进行数据库和数据表的初始化。

建立一个连接类,来完成数据库test.db以及库中的表news的建立

public class DataBaseConnection extends SQLiteOpenHelper {

	// 初始化固定的值
	private static final int DBVERSION = 1;
	private static final String DBNAME = "test.db";

	// 为了方便使用,这里自己完成一个构造方法
	public DataBaseConnection(Context ctx) {
		super(ctx, DBNAME, null, DBVERSION);
	}

	public DataBaseConnection(Context context, String name,
			CursorFactory factory, int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase conn) {
		// 当第一次运行程序时, 需要在这里直接建立出数据库中的表
		String sql = "CREATE TABLE news (" +
				"id			integer    primary key," +
				"title		text	   ," +
				"content	text	   ," +
				"pub_date	text	   ," +
				"type	    integer	   " +
				")" ;
		// 执行这条sql
		conn.execSQL(sql);
	}	

	@Override
	public void onUpgrade(SQLiteDatabase conn, int oldVersion, int newVersion) {
		String sql = null;
		if (oldVersion == 1) {
			sql = "DROP TABLE news";
			conn.execSQL(sql);
		}
		sql = "CREATE TABLE news (" +
				"id			integer    primary key," +
				"title		text	   ," +
				"content	text	   ," +
				"pub_date	text	   ," +
				"type	    integer    ," +
				"photo		text	   " +
				")" ;
		conn.execSQL(sql);
	}

}

使用时,只需要建立这个类,并取得了一个数据库连接,就会自动执行onCreate操作(仅限于第一次)

完成数据库的简单操作

public class NewsDAOUtils {

	public static void insertData(SQLiteDatabase db, Map<String, Object> map) {
		String sql = "INSERT INTO news (title,content,pub_date,type) VALUES (?,?,?,?)";
		// 调用添加操作
		db.execSQL(sql, new Object[] { map.get("title"), map.get("content"),
				map.get("pubDate"), map.get("type") });
	}

	public static List<Map<String, Object>> listData(SQLiteDatabase db) {
		// 调用查询,接收一个返回结果, Cursor 类似 JDBC中的ResultSet
		String sql = "SELECT id,title,content,pub_date,type FROM news";
		Cursor c = db.rawQuery(sql, null);
		// 循环将数据加入到List中
		List<Map<String, Object>> allValues = new ArrayList<Map<String, Object>>();

		// 移动查询的游标,指向第一条数据
		c.moveToFirst();
		// 如果没有到最后,就继续循环
		while (!c.isAfterLast()) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", c.getInt(0));
			map.put("title", c.getString(1));
			map.put("content", c.getString(2));
			map.put("pubDate", c.getString(3));
			map.put("type", c.getInt(4));

			allValues.add(map);
			// 手工移动游标
			c.moveToNext();
		}
		c.close();
		
		return allValues;
	}
}
测试时注意,日期类型最好自己进行格式化转换,保存成String来存到数据库里,否则取得时格式会有问题。

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		DataBaseConnection dbc = new DataBaseConnection(this);
		// 测试添加操作
		Map<String, Object> map = new HashMap<String, Object>();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		map.put("title", "新闻数据标题3");
		map.put("content", "新闻数据内容content3");
		map.put("pubDate", sf.format(new Date()));
		map.put("type", 3);
		NewsDAOUtils.insertData(dbc.getWritableDatabase(), map);

		// 测试列表操作
		System.out.println(NewsDAOUtils.listData(dbc.getReadableDatabase()));

	}

}






















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值