1、数据库管理
继承自SQLiteOpenHelper类,实现数据库的创建、升级、增删查改方法
<pre name="code" class="java">/**
*
*/
package com.zhe.moodpulse.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* @author Administrator
*
*/
public class DBManager extends SQLiteOpenHelper {
private static final String DB_NAME = "moodpulse.db";
private static final int DB_VERSION = 1;
private static final String INIT_DB = UserTable.CREATE_TABLE;
private static final String UPGRADE_DB = "";
/**
* @param context
*/
public DBManager(Context context) {
super(context, DB_NAME, null, DB_VERSION);
// TODO Auto-generated constructor stub
}
/* (non-Javadoc)
* @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(INIT_DB);
ContentValues values = new ContentValues();
values.put(UserTable.USER_CODE, "user_" + System.currentTimeMillis());
values.put(UserTable.USER_PASSWORD, "");
values.put(UserTable.IS_VALID, 0);
db.insert(UserTable.TABLE_NAME,null,values);
}
public void insert(String tableName, ContentValues values) {
SQLiteDatabase db = getWritableDatabase();
db.insert(tableName, null, values);
db.close();
}
public void delete(String tableName, String whereClause, String[] whereArgs) {
SQLiteDatabase db = getWritableDatabase();
db.delete(tableName, whereClause, whereArgs);
db.close();
}
public Cursor query(String tableName, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {
SQLiteDatabase db = getWritableDatabase();
Cursor c = db.query(tableName, columns, selection, selectionArgs, groupBy, having, orderBy);
return c;
}
/* (non-Javadoc)
* @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, int, int)
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(UPGRADE_DB);
}
}
2、对于每一张表,创建一个对应的类,描述其表名、字段以及创建该表的sql语句
<pre name="code" class="java">package com.zhe.moodpulse.db;
public class UserTable {
public static final String TABLE_NAME = "user_table";
public static final String _ID = "_id";
public static final String USER_CODE = "user_code";
public static final String USER_NICK = "user_nick";
public static final String USER_PASSWORD = "user_password";
public static final String REGISTER_DATE = "register_date";
public static final String HEAD_IMG = "user_head";
public static final String ONLINE_STATUS = "online_status";
public static final String IS_VALID = "is_valid";
public static final String CREATE_TABLE = " create table " + TABLE_NAME + "("
+ _ID + " integer primary key autoincrement, "
+ USER_CODE + " text, "
+ USER_NICK + " text, "
+ USER_PASSWORD + " text, "
+ REGISTER_DATE + " smalldatetime, "
+ HEAD_IMG + " image, "
+ ONLINE_STATUS + " text, "
+ IS_VALID + " int) ";
}
3、在界面控件的响应中调用相应的方法
首先实例化一个数据库管理器对象:<pre name="code" class="java">DBManager helper = new DBManager(getApplicationContext());
4、一些小技巧
</pre><pre name="code" class="java">1)获取当前时间中的年月日时分秒的信息
<pre name="code" class="java">package com.zhe.utils;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class TimeUtils {
private static SimpleDateFormat allFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss",Locale.CHINA);
private static SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy",Locale.CHINA);
private static SimpleDateFormat monthFormat = new SimpleDateFormat("MM",Locale.CHINA);
private static SimpleDateFormat dayFormat = new SimpleDateFormat("dd",Locale.CHINA);
private static SimpleDateFormat hourFormat = new SimpleDateFormat("hh",Locale.CHINA);
private static SimpleDateFormat minFormat = new SimpleDateFormat("mm",Locale.CHINA);
public static String getAll(Date now) { return allFormat.format(now); }
public static String getYear(Date now) { return yearFormat.format(now); }
public static String getMonth(Date now) { return monthFormat.format(now); }
public static String getDate(Date now) { return dayFormat.format(now); }
public static String getHour(Date now) { return hourFormat.format(now); }
public static String getMinute(Date now){ return minFormat.format(now); }
}
2)在app中保存用户信息,在任意位置能够调用
<pre name="code" class="java"><span style="white-space:pre"> private SharedPreferences sp; </span>
<span style="white-space:pre"> </span>private void initialUser() {
sp = this.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
String userName = sp.getString("user_name", "");
if(userName == "") checkDefaultUser();
}
private void checkDefaultUser() {
Cursor res = helper.query(UserTable.TABLE_NAME, null, null, null, null, null, null);
res.moveToNext();
String userName = res.getString(1);
Editor editor = sp.edit();
editor.putString("user_name", userName);
editor.putString("password","");
editor.commit();
}
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span>