流程: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.感谢我在网上借鉴的一些大佬,希望我也能早日成为大佬。

更多相关文章

  1. 一句话锁定MySQL数据占用元凶
  2. Android(安卓)LsitView的实现
  3. Android中Handle总结
  4. android 数据存储 SharePreferences 简单使用
  5. eclipse编译cocos2d-x2.2.6的Android
  6. 使用SQLiteOpenHelper 和使用ContentProvider。
  7. Android请求数据格式与Java后台需求格式不匹配HttpMediaTypeNotS
  8. Android(安卓)中 ListView 控件的使用
  9. onCharacteristicChanged 回调问题

随机推荐

  1. Android中引入开源库(Eclipse与Android(安
  2. Android修行之路——Android程序设计基础
  3. 【漫漫安卓路之承】android tips
  4. Android开发小记之一
  5. android(EditText)属性详解
  6. android shape总结 和控制的风格定制
  7. Android设备硬件序列号(SN、串号)分析
  8. Android(安卓)NDK开发:SeetaFace2实现人脸
  9. Android(安卓)Service :利用Binder实现通
  10. Android(安卓)NDK开发之旅35--FFmpeg+Aud