我总是有单独的数据库包名称。我编写了一个单独的数据库类,我在每个项目中都使用它。我只更改数据库名、表名、列名。以下是示例类:
package com.mobisys.android.contactwidget.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class ContactDatabase {
public static final String DATABASE_NAME = "contact.db";
public static final int DATABASE_VERSION = 1;
public static final String CONTACT_TABLE_NAME = "contact";
public static final String KEY_ID = "_id";
public static final String KEY_ROW = "row";
public static final String KEY_COL = "col";
public static final String KEY_APP_WIDGET_ID = "app_widget_id";
public static final String KEY_CONTACT_IMAGE = "image";
public static final String KEY_CONTACT_NAME = "name";
public static final String KEY_CONTACT_NUMBER = "number";
public static final String KEY_CONTACT_EMAIL = "email";
private final OpenHelper contactHelper;
public ContactDatabase(Context context){
contactHelper=new OpenHelper(context);
}
public long insert(String table, ContentValues values){
return contactHelper.getWritableDatabase().insert(table, null, values);
}
public long delete(String table, String where, String[] whereArgs){
return contactHelper.getWritableDatabase().delete(table, where, whereArgs);
}
public int update(String table, ContentValues values, String whereClause, String[] whereArgs){
return contactHelper.getWritableDatabase().update(table, values, whereClause, whereArgs);
}
public long countRows(String query){
return DatabaseUtils.longForQuery(contactHelper.getReadableDatabase(), query, null);
}
public Cursor query(String table,String[] columns, String selection,String[] selectionArgs,String groupBy,String having,String orderBy){
return contactHelper.getReadableDatabase().query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
}
public void close(){
contactHelper.close();
}
private static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+
CONTACT_TABLE_NAME+
" ("+ KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
KEY_ROW+" INT, "+
KEY_COL+" INT, "+
KEY_APP_WIDGET_ID+" INT, "+
KEY_CONTACT_IMAGE+" BLOB, "+
KEY_CONTACT_NAME+" TEXT, "+
KEY_CONTACT_NUMBER+" TEXT, "+
KEY_CONTACT_EMAIL+" TEXT"+")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
String alter_query1="alter table "+CONTACT_TABLE_NAME+" RENAME TO temp1;";
db.execSQL(alter_query1);
onCreate(db);
String insert_query1="insert into "+CONTACT_TABLE_NAME+" select * from temp1;";
db.execSQL(insert_query1);
String delete_query1="DROP TABLE temp1;";
db.execSQL(delete_query1);
}
}
}
我还创建了一个 HelperDatabase 类,其中包含所有与数据库相关的静态方法。示例类:
package com.mobisys.android.contactwidget.database;
import android.content.ContentValues;
import android.database.Cursor;
import com.mobisys.android.contactwidget.data.CONTACT;
public class HelperDatabase {
public static long inserContact(CONTACT contact, ContactDatabase database){
ContentValues values=new ContentValues();
values.put(ContactDatabase.KEY_APP_WIDGET_ID, contact.app_widget_id);
values.put(ContactDatabase.KEY_ROW, contact.row);
values.put(ContactDatabase.KEY_COL, contact.col);
values.put(ContactDatabase.KEY_CONTACT_NAME, contact.name);
values.put(ContactDatabase.KEY_CONTACT_NUMBER, contact.cotact_number);
values.put(ContactDatabase.KEY_CONTACT_EMAIL, contact.email);
values.put(ContactDatabase.KEY_CONTACT_IMAGE, contact.image);
long id=database.insert(ContactDatabase.CONTACT_TABLE_NAME, values);
return id;
}
public static void updateMyContactInfo(ContactDatabase contactdb, int _id, String number){
ContentValues values=new ContentValues();
values.put(ContactDatabase.KEY_CONTACT_NUMBER, number);
contactdb.update(ContactDatabase.CONTACT_TABLE_NAME, values, "_id"+"="+_id, null);
}
public static Cursor getContacts(ContactDatabase contactdb, int sort){
if(sort==1)
return contactdb.query(ContactDatabase.CONTACT_TABLE_NAME, null, null, null, null, null, ContactDatabase.KEY_CONTACT_NAME);
else if(sort==2)
return contactdb.query(ContactDatabase.CONTACT_TABLE_NAME, null, null, null, null, null, ContactDatabase.KEY_CONTACT_EMAIL);
else if(sort==3)
return contactdb.query(ContactDatabase.CONTACT_TABLE_NAME, null, null, null, null, null, ContactDatabase.KEY_CONTACT_NUMBER);
return contactdb.query(ContactDatabase.CONTACT_TABLE_NAME, null, null, null, null, null, null);
}
public static boolean isContactExist(ContactDatabase contactdb, String number){
return contactdb.countRows("SELECT COUNT(*) FROM "+ContactDatabase.CONTACT_TABLE_NAME+" WHERE"+ ContactDatabase.KEY_CONTACT_NUMBER + "='"+number+"'")>0;
}
}
所以,基本上,我可以为项目中的每个数据库设置一个类和一个 HelperDatabase 类,它执行所有插入、更新、检索和删除功能。
如果我的项目严重依赖数据库,那么最好为您的数据库类设置一个静态对象,该对象将在您的主要活动开始时打开,并在您的主要活动将销毁时关闭。
以下是代码示例:
public class HomeActivity extends Activity implements View.OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
HelperDatabase.contactdb=new ContactDatabase(this);
startApplication();
}
@Override
public void onDestroy(){
HelperDatabase.contactdb.close();
super.onDestroy();
}
}
希望,这将对您有所帮助。