Android提供以下四种存储方式:
private String setCityName(String _cityName){ City.getCity().setCityName(_cityName); Context ctx =MainActivity.this; SharedPreferences sp =ctx.getSharedPreferences("CITY", MODE_PRIVATE); Editor editor=sp.edit(); editor.putString("CityName", City.getCity().getCityName()); editor.commit(); return City.getCity().getCityName(); }
private String getCityName(){ String cityName = City.getCity().getCityName(); if(cityName==null ||cityName==""){ Context ctx =MainActivity.this; SharedPreferences sp =ctx.getSharedPreferences("CITY", MODE_PRIVATE); City.getCity().setCityName(sp.getString("CityName", "广州")); } return City.getCity().getCityName(); }
SQLite是一个轻量级关系型数据库,既然是关系型数据库,那操作起来其实跟mysql、sql server差不多的。需要注意的一点是,SQLite只有NULL、INTEGER、REAL(浮点数)、TEXT(字符串)和BLOB(大数据)五种类型,不存在BOOLEAN和DATE类型。
String sql="create table t_user (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL,password TEXT NOT NULL)";
db.execSQL(sql);
ContentValues values=new ContentValues(); values.put("name", "liangjh"); values.put("password", "123456"); db.insert("t_user", "id", values);
// 方式1 直接将条件写入到条件里面(个人觉得容易被注入,但其实数据都在客户端,没啥安全性可言) db.delete("t_user", "id=1", null); // 方式2 条件分开写,感觉比较安全 db.delete("t_user", "name=? and password =?", new String[]{"weiyg","112233"});
// 使用rawQuery // Cursor c = db.rawQuery("select * from t_user", null); // db.rawQuery("select * from t_user where id=1", null); // db.rawQuery("select * from t_user where id=?", new String[]{"1"}); // 使用query() Cursor c = db.query("t_user", new String[]{"id","name"}, "name=?", new String[]{"weiyg"}, null, null, null); c.moveToFirst(); while(!c.isAfterLast()){ String msg=""; for(int i=0,j=c.getColumnCount();i<j;i++){ msg+="--"+c.getString(i); } Log.v("SQLite", "data:"+msg); c.moveToNext(); }
ContentValues values=new ContentValues(); values.put("password", "111111"); // 方式1 条件写在字符串内 db.update("t_user", values, "id=1", null); // 方式2 条件和字符串分开 db.update("t_user", values, "name=? or password=?",new String[]{"weiyg","123456"});
无论何时,打开的数据库,记得关闭。
db.close()
另外使用beginTransaction()和endTransaction()可以设置事务。
1 public class MyProvider extends ContentProvider { 2 3 @Override 4 public int delete(Uri uri, String selection, String[] selectionArgs) { 5 // TODO Auto-generated method stub 6 return 0; 7 } 8 9 @Override 10 public String getType(Uri uri) { 11 // TODO Auto-generated method stub 12 return null; 13 } 14 15 @Override 16 public Uri insert(Uri uri, ContentValues values) { 17 // TODO Auto-generated method stub 18 return null; 19 } 20 21 @Override 22 public boolean onCreate() { 23 // 新建个数据库并插入一条数据 24 SQLiteDatabase db=this.getContext().openOrCreateDatabase("test_db2.db", Context.MODE_PRIVATE, null); 25 db.execSQL("CREATE TABLE t_user (id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT NOT NULL)"); 26 ContentValues values=new ContentValues(); 27 values.put("name", "liangjh2"); 28 db.insert("t_user", "id", values); 29 db.close(); 30 return false; 31 } 32 33 @Override 34 public Cursor query(Uri uri, String[] projection, String selection, 35 String[] selectionArgs, String sortOrder) { 36 // 获取数据 37 SQLiteDatabase db=this.getContext().openOrCreateDatabase("test_db2.db", Context.MODE_PRIVATE, null); 38 Cursor c = db.query("t_user", null, null, null, null, null, null); 39 db.close(); 40 return c; 41 } 42 43 @Override 44 public int update(Uri uri, ContentValues values, String selection, 45 String[] selectionArgs) { 46 // TODO Auto-generated method stub 47 return 0; 48 } 49 50 }
<application ...> ... <provider android:name=".MyProvider" android:authorities="com.example.androidtestdemo" android:exported="true"/> </application>
Context ctx=MainActivity.this; ContentResolver resolver =ctx.getContentResolver(); Uri uri=Uri.parse("content://com.example.androidtestdemo"); Cursor c = resolver.query(uri, null, null, null, null); c.moveToFirst(); while(!c.isAfterLast()){ for(int i=0,j=c.getColumnCount();i<j;i++){ Log.v("Android2",""+c.getString(i)); } c.moveToNext(); }