android极简原创系列:三层结构和.net

安卓的数据库操作最简单三层

首先是数据库创建:

package com.postdep.dal;

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 = "saccount.db";
	public DBOpenHelper(Context context) {
		super(context, DBNAME, null, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {// 创建数据库
		db.execSQL("CREATE TABLE [users] ([id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,[username] VARCHAR(50) NOT NULL,[password] VARCHAR(50) NOT NULL)");// 创建users表
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("drop table if exists users");
	}
}
我上面创建了一个叫saccount.db的数据库和一张叫users的表,表里面id是自增主键,两个varchar的字段

namespace是postdep.dal

DAL+BLL

package com.postdep.dal;

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

import com.postdep.model.User;

public class DBUser {
	
	private DBOpenHelper helper;
	private SQLiteDatabase db;

	public DBUser(Context context) {
		helper = new DBOpenHelper(context);
		db = helper.getWritableDatabase();
	}

	// 添加操作
	public void insert(User user) {

		ContentValues values = new ContentValues();
		values.put("username", user.getUsername());
		values.put("password", user.getPassword());
		db.insert("users", null, values);
	}

	// 修改操作
	public void update(User user, int id) {

		db.execSQL("update users set username=?,password=? where id=" + id,
				new Object[] { user.getUsername(), user.getPassword()});
	}

	// 查询操作
	public User query(int id) {
		db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
		User user = new User();
		Cursor cursor = db.query("users",
				new String[] { "username", "password" }, "id =" + id, null,
				null, null, null);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			user.setUsername(cursor.getString(cursor.getColumnIndex("username")));
			user.setPassword(cursor.getString(cursor.getColumnIndex("password")));
			return user;
		}
		cursor.close();
		return null;
	}

}

上面这个一看都懂我就不多解释了

MODEL

package com.postdep.model;

public class User// 密码数据表实体类
{
	private int id;
	private String username;
	private String password;// 定义字符串,表示用户密码

	public User() {// 默认构造函数
		super();
	}

	public User(String username, String password) {
		this.username = username;
		this.password = password;
	}

	public int getId() {
		return id;

	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}

做好以上三个操作后在系统里面调用的例子如下:

插入数据
User user = new User("abc","123654");
DBUser helper = new DBUser(MainActivity.this);		
helper.insert(user);

查询数据
final TextView tv1 = (TextView) findViewById(R.id.textView1);
final TextView tv2 = (TextView) findViewById(R.id.textView2);
DBUser helper = new DBUser(GrzxActivity.this);
User user = helper.query(1);
tv1.setText(user.getUsername());
tv2.setText(user.getPassword());

你可能感兴趣的:(android,sqlite,asp.net,三层)