Android(安卓)| 1. Fragment中使用SQLite实现简单存储及查询
16lz
2021-01-26
流程:1.在新建页面输入标题(et_title)和内容(et_content)
2.点击“提交(btn_submit)”,添加至SQLite数据库
3.点击“Log显示数据(btn_show)”,通过Log查看数据库中已存数据
1.SoundFragment(新建页)
(效果如下)
public class SoundFragment extends Fragment { String TAG = "TAG";private EditText et_title,et_content;private Button btn_submit,btn_show;private SQLiteDatabase db;MySQLiteHelper helper; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.sound_fragment, container, false); return view; } @Overridepublic void onActivityCreated(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onActivityCreated(savedInstanceState);et_title = (EditText)getActivity().findViewById(R.id.et_title);et_content = (EditText)getActivity().findViewById(R.id.et_content);helper = new MySQLiteHelper(getActivity(),"diary",null,1);btn_submit = (Button)getActivity().findViewById(R.id.btn_submit);btn_submit.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubdb = helper.getWritableDatabase();Insert();et_title.setText("");et_content.setText("");}});btn_show = (Button)getActivity().findViewById(R.id.btn_show);btn_show.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub//得到数据库db = helper.getWritableDatabase();//查询数据Query();}});} public void Query(){Cursor cursor= db.query("diary",null,null,null,null,null,null);if(cursor.moveToFirst()){do{String title = cursor.getString(cursor.getColumnIndex("title"));String content = cursor.getString(cursor.getColumnIndex("content"));Log.i(TAG, "title:" + title);Log.i(TAG, "content:" + content);}while(cursor.moveToNext());}cursor.close();} public void Insert(){ ContentValues values = new ContentValues();values.put("title", et_title.getText().toString());values.put("content", et_content.getText().toString());db.insert("diary", null, values); } }
2.MySQLiteHelper
学习SQLite必知的SQLiteOpenHelper,如果不知道自行度娘吧,我解释的也不专业~
public class MySQLiteHelper extends SQLiteOpenHelper{private Context context;public MySQLiteHelper(Context context, String name,CursorFactory factory, int version) {super(context, "Blossom.db", null, 1);this.context = context;}@Overridepublic void onCreate(SQLiteDatabase db) {// TODO Auto-generated method stubdb.execSQL("create table diary(id integer PRIMARY KEY AUTOINCREMENT," +"title text,content text)");}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {// TODO Auto-generated method stubdb.execSQL("drop table if exists diary");onCreate(db);}}
之后我们点击Log显示数据,在logcat中即显示:
证明数据成功的存入了SQLite,同时也成功的查到了数据!!
ps.感谢我在网上借鉴的一些大佬,希望我也能早日成为大佬。
更多相关文章
- 一句话锁定MySQL数据占用元凶
- Android(安卓)LsitView的实现
- Android中Handle总结
- android 数据存储 SharePreferences 简单使用
- eclipse编译cocos2d-x2.2.6的Android
- 使用SQLiteOpenHelper 和使用ContentProvider。
- Android请求数据格式与Java后台需求格式不匹配HttpMediaTypeNotS
- Android(安卓)中 ListView 控件的使用
- onCharacteristicChanged 回调问题