我想实现一个系统,我在android设备中有一个本地数据库,这个本地数据库将共享数据库到全局数据库,全局数据库将通过添加,删除或更新条目来响应本地数据库某些信息(控制信息)的本地数据库。更新需要非常快。 任何人都可以与我分享最有效和最简单的方法来实现这个系统?
谢谢
答案 0 :(得分:0)
不确定是否有为其构建的功能但是如果它需要很快我假设你不想在服务器端编写所有脚本来管理你的sql语句。我会使用windows azure移动服务。看看它:)
答案 1 :(得分:0)
public class DBHelper {
private static final String DATABASE_NAME = "APP_DB.db";
private static final int DATABASE_VERSION = 1;
public static String Table_CATEGORY = "CREATE TABLE IF NOT EXISTS [Categories] ([USERID] TEXT, [catid] TEXT, [catname] TEXT, [parentid]TEXT)";
public static String Table_ADDRESS = "CREATE TABLE IF NOT EXISTS [Address] ([USERID] TEXT, [address_name] TEXT, [address_phone] TEXT,[address_pincode] TEXT, [address_details] TEXT, [address_city] TEXT, [address_landmark] TEXT, [address_tag] TEXT)";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase myDataBase;
public DBHelper(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(Table_CATEGORY);
db.execSQL(Table_ADDRESS);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS Categories");
db.execSQL("DROP TABLE IF EXISTS Address");
onCreate(db);
}
}
// ---opens the database---
public DBHelper open() throws SQLException {
myDataBase = DBHelper.getWritableDatabase();
return this;
}
// ---closes the database---
public void close() {
DBHelper.close();
}
public long insertCategories(DBCaModel categories, String userid) {
long rowNo = 0;
ContentValues initialValues = new ContentValues();
initialValues.put(DatabaseStaticField.KEY_User_ID, userid);
initialValues.put(DatabaseStaticField.KEY_CATEGORY_ID,
categories.getCatId());
initialValues.put(DatabaseStaticField.KEY_CATEGORY_NAME,
categories.getCat_name());
initialValues.put(DatabaseStaticField.KEY_CATEGORY_PARENTID,
categories.getCatParent_id());
rowNo = myDataBase.insert(DatabaseStaticField.DATABASE_TABLE_CATEGORY,
null, initialValues);
return rowNo;
}
public void deleteCategories() {
myDataBase.delete(DatabaseStaticField.DATABASE_TABLE_CATEGORY, null,
null);
}
public List<String> getCategories(String sql, String columName) {
Cursor c = myDataBase.rawQuery(sql, null);
List<String> mVillageList = new ArrayList<String>();
if (c != null && c.getCount() > 0) {
c.moveToFirst();
do {
mVillageList.add(c.getString(c.getColumnIndex(columName)));
} while (c.moveToNext());
}
c.close();
return mVillageList;
}
public String getCategoriesName(String sql) {
Cursor c = myDataBase.rawQuery(sql, null);
List<String> mVillageList = new ArrayList<String>();
if (c != null && c.getCount() > 0) {
c.moveToFirst();
do {
mVillageList.add(c.getString(c.getColumnIndex(DatabaseStaticField.KEY_CATEGORY_NAME)));
} while (c.moveToNext());
}
c.close();
return mVillageList.get(0);
}
public ArrayList<Categories> getCategories(String sql) {
Cursor c = myDataBase.rawQuery(sql, null);
ArrayList<Categories> categoriesList = new ArrayList<Categories>();
if (c != null && c.getCount() > 0) {
c.moveToFirst();
do {
Categories model=new Categories();
model.setCat_id(c.getString(c.getColumnIndex(DatabaseStaticField.KEY_CATEGORY_ID)));
model.setCat_name(c.getString(c
.getColumnIndex(DatabaseStaticField.KEY_CATEGORY_NAME)));
model.setParent_id(c.getString(c
.getColumnIndex(DatabaseStaticField.KEY_CATEGORY_PARENTID)));
categoriesList.add(model);
} while (c.moveToNext());
}
c.close();
return categoriesList;
}
}
答案 2 :(得分:0)
public class BillnoxCryptography {
private static final String ALGORITHM = "AES";
public static String encrypt(String seed, String cleartext) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException,
IllegalBlockSizeException, BadPaddingException {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
}
public static String decrypt(String seed, String encrypted) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException,
IllegalBlockSizeException, BadPaddingException {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] enc = toByte(encrypted);
byte[] result = decrypt(rawKey, enc);
return new String(result);
}
private static byte[] getRawKey(byte[] seed) {
seed = Arrays.copyOf(seed, 16);
SecretKey key = new SecretKeySpec(seed, ALGORITHM);
byte[] raw = key.getEncoded();
return raw;
}
// Since the credentials are already secured through shared prefs, we're
// using this as a lightweight solution for obfuscation. Fixing SecureRandom
// to provide cryptographically strong values is outside the scope of this
// application. See
// http://android-developers.blogspot.com/2013/08/some-securerandom-thoughts.html
@SuppressLint("TrulyRandom")
private static byte[] encrypt(byte[] raw, byte[] clear) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
SecretKeySpec skeySpec = new SecretKeySpec(raw, ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
public static byte[] toByte(String hexString) {
int len = hexString.length() / 2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++) {
result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue();
}
return result;
}
public static String toHex(byte[] buf) {
if (buf == null) {
return "";
}
StringBuffer result = new StringBuffer(2 * buf.length);
for (int i = 0; i < buf.length; i++) {
appendHex(result, buf[i]);
}
return result.toString();
}
private final static String HEX = "0123456789ABCDEF";
private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
}
}
public class BillNoxApp extends Application {
private Activity localActivity;
public static Context mcontext;
private DBHelper db = null;
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
public void onCreate() {
super.onCreate();
this.mcontext = this.getApplicationContext();
try {
BillNoxApp.mcontext =this;
db = new DBHelper(this);
db.open();
} catch (Exception e) {
Log.i("Exception", e + "");
}
}
public static Context getContext() {
return mcontext;
}
@Override
public void onLowMemory() {
super.onLowMemory();
}
@Override
public void onTerminate() {
db.close();
super.onTerminate();
}
public DBHelper getDatabase() {
return db;
}
public void setLocalActivity(Activity localActivity) {
this.localActivity = localActivity;
}
public Activity getLocalActivity() {
return this.localActivity;
}
}