我正在开发一个移动应用程序,它将涉及在用户之间共享的实时数据。问题是我真的不知道存储和管理数据的最佳方式是什么,比如我应该使用什么数据库,是MySQL吗?我是一个初学者,所以一个简单的答案就足够了。
编辑:我开始使用android,但我将以IOS为目标。我看到许多答案谈论本地存储解决方案,但我正在寻找访问网络的东西。我想要做的一个很好的例子是谷歌地图。他们如何管理他们的数据?
在iOS的情况下,您可以使用SQLite,Coredata,平面文件系统,JSON文件,.plist文件,NSArchiver等。
如果数据量巨大且结构化记录使用SQLite。如果数据很小,就像简单地保存开关按钮的状态那么使用NSUserdefault。如果数据小且机密且需要加密,请使用密钥链存储。
如果数据庞大且结构化,并且您希望让Wrapper处理带下划线的Sqlite使用coredata
甚至android支持相同的SQLite。
因此,这取决于您的要求,您希望使用哪种存储功能。
注意: - 仅适用于客户端存储。
如果您使用Android或IOS SQLite数据库是好的。因为它很容易存储和管理数据。如果您想将数据存储在内部存储中,您只需使用文件系统。您也可以存储大量的数据。数据的。
编写一个扩展SQLiteOpenHelper的类
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "MyDBName.db";
private static final int DATATBASE_VERSION = 2;
// int count;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATATBASE_VERSION);
this.getWritableDatabase();
}
// Create the tables inside the oncreate methode
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS table1 " + "(id integer primary key,name text)");
db.execSQL("CREATE TABLE IF NOT EXISTS table2 " + "(id integer primary key,name text,filed1 text,filed2 text)");
}
// write methodes for inserting intoo table
public boolean insertTable1(int id, String name) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("id", id);
contentValues.put("name", name);
db.insert("work_items", null, contentValues);
return true;
}
// methode for deleting all values from table1
public void deleteAllWorkers() {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("delete from table1");
db.execSQL("vacuum");
db.close();
}
}
每当需要保存一些数据时,创建一个DBHelper对象并使用相应的方法。例如:
DBHelper dbHelper=new DBHelper(activity);
dbHelper.insertTable1(id,name);
希望这会帮助你。