文件写:
FileOutputStream out = null; BufferedWriter writer = null; try { out = openFileOutput("data", Context.MODE_PRIVATE); writer = new BufferedWriter(new OutputStreamWriter(out)); writer.write(inputText); } catch (IOException e) { e.printStackTrace(); } finally { try { if (writer != null) writer.close(); } catch (IOException e) { e.printStackTrace(); } }
文件读:
FileInputStream in = null; BufferedReader reader = null; StringBuilder content = new StringBuilder(); try { in = openFileInput("data"); reader = new BufferedReader(new InputStreamReader(in)); String line = ""; while ((line = reader.readLine()) != null) { content.append(line); } } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return content.toString();
SharedPreference:
SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit(); editor.putString("name","bob"); editor.apply();
SharedPreferences pref = getSharedPreferences("data",MODE_PRIVATE); String name = pref.getString("name",""); Log.d("MainAcitivity","name is " + name);
SQLite:
public class MyDatabaseHelper extends SQLiteOpenHelper { public static final String CREATE_BOOK = "create table Book(id integer primary key autoincrement,author text,price real,pages integer,name text)"; private Context mContext; public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,int version){ super(context,name,factory,version); mContext = context; } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_BOOK); } @Override public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){ } }建表:
private MyDatabaseHelper dbHelper; dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,1); dbHelper.getWritableDatabase();
添加一个新表:
public class MyDatabaseHelper extends SQLiteOpenHelper { public static final String CREATE_BOOK = "create table Book(id integer primary key autoincrement,author text,price real,pages integer,name text)"; public static final String CREATE_CATEGORY = "create table Category(id integer primary key autoincrement,catogory_name text,category_code integer)"; private Context mContext; public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,int version){ super(context,name,factory,version); mContext = context; } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_BOOK); db.execSQL(CREATE_CATEGORY); } @Override public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){ db.execSQL("drop table if exists Book"); db.execSQL("drop table if exists Category"); onCreate(db); } }
dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,2);添加数据:
SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("name","bob"); values.put("author","bob author"); values.put("pages",458); values.put("price",666); db.insert("Book",null,values);更新:
SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("price",233); db.update("Book",values,"name=?",new String[]{"bob"});
删除:
QLiteDatabase db = dbHelper.getWritableDatabase(); db.delete("Book","pages > ?",new String[]{"100"});查询:
SQLiteDatabase db = dbHelper.getWritableDatabase(); Cursor cursor = db.query("Book",null,null,null,null,null,null); if (cursor.moveToFirst()) { do { String name = cursor.getString(cursor.getColumnIndex("name")); Log.d("MainActivity",name); }while (cursor.moveToNext()); } cursor.close();
用SQL语句CURD操作:
db.execSQL("insert into Book(name,author,pages,price)values(?,?,?,?)",new String[]{"The Da","bob","458","233"});
db.execSQL("update Book set price = ? where name = ?",new String[]{"666","The Da"});
db.execSQL("delete from Book where pages > ?",new String[]{"233"});
db.rawQuery("select * from Book",null);
本文详细介绍了Android应用中常见的四种数据存储方式:文件存储、SharedPreferences、SQLite数据库及内容提供者(Content Provider)的基本使用方法及应用场景。
1588

被折叠的 条评论
为什么被折叠?



