文件读写、SharePreferences、SQLite
来源:互联网 发布:网易音乐经典评论知乎 编辑:程序博客网 时间:2024/06/05 14:39
文件写:
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);
阅读全文
0 0
- 文件读写、SharePreferences、SQLite
- Android SharePreferences 的使用读写
- SharePreferences 跨APK读写操作
- Android0907<十二>(文件存储,SharePreferences,getCacheDir、Environment,SQLite数据库存储)
- SharePreferences资源文件处理
- SharePreferences
- SharePreferences
- SharePreferences
- sharePreferences
- Sharepreferences
- sharepreferences
- Sharepreferences
- Flash/ActionScript读写SQLite二进制字段里的图片文件
- QT读写Sqlite数据库
- SQLite多线程读写
- SQLite多线程读写
- android sqlite 数据库读写
- SQlite多线程读写
- 第三周 【项目2
- 深入了解 Java迭代器
- 业余草通告CSDN博客用户zhang__ao非法转载文章的公告
- 实验一:顺序表的实现
- poj 3069 Saruman's Army
- 文件读写、SharePreferences、SQLite
- nginx(五)自带变量
- tornado 异步
- java final 构造方法
- CSS样式
- windows安装xampp wamp 集成包流程
- js中常用函数
- Qt 学习之路 2(6):Qt 模块简介
- 管理卡券