现在的位置: 首页 > 综合 > 正文

Android平台SQLite简单应用

2017年09月23日 ⁄ 综合 ⁄ 共 3519字 ⁄ 字号 评论关闭

<p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 24px;">Android数据存储共有四种方式::SharePreference、SQLite、Content Provider和File,他们都在“data/data/程序包名”目录下。</p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 24px;">SharePreference: 以xml文件的形式保存数据,通常用户保存简单的数据。</p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 24px;">SQLite:是Android平台上的轻量级数据库,可以采用sql语句进行访问,也可以采用相应的api已经访问。</p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 24px;"> FILE:一般用来存储大量数据。</p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 24px;">ContentProvider:用来实现数据共有。</p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 24px;">  <span style="margin: 0px; padding: 0px;">SharedPreference: </span>除SQLite数据库外,另一种常用的数据存储方式,其本质就是一个xml文件,常用于存储较简单的参数设置。</p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 24px;"><span style="margin: 0px; padding: 0px;"> 此文主要讲述的的是SQLite。</span></p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 24px;">首先写一个继承SQLiteOpenHelper的类</p>
<pre name="code" class="java">public class DBHelper extends SQLiteOpenHelper {
	private static final String DB_NAME = "hht.db";
	private static final String TBL_NAME = "hhtTAB";
	private static final String CREATE_TBL = " create table "
			+ "hhtTAB(_id integer primary key autoincrement,username text,gender text, age text,registertime datetime) ";
//创建表的aql语句
	private SQLiteDatabase db;

	DBHelper(Context c) {
		super(c, DB_NAME, null, 2);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		this.db = db;
		db.execSQL(CREATE_TBL);
	}

	public void insert(ContentValues values) {//插入语句,value来自mainactivity
		SQLiteDatabase db = getWritableDatabase();
		db.insert(TBL_NAME, null, values);
		db.close();
	}

	public Cursor query(String name,String age) {//查询语句
		SQLiteDatabase db = getWritableDatabase();
		String message[] = { name,age };
		Cursor c = db.query(TBL_NAME, null, "name=? and age=?", message,
				null, null, "age asc",
				"5,5");// select form hhtTAB where name=name and age=age orderby age asc limit 5.5
		return c;
	}

	public void del(int id) {//删除语句
		if (db == null)
			db = getWritableDatabase();
		db.delete(TBL_NAME, "_id=?", new String[] { String.valueOf(id) });
	}

	public void close() {
		if (db != null)
			db.close();
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {//更新语句
	}
}

以insert为例,当需要使用的时候:


DBHelper helper=new DEHelper(Context);
ContentValues values = new ContentValues();
<span style="white-space:pre">				</span>values.put("username", username);
<span style="white-space:pre">				</span>values.put("gender", gender);
<span style="white-space:pre">				</span>values.put("age", ge);
<span style="white-space:pre">				</span>SimpleDateFormat df = new SimpleDateFormat(
<span style="white-space:pre">						</span>"yyyy-MM-dd HH:mm:ss");
<span style="white-space:pre">				</span>values.put("registertime", df.format(new Date()));
<span style="white-space:pre">				</span>DBHelper helper = new DBHelper(getApplicationContext());
<span style="white-space:pre">				</span>helper.insert(values);

抱歉!评论已关闭.