创建离线的SQLite数据库组 [英] Create Offline SQLite Database For Groups

查看:99
本文介绍了创建离线的SQLite数据库组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序创建的数据库群页面。但我不知道如何来创建此。 该页面包含两个文本组。这是我的群,我的辅助组。这些两组包含微调供选择电子邮件ID的。通过选择特定的电子邮件ID有两个按钮被添加和删除。 当我选择一个识别码,然后点击添加按钮,然后将其保存在服务器端。有如下微调了一个又一个文本视图上点击它,我可以看到,ID添加。如果此选择标识我想补充已经存在那么消息display.same的删除按钮。和同为我的辅助组。现在,我只想说我是如何创建和存储SQLite数据库对于此页面,因此获取来自SQLite的数据而不是web服务器。

  ** Fragmentgroups.java **



    公共类FragmentGroups扩展片段{

         私人共享preferences preF;
         光标光标;
         ArrayList的<成员> membr,membr2;
         ListView控件List1中;
         ListView控件list2中;
         按钮添加,删除,ADD2,delete2;
         TextView的GP1,GP2;
         GroupAdapter groupadapter;
         私人字样FTYPE;
         SendJobDataBase分贝;
         FragmentDialog对话框;
         ArrayList的<字符串> emailArray =新的ArrayList<字符串>();
         字符串memail;
         私人字符串login_token;
         ArrayAdapter<字符串>适配器 ;
         喷丝E1,E2;
         串电子邮件[];

         公共FragmentGroups(){}

        @覆盖
        公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                捆绑savedInstanceState){
            查看rootView = inflater.inflate(R.layout.group_fragment,集装箱,假);
            E1 =(微调)rootView.findViewById(R.id.eemail);
            E2 =(微调)rootView.findViewById(R.id.eemail2);
                     新会员()执行();
             添加=(按钮)rootView.findViewById(R.id.add);
             ADD2 =(按钮)rootView.findViewById(R.id.add2);
             删除=(按钮)rootView.findViewById(R.id.delete);
             delete2 =(按钮)rootView.findViewById(R.id.delete2);
             GP1 =(TextView中)rootView.findViewById(R.id.viewm);
             GP2 =(TextView中)rootView.findViewById(R.id.views);
             。preF = this.getActivity()getShared preferences(驱动程序,getActivity()MODE_WORLD_READABLE。);
             login_token = pref.getString(login_token,login_token);

            add.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                    新AddMembers()执行();
                    }
                });

            add2.setOnClickListener(新OnClickListener(){
                公共无效的onClick(视图v){
                        新AddMembers2()执行();
                        }
                    });

            delete.setOnClickListener(新OnClickListener(){
                公共无效的onClick(视图v){
                        新DeleteMembers()执行();
                        }
                    });
            delete2.setOnClickListener(新OnClickListener(){
                公共无效的onClick(视图v){
                        新DeleteMembers2()执行()。
                        }
                    });

            gp1.setOnClickListener(新OnClickListener(){
                公共无效的onClick(视图v){
                    意向意图=新的意图(getActivity(),viewgroup.class);
                    startActivity(意向);
                        }
                    });

            gp2.setOnClickListener(新OnClickListener(){
                公共无效的onClick(视图v){
                    意图int​​ent2 =新的意图(getActivity(),viewsecgroup.class);
                    startActivity(intent2);
                        }
                    });

             返回rootView;
           }

        私有类成员扩展的AsyncTask<字符串,字符串,字符串[]> {


            @覆盖
            受保护的String [] doInBackground(最后弦乐... PARAMS)
            {
                ConnectivityManager conMgr =(ConnectivityManager)getActivity()getSystemService(Context.CONNECTIVITY_SERVICE)。
                如果(conMgr.getActiveNetworkInfo()!=空
                        &功放;&安培; conMgr.getActiveNetworkInfo()。isAvailable()
                        &功放;&安培; conMgr.getActiveNetworkInfo()。isConnected())
                {
                    HttpClient的HttpClient的=新DefaultHttpClient();
                    尝试
                    {
                        JSONObject的工作=新的JSONObject();
                         job.put(status_key,2);
                         job.put(法,driver_emails);
                         job.put(login_token,login_token);
                         System.out.print(login_token);


                      StringEntity本身=新StringEntity(job.toString());
                      HttpPost httppost =新HttpPost(http://suntechwebsolutions.com/clients/mobileapp_now/webservice.php);
                      httppost.setEntity(SE);

                      HTT presponse响应= httpclient.execute(httppost);
                      字符串数据= EntityUtils.toString(response.getEntity());

                      JSONObject的乔=新的JSONObject(数据);
                      Log.d(响应,jo.toString(4));

                        JSONArray jArray = jo.getJSONArray(all_driver_email_Ids);
                          的for(int i = 0; I< jArray.length();我++)
                        {
                            的System.out.println(请打印我的名字......为......);
                            尝试 {
                                的JSONObject jsob = jArray.getJSONObject(ⅰ);
                                emailArray.add(jsob.getString(电子邮件)的toString());

                                 适配器=新的ArrayAdapter<字符串>(getActivity(),R.layout.listrow,emailArray);
                          }赶上(JSONException E){

                            }
                        }

                        }
                赶上(例外五)
                    {
                        e.printStackTrace();
                    }
                }

                返回PARAMS;

            }

            @燮pressLint(NewApi)
            @覆盖
            保护无效onPostExecute(字符串[]结果)
            {

                super.onPostExecute(结果);
                e1.setAdapter(适配器);
                e2.setAdapter(适配器);
            }
            }

        私有类AddMembers扩展的AsyncTask<字符串,字符串,字符串[]> {
            ProgressDialog pDialog =新ProgressDialog(getActivity());
            @覆盖
            受保护的String [] doInBackground(最后弦乐... PARAMS)
            {
                ConnectivityManager conMgr =(ConnectivityManager)getActivity()getSystemService(Context.CONNECTIVITY_SERVICE)。
                如果(conMgr.getActiveNetworkInfo()!=空
                        &功放;&安培; conMgr.getActiveNetworkInfo()。isAvailable()
                        &功放;&安培; conMgr.getActiveNetworkInfo()。isConnected())
                {
                    HttpClient的HttpClient的=新DefaultHttpClient();
                    尝试
                    {
                    JSONObject的工作=新的JSONObject();

                        。memail = e1.getSelectedItem()的toString();
                        的System.out.println(memail);
                        memail.replace(,20%);
                         job.put(电子邮件,memail);

                         job.put(status_key,2);
                         job.put(法,add_emails);
                         job.put(login_token,login_token);

                        StringEntity本身=新StringEntity(job.toString());
                        HttpPost httppost =新HttpPost(http://suntechwebsolutions.com/clients/mobileapp_now/webservice.php);
                        httppost.setEntity(SE);
                        HTT presponse响应= httpclient.execute(httppost);
                        字符串数据= EntityUtils.toString(response.getEntity());
                           Log.i(回应,数据);

                            的System.out.println(回应+数据);
                                    字符串调用;
                                    呼叫=数据;

                                    的System.out.println(打印我.............+电话);

                                    JSONObject的乔=新的JSONObject(数据);
                                    Log.d(响应,jo.toString(4));

                                    如果(jo.getString(err- code)。等于(0))
                                    {
                                        最后AlertDialog.Builder警报=新AlertDialog.Builder(getActivity());
                                        alert.setTitle(警报!!!);
                                        alert.setMessage(jo.getString(信息));
                                        alert.setPositiveButton(OK,
                                                新DialogInterface.OnClickListener(){
                                            公共无效的onClick(DialogInterface对话框,
                                                    INT whichButton)
                                            {
                                                pDialog.dismiss();
                                    dialog.dismiss();


                                            }
                                        });
                                        getActivity()。runOnUiThread(新的Runnable(){
                                            公共无效的run(){
                                                alert.show();
                                            }
                                        });
                                    }
                                    其他
                                    {
                                最后AlertDialog.Builder警报=新AlertDialog.Builder(getActivity());
                                        alert.setTitle(警报!);
                                        alert.setMessage(jo.getString(信息));
                                        alert.setPositiveButton(OK,
                                                新DialogInterface.OnClickListener(){
                                            公共无效的onClick(DialogInterface对话框,
                                                    INT whichButton){
                                                dialog.dismiss();

                                            }
                                        });
                                        getActivity()。runOnUiThread(新的Runnable()
                                        {
                                            公共无效的run()
                                            {
                                                pDialog.dismiss();

                                                alert.show();
                                            }
                                        });
                                    }

                                }
                                赶上(例外五)
                                {
                                    e.printStackTrace();
                                }
                            }
                            返回PARAMS;
                        }
                        @覆盖
                        保护无效onPostExecute(字符串[]结果)
                        {

                            super.onPostExecute(结果);
                        }
            }


        私有类DeleteMembers扩展的AsyncTask<字符串,字符串,字符串[]> {
            ProgressDialog pDialog =新ProgressDialog(getActivity());
            @覆盖
            受保护的String [] doInBackground(最后弦乐... PARAMS)
            {
                ConnectivityManager conMgr =(ConnectivityManager)getActivity()getSystemService(Context.CONNECTIVITY_SERVICE)。
                如果(conMgr.getActiveNetworkInfo()!=空
                        &功放;&安培; conMgr.getActiveNetworkInfo()。isAvailable()
                        &功放;&安培; conMgr.getActiveNetworkInfo()。isConnected())
                {
                    HttpClient的HttpClient的=新DefaultHttpClient();
                    尝试
                    {
                    JSONObject的工作=新的JSONObject();

                        。memail = e1.getSelectedItem()的toString();
                        的System.out.println(电子邮件是+ memail);
                        memail.replace(,20%);
                         job.put(电子邮件,memail);
                         job.put(status_key,2);
                         job.put(法,delete_groupemails);
                         job.put(login_token,login_token);

                        StringEntity本身=新StringEntity(job.toString());
                        HttpPost httppost =新HttpPost(http://suntechwebsolutions.com/clients/mobileapp_now/webservice.php);
                        httppost.setEntity(SE);
                        HTT presponse响应= httpclient.execute(httppost);
                        字符串数据= EntityUtils.toString(response.getEntity());
                           Log.i(回应,数据);

                            的System.out.println(回应+数据);
                                    字符串调用;
                                    呼叫=数据;

                                    的System.out.println(打印我.............+电话);

                                    JSONObject的乔=新的JSONObject(数据);
                                    Log.d(响应,jo.toString(4));

                                    如果(jo.getString(err- code)。等于(0))
                                    {
                                        最后AlertDialog.Builder警报=新AlertDialog.Builder(getActivity());
                                        alert.setTitle(警报!!!);
                                        alert.setMessage(jo.getString(信息));
                                        alert.setPositiveButton(OK,
                                                新DialogInterface.OnClickListener(){
                                            公共无效的onClick(DialogInterface对话框,
                                                    INT whichButton)
                                            {
                                                pDialog.dismiss();
                                    dialog.dismiss();


                                            }
                                        });
                                        getActivity()。runOnUiThread(新的Runnable(){
                                            公共无效的run(){
                                                alert.show();
                                            }
                                        });
                                    }
                                    其他
                                    {
                                最后AlertDialog.Builder警报=新AlertDialog.Builder(getActivity());
                                        alert.setTitle(警报!);
                                        alert.setMessage(jo.getString(信息));
                                        alert.setPositiveButton(OK,
                                                新DialogInterface.OnClickListener(){
                                            公共无效的onClick(DialogInterface对话框,
                                                    INT whichButton){
                                                dialog.dismiss();

                                            }
                                        });
                                        getActivity()。runOnUiThread(新的Runnable()
                                        {
                                            公共无效的run()
                                            {
                                                pDialog.dismiss();

                                                alert.show();
                                            }
                                        });
                                    }

                                }
                                赶上(例外五)
                                {
                                    e.printStackTrace();
                                }
                            }
                            返回PARAMS;
                        }
                        @覆盖
                        保护无效onPostExecute(字符串[]结果)
                        {

                            super.onPostExecute(结果);
                        }

        }


        私有类AddMembers2扩展的AsyncTask<字符串,字符串,字符串[]> {
            ProgressDialog pDialog =新ProgressDialog(getActivity());
            @覆盖
            受保护的String [] doInBackground(最后弦乐... PARAMS)
            {
                ConnectivityManager conMgr =(ConnectivityManager)getActivity()getSystemService(Context.CONNECTIVITY_SERVICE)。
                如果(conMgr.getActiveNetworkInfo()!=空
                        &功放;&安培; conMgr.getActiveNetworkInfo()。isAvailable()
                        &功放;&安培; conMgr.getActiveNetworkInfo()。isConnected())
                {
                    HttpClient的HttpClient的=新DefaultHttpClient();
                    尝试
                    {
                    JSONObject的工作=新的JSONObject();

                        。memail = e2.getSelectedItem()的toString();
                        的System.out.println(memail);
                        memail.replace(,20%);
                         job.put(电子邮件,memail);
                         job.put(status_key,2);
                         job.put(法,add_myscndryemails);
                         job.put(login_token,login_token);

                        StringEntity本身=新StringEntity(job.toString());
                        HttpPost httppost =新HttpPost(http://suntechwebsolutions.com/clients/mobileapp_now/webservice.php);
                        httppost.setEntity(SE);
                        HTT presponse响应= httpclient.execute(httppost);
                        字符串数据= EntityUtils.toString(response.getEntity());
                           Log.i(回应,数据);

                            的System.out.println(回应+数据);
                                    字符串调用;
                                    呼叫=数据;

                                    的System.out.println(打印我.............+电话);

                                    JSONObject的乔=新的JSONObject(数据);
                                    Log.d(响应,jo.toString(4));

                                    如果(jo.getString(err- code)。等于(0))
                                    {
                                        最后AlertDialog.Builder警报=新AlertDialog.Builder(getActivity());
                                        alert.setTitle(警报!!!);
                                        alert.setMessage(jo.getString(信息));
                                        alert.setPositiveButton(OK,
                                                新DialogInterface.OnClickListener(){
                                            公共无效的onClick(DialogInterface对话框,
                                                    INT whichButton)
                                            {
                                                pDialog.dismiss();
                                    dialog.dismiss();


                                            }
                                        });
                                        getActivity()。runOnUiThread(新的Runnable(){
                                            公共无效的run(){
                                                alert.show();
                                            }
                                        });
                                    }
                                    其他
                                    {
                                最后AlertDialog.Builder警报=新AlertDialog.Builder(getActivity());
                                        alert.setTitle(警报!);
                                        alert.setMessage(jo.getString(信息));
                                        alert.setPositiveButton(OK,
                                                新DialogInterface.OnClickListener(){
                                            公共无效的onClick(DialogInterface对话框,
                                                    INT whichButton){
                                                dialog.dismiss();

                                            }
                                        });
                                        getActivity()。runOnUiThread(新的Runnable()
                                        {
                                            公共无效的run()
                                            {
                                                pDialog.dismiss();

                                                alert.show();
                                            }
                                        });
                                    }

                                }
                                赶上(例外五)
                                {
                                    e.printStackTrace();
                                }
                            }
                            返回PARAMS;
                        }
                        @覆盖
                        保护无效onPostExecute(字符串[]结果)
                        {

                            super.onPostExecute(结果);
                        }
                   }

        私有类DeleteMembers2扩展的AsyncTask<字符串,字符串,字符串[]> {
            ProgressDialog pDialog =新ProgressDialog(getActivity());
            @覆盖
            受保护的String [] doInBackground(最后弦乐... PARAMS)
            {
                ConnectivityManager conMgr =(ConnectivityManager)getActivity()getSystemService(Context.CONNECTIVITY_SERVICE)。
                如果(conMgr.getActiveNetworkInfo()!=空
                        &功放;&安培; conMgr.getActiveNetworkInfo()。isAvailable()
                        &功放;&安培; conMgr.getActiveNetworkInfo()。isConnected())
                {
                    HttpClient的HttpClient的=新DefaultHttpClient();
                    尝试
                    {
                    JSONObject的工作=新的JSONObject();

                        。memail = e2.getSelectedItem()的toString();
                        的System.out.println(电子邮件是+ memail);
                        memail.replace(,20%);
                         job.put(电子邮件,memail);

                         job.put(status_key,2);
                         job.put(法,delete_scndryemails);
                         job.put(login_token,login_token);

                        StringEntity本身=新StringEntity(job.toString());
                        HttpPost httppost =新HttpPost(http://suntechwebsolutions.com/clients/mobileapp_now/webservice.php);
                        httppost.setEntity(SE);
                        HTT presponse响应= httpclient.execute(httppost);
                        字符串数据= EntityUtils.toString(response.getEntity());
                           Log.i(回应,数据);

                            的System.out.println(回应+数据);
                                    字符串调用;
                                    呼叫=数据;

                                    的System.out.println(打印我.............+电话);

                                    JSONObject的乔=新的JSONObject(数据);
                                    Log.d(响应,jo.toString(4));

                                    如果(jo.getString(err- code)。等于(0))
                                    {
                                        最后AlertDialog.Builder警报=新AlertDialog.Builder(getActivity());
                                        alert.setTitle(警报!!!);
                                        alert.setMessage(jo.getString(信息));
                                        alert.setPositiveButton(OK,
                                                新DialogInterface.OnClickListener(){
                                            公共无效的onClick(DialogInterface对话框,
                                                    INT whichButton)
                                            {
                                                pDialog.dismiss();
                                    dialog.dismiss();
                                        }
                                        });
                                        getActivity()。runOnUiThread(新的Runnable(){
                                            公共无效的run(){
                                                alert.show();
                                            }
                                        });
                                    }
                                    其他
                                    {
                                最后AlertDialog.Builder警报=新AlertDialog.Builder(getActivity());
                                        alert.setTitle(警报!);
                                        alert.setMessage(jo.getString(信息));
                                        alert.setPositiveButton(OK,
                                                新DialogInterface.OnClickListener(){
                                            公共无效的onClick(DialogInterface对话框,
                                                    INT whichButton){
                                                dialog.dismiss();

                                            }
                                        });
                                        getActivity()。runOnUiThread(新的Runnable()
                                        {
                                            公共无效的run()
                                            {
                                                pDialog.dismiss();

                                                alert.show();
                                            }
                                        });
                                    }

                                }
                                赶上(例外五)
                                {
                                    e.printStackTrace();
                                }
                            }
                            返回PARAMS;
                        }
                        @覆盖
                        保护无效onPostExecute(字符串[]结果)
                        {

                            super.onPostExecute(结果);
                        }

        }

    }




** GroupAdapter.java **



包com.sunmobile.navigationdrawer;

进口的java.util.ArrayList;
进口com.sunmobileappnow.mobileappnow.R;
进口android.app.Activity;
进口android.app.AlertDialog;
进口android.content.Context;
进口android.content.DialogInterface;
进口android.content.Intent;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.view.View.OnClickListener;
进口android.widget.ArrayAdapter;
进口android.widget.Button;
进口android.widget.ListView;
进口android.widget.TextView;



公共类GroupAdapter扩展ArrayAdapter<成员> {


    GroupAdapter GDB;
    活动活性;
    INT layoutResourceId;
    会员用户;
    ArrayList的<成员>数据=新的ArrayList<成员>();
    ListView的L1;
    SendJobDataBase分贝;
   FragmentGroups FG;




公共GroupAdapter(活动的行为,诠释layoutResourceId,
            ArrayList的<成员>数据) {
            超(行为,layoutResourceId,数据);
            this.layoutResourceId = layoutResourceId;
            this.activity =行为;
            this.data =数据;
            notifyDataSetChanged();
        }



    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        查看排= convertView;
        MemberHolder支架=无效;

        如果(行== NULL){
        LayoutInflater充气= LayoutInflater.from(活动);

        行= inflater.inflate(layoutResourceId,父母,假);
        持有人=新MemberHolder();
        holder.name =(TextView中)row.findViewById(R.id.user_name_txt);
        holder.delete =(按钮)row.findViewById(R.id.btn_delete);
        row.setTag(保持器);
       } 其他 {
    支架=(MemberHolder)row.getTag();
        }
       用户= data.get(位置);

        holder.name.setText(user.getName());




      holder.delete.setOnClickListener(新OnClickListener(){

        公共无效的onClick(最终查看V){


            AlertDialog.Builder ADB =新AlertDialog.Builder(活动);
            adb.setTitle(删除?);
            adb.setMessage(你确定要删除);
            最终诠释USER_ID =的Integer.parseInt(v.getTag()的toString());
            adb.setNegativeButton(取消,NULL);
            adb.setPositiveButton(OK,
                新AlertDialog.OnClickListener(){
                公共无效的onClick(DialogInterface对话框,
                    其中INT){
                    // MyDataObject.remove(positionToRemove);
                    SendJobDataBase dBHandler =新SendJobDataBase(
                        activity.getApplicationContext());
                   dBHandler.Delete_Contact(USER_ID);


                }
                });


            adb.show();
        }

        });
        返回行;

    }

    类MemberHolder {
        TextView的名称;
       按钮删除;
    }

    }
 

解决方案

我使用的SQLite数据库交互的模板如下:

助手创建数据库:

 公共类DatabaseHelper扩展SQLiteOpenHelper
{
   私有静态最后弦乐DATABASE_NAME =database.db;
   私有静态最终诠释DATABASE_VERSION = 1;

   公共DatabaseHelper(上下文的背景下)
   {
      超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
   }

   @覆盖
   公共无效的onCreate(SQLiteDatabase数据库)
   {
      Table1.onCreate(数据库);
      Table2.onCreate(数据库);
      Table3.onCreate(数据库);
   }

   @覆盖
   公共无效onUpgrade(SQLiteDatabase数据库,诠释oldVersion,诠释静态网页)
   {
      Table1.onUpgrade(数据库,oldVersion,静态网页);
      Table2.onUpgrade(数据库,oldVersion,静态网页);
      Table3.onUpgrade(数据库,oldVersion,静态网页);
   }
}
 

一个表为例:

 公共类表1
{
    公共静态最后弦乐TABLE_TABLE1 =表1;

    公共静态最后弦乐COLUMN_ID =_id;
    公共静态最后弦乐COLUMN_NAME =名;
    公共静态最后弦乐COLUMN_PHONENUMBER =phoneNumber的;
    公共静态最后弦乐COLUMN_EMAIL =电子邮件;

    私有静态最后弦乐DATABASE_CREATE =创建表+ TABLE_TABLE1 +(
    + COLUMN_ID +整数主键自动增量,
    + COLUMN_NAME +文字不为空,
    + COLUMN_PHONENUMBER +文字不为空,
    + COLUMN_EMAIL +文字不为空,
    +);;

    公共静态无效的onCreate(SQLiteDatabase数据库)
    {
       database.execSQL(DATABASE_CREATE);
    }

    公共静态无效onUpgrade(SQLiteDatabase数据库,诠释oldVersion,诠释静态网页)
    {
       Log.w(Table1.class.getName(),从版本升级+ oldVersion +到+静态网页);
       database.execSQL(DROP TABLE IF EXISTS+ TABLE_TABLE1);
       的onCreate(数据库);
    }
}
 

操作的表:

 公共类Table1DataSource
{
    //数据库的字段
    私人SQLiteDatabase数据库;
    私人DatabaseHelper dbHelper;
    私有String [] allColumns = {Table1.COLUMN_ID,Table1.COLUMN_NAME,
            Table1.COLUMN_PHONENUMBER,Table1.COLUMN_EMAIL};

    公共Table1DataSource(上下文的背景下)
    {
        dbHelper =新DatabaseHelper(上下文);
    }

    公共无效的open()抛出的SQLException
    {
        数据库= dbHelper.getWritableDatabase();
    }

    公共无效的close()
    {
        dbHelper.close();
    }

    私人EmployeeDB cursorToEmployee(光标指针)
    {
        EmployeeDB员工=新EmployeeDB();
        Employee.setId(cursor.getLong(0));
        Employee.setName(cursor.getString(1));
        Employee.setPhoneNumber(cursor.getString(2));
        Employee.setEmail(cursor.getString(3));
        回报员工;
    }

    公共EmployeeDB createEmployee(字符串名称,字符串phoneNumber的,弦乐电子邮件)
    {
        ContentValues​​值=新ContentValues​​();
        values​​.put(Table1.COLUMN_NAME,姓名);
        values​​.put(Table1.COLUMN_PHONENUMBER,phoneNumber的);
        values​​.put(Table1.COLUMN_EMAIL,电子邮件);
        长insertId = database.insert(Table1.TABLE_TABLE1,空,价值观);
        光标光标= database.query(Table1.TABLE_TABLE1,allColumns,Table1.COLUMN_ID
                +=+ insertId,NULL,NULL,NULL,NULL);
        cursor.moveToFirst();
        EmployeeDB newEmployee = cursorToEmployee(光标);
        cursor.close();
        返回newEmployee;
    }

    公共无效updateEmployee(EmployeeDB员工)
    {
        ContentValues​​值=新ContentValues​​();
        values​​.put(Table1.COLUMN_NAME,姓名);
        values​​.put(Table1.COLUMN_PHONENUMBER,phoneNumber的);
        values​​.put(Table1.COLUMN_EMAIL,电子邮件);
        database.update(Table1.TABLE_TABLE1,价值观,Table1.COLUMN_ID +=?,新的String [] {+ employee.getId()});
    }

    公共无效deleteEmployee(EmployeeDB员工)
    {
        长ID = Employee.getId();
        database.delete(EmployeeTable.TABLE_EMPLOYEE,EmployeeTable.COLUMN_ID +=+ ID,NULL);
    }

    公共光标getAllEmployees()
    {
        返回database.query(EmployeeTable.TABLE_EMPLOYEE,allColumns,NULL,NULL,NULL,NULL,NULL);
    }

    公共EmployeeDB findEmployeeById(长ID)
    {
        EmployeeDB员工= NULL;
        光标光标= database.query(EmployeeTable.TABLE_EMPLOYEE,allColumns,EmployeeTable.COLUMN_ID +=?,新的String [] {+ ID},NULL,NULL,NULL);
        如果(cursor.moveToFirst())
        {
            员工= cursorToEmployee(光标);
        }
        返回的员工;
    }

    公开名单< EmployeeDB> getAllEmployeesList()
    {
        名单< EmployeeDB>员工=新的ArrayList< EmployeeDB>();

        光标光标= getAllEmployees();

        如果(cursor.moveToFirst())
        {
            做
            {
                EmployeeDB员工= cursorToEmployee(光标);
                Employees.add(员工);
                cursor.moveToNext();
            }
            而(cursor.moveToNext());
        }
        cursor.close();
        返回的员工;
    }
}
 

I am trying to create the the database for groups page in my application. But I do not know how to create this. This page include the two Text groups. That is my group and my secondary group. each of these two group contain spinner for selection of email id. By selecting a particular email id there are two button that are add and delete. when I select One Id and click on add button then it is saved on server side .there is a another text view below spinner on click of which I can see that the Id is added .if this selected Id I want to add has already exist then message display.same for delete button. and same for my secondary group. Now I just want that how I create and store SQLite Database For this page so it fetch data from SQLite rather then the web server.

**Fragmentgroups.java**



    public class FragmentGroups extends Fragment {

         private SharedPreferences pref;
         Cursor cursor;
         ArrayList<member> membr,membr2;
         ListView list1 ;
         ListView list2 ;
         Button add,delete,add2,delete2;
         TextView gp1,gp2;
         GroupAdapter groupadapter;
         private Typeface ftype;
         SendJobDataBase db;
         FragmentDialog dialog ;
         ArrayList<String> emailArray = new ArrayList<String>();
         String memail;
         private String login_token;
         ArrayAdapter<String> adapter ;
         Spinner e1,e2;
         String emails[];

         public FragmentGroups(){}

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.group_fragment, container, false);
            e1=(Spinner) rootView.findViewById(R.id.eemail);
            e2=(Spinner) rootView.findViewById(R.id.eemail2); 
                     new Members().execute();
             add =(Button) rootView.findViewById(R.id.add);
             add2 =(Button) rootView.findViewById(R.id.add2);
             delete =(Button) rootView.findViewById(R.id.delete);
             delete2 =(Button) rootView.findViewById(R.id.delete2);
             gp1  =(TextView) rootView.findViewById(R.id.viewm);
             gp2  =(TextView) rootView.findViewById(R.id.views);
             pref=this.getActivity().getSharedPreferences("Driver", getActivity().MODE_WORLD_READABLE);
             login_token = pref.getString("login_token","login_token"); 

            add.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                    new AddMembers().execute(); 
                    }           
                });

            add2.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                        new AddMembers2().execute();    
                        }           
                    });

            delete.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                        new DeleteMembers().execute();  
                        }           
                    });
            delete2.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                        new DeleteMembers2().execute(); 
                        }           
                    });

            gp1.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Intent intent =new Intent(getActivity() ,viewgroup.class);
                    startActivity(intent);
                        }           
                    });

            gp2.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Intent intent2 =new Intent(getActivity() ,viewsecgroup.class);
                    startActivity(intent2);
                        }           
                    });

             return rootView;
           }

        private class Members extends AsyncTask<String, String, String[]> {


            @Override
            protected String[] doInBackground(final String... params) 
            {
                ConnectivityManager conMgr = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
                if (conMgr.getActiveNetworkInfo() != null
                        && conMgr.getActiveNetworkInfo().isAvailable()
                        && conMgr.getActiveNetworkInfo().isConnected()) 
                {
                    HttpClient httpclient = new DefaultHttpClient();
                    try 
                    {
                        JSONObject job= new JSONObject();
                         job.put("status_key","2");
                         job.put("method","driver_emails");
                         job.put("login_token",login_token);
                         System.out.print(login_token);


                      StringEntity se = new StringEntity(job.toString());
                      HttpPost httppost = new HttpPost("http://suntechwebsolutions.com/clients/mobileapp_now/webservice.php");
                      httppost.setEntity(se);

                      HttpResponse response = httpclient.execute(httppost);
                      String data = EntityUtils.toString(response.getEntity());

                      JSONObject jo = new JSONObject(data);
                      Log.d("response", jo.toString(4));

                        JSONArray jArray = jo.getJSONArray("all_driver_email_Ids");  
                          for (int i=0; i < jArray.length(); i++)
                        {
                            System.out.println("please print my name...for....");
                            try {
                                JSONObject jsob = jArray.getJSONObject(i);
                                emailArray.add(jsob.getString("Email").toString());

                                 adapter = new ArrayAdapter<String>(getActivity(), R.layout.listrow, emailArray);           
                          } catch (JSONException e) {

                            } 
                        }

                        }
                catch (Exception e) 
                    {
                        e.printStackTrace();
                    }
                }

                return params;

            }             

            @SuppressLint("NewApi")
            @Override
            protected void onPostExecute(String[] result) 
            {

                super.onPostExecute(result);
                e1.setAdapter(adapter); 
                e2.setAdapter(adapter);
            }
            }   

        private class AddMembers extends AsyncTask<String, String, String[]> {
            ProgressDialog pDialog = new ProgressDialog(getActivity());
            @Override
            protected String[] doInBackground(final String... params) 
            {
                ConnectivityManager conMgr = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
                if (conMgr.getActiveNetworkInfo() != null
                        && conMgr.getActiveNetworkInfo().isAvailable()
                        && conMgr.getActiveNetworkInfo().isConnected()) 
                {
                    HttpClient httpclient = new DefaultHttpClient();
                    try 
                    {
                    JSONObject job= new JSONObject();

                        memail= e1.getSelectedItem().toString();
                        System.out.println(memail);
                        memail.replace("" ,"%20");
                         job.put("email",memail);

                         job.put("status_key","2");
                         job.put("method","add_emails");
                         job.put("login_token",login_token);

                        StringEntity se = new StringEntity(job.toString());
                        HttpPost httppost = new HttpPost("http://suntechwebsolutions.com/clients/mobileapp_now/webservice.php");
                        httppost.setEntity(se);
                        HttpResponse response = httpclient.execute(httppost);
                        String data = EntityUtils.toString(response.getEntity());
                           Log.i("response", data);

                            System.out.println("response "+data);
                                    String call;
                                    call = data;

                                    System.out.println("print me............."+call);

                                    JSONObject jo = new JSONObject(data);
                                    Log.d("response", jo.toString(4));

                                    if(jo.getString("err-code").equals("0"))
                                    {
                                        final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                                        alert.setTitle("Alert!!!");
                                        alert.setMessage(jo.getString("message"));
                                        alert.setPositiveButton("Ok",
                                                new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog,
                                                    int whichButton) 
                                            {
                                                pDialog.dismiss();
                                    dialog.dismiss();


                                            }
                                        });
                                        getActivity().runOnUiThread(new Runnable() {
                                            public void run() {
                                                alert.show();
                                            }
                                        });
                                    }
                                    else
                                    {
                                final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                                        alert.setTitle("Alert !");
                                        alert.setMessage(jo.getString("message"));
                                        alert.setPositiveButton("Ok",
                                                new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog,
                                                    int whichButton) {
                                                dialog.dismiss();

                                            }
                                        });
                                        getActivity().runOnUiThread(new Runnable() 
                                        {
                                            public void run() 
                                            {
                                                pDialog.dismiss();

                                                alert.show();
                                            }
                                        });
                                    }

                                }
                                catch (Exception e) 
                                {
                                    e.printStackTrace();
                                }
                            }
                            return params;
                        }
                        @Override
                        protected void onPostExecute(String[] result) 
                        {

                            super.onPostExecute(result);
                        }       
            }   


        private class DeleteMembers extends AsyncTask<String, String, String[]> {
            ProgressDialog pDialog = new ProgressDialog(getActivity());
            @Override
            protected String[] doInBackground(final String... params) 
            {
                ConnectivityManager conMgr = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
                if (conMgr.getActiveNetworkInfo() != null
                        && conMgr.getActiveNetworkInfo().isAvailable()
                        && conMgr.getActiveNetworkInfo().isConnected()) 
                {
                    HttpClient httpclient = new DefaultHttpClient();
                    try 
                    {
                    JSONObject job= new JSONObject();

                        memail= e1.getSelectedItem().toString();
                        System.out.println("email is " +memail);
                        memail.replace("" ,"%20");
                         job.put("email",memail);
                         job.put("status_key","2");
                         job.put("method","delete_groupemails");
                         job.put("login_token",login_token);

                        StringEntity se = new StringEntity(job.toString());
                        HttpPost httppost = new HttpPost("http://suntechwebsolutions.com/clients/mobileapp_now/webservice.php");
                        httppost.setEntity(se);
                        HttpResponse response = httpclient.execute(httppost);
                        String data = EntityUtils.toString(response.getEntity());
                           Log.i("response", data);

                            System.out.println("response "+data);
                                    String call;
                                    call = data;

                                    System.out.println("print me............."+call);

                                    JSONObject jo = new JSONObject(data);
                                    Log.d("response", jo.toString(4));

                                    if(jo.getString("err-code").equals("0"))
                                    {
                                        final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                                        alert.setTitle("Alert!!!");
                                        alert.setMessage(jo.getString("message"));
                                        alert.setPositiveButton("Ok",
                                                new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog,
                                                    int whichButton) 
                                            {
                                                pDialog.dismiss();
                                    dialog.dismiss();


                                            }
                                        });
                                        getActivity().runOnUiThread(new Runnable() {
                                            public void run() {
                                                alert.show();
                                            }
                                        });
                                    }
                                    else
                                    {
                                final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                                        alert.setTitle("Alert !");
                                        alert.setMessage(jo.getString("message"));
                                        alert.setPositiveButton("Ok",
                                                new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog,
                                                    int whichButton) {
                                                dialog.dismiss();

                                            }
                                        });
                                        getActivity().runOnUiThread(new Runnable() 
                                        {
                                            public void run() 
                                            {
                                                pDialog.dismiss();

                                                alert.show();
                                            }
                                        });
                                    }

                                }
                                catch (Exception e) 
                                {
                                    e.printStackTrace();
                                }
                            }
                            return params;
                        }
                        @Override
                        protected void onPostExecute(String[] result) 
                        {

                            super.onPostExecute(result);
                        }

        }   


        private class AddMembers2 extends AsyncTask<String, String, String[]> {
            ProgressDialog pDialog = new ProgressDialog(getActivity());
            @Override
            protected String[] doInBackground(final String... params) 
            {
                ConnectivityManager conMgr = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
                if (conMgr.getActiveNetworkInfo() != null
                        && conMgr.getActiveNetworkInfo().isAvailable()
                        && conMgr.getActiveNetworkInfo().isConnected()) 
                {
                    HttpClient httpclient = new DefaultHttpClient();
                    try 
                    {
                    JSONObject job= new JSONObject();

                        memail= e2.getSelectedItem().toString();
                        System.out.println(memail);
                        memail.replace("" ,"%20");
                         job.put("email",memail);
                         job.put("status_key","2");
                         job.put("method","add_myscndryemails");
                         job.put("login_token",login_token);

                        StringEntity se = new StringEntity(job.toString());
                        HttpPost httppost = new HttpPost("http://suntechwebsolutions.com/clients/mobileapp_now/webservice.php");
                        httppost.setEntity(se);
                        HttpResponse response = httpclient.execute(httppost);
                        String data = EntityUtils.toString(response.getEntity());
                           Log.i("response", data);

                            System.out.println("response "+data);
                                    String call;
                                    call = data;

                                    System.out.println("print me............."+call);

                                    JSONObject jo = new JSONObject(data);
                                    Log.d("response", jo.toString(4));

                                    if(jo.getString("err-code").equals("0"))
                                    {
                                        final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                                        alert.setTitle("Alert!!!");
                                        alert.setMessage(jo.getString("message"));
                                        alert.setPositiveButton("Ok",
                                                new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog,
                                                    int whichButton) 
                                            {
                                                pDialog.dismiss();
                                    dialog.dismiss();


                                            }
                                        });
                                        getActivity().runOnUiThread(new Runnable() {
                                            public void run() {
                                                alert.show();
                                            }
                                        });
                                    }
                                    else
                                    {
                                final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                                        alert.setTitle("Alert !");
                                        alert.setMessage(jo.getString("message"));
                                        alert.setPositiveButton("Ok",
                                                new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog,
                                                    int whichButton) {
                                                dialog.dismiss();

                                            }
                                        });
                                        getActivity().runOnUiThread(new Runnable() 
                                        {
                                            public void run() 
                                            {
                                                pDialog.dismiss();

                                                alert.show();
                                            }
                                        });
                                    }

                                }
                                catch (Exception e) 
                                {
                                    e.printStackTrace();
                                }
                            }
                            return params;
                        }
                        @Override
                        protected void onPostExecute(String[] result) 
                        {

                            super.onPostExecute(result);
                        }       
                   }    

        private class DeleteMembers2 extends AsyncTask<String, String, String[]> {
            ProgressDialog pDialog = new ProgressDialog(getActivity());
            @Override
            protected String[] doInBackground(final String... params) 
            {
                ConnectivityManager conMgr = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
                if (conMgr.getActiveNetworkInfo() != null
                        && conMgr.getActiveNetworkInfo().isAvailable()
                        && conMgr.getActiveNetworkInfo().isConnected()) 
                {
                    HttpClient httpclient = new DefaultHttpClient();
                    try 
                    {
                    JSONObject job= new JSONObject();

                        memail= e2.getSelectedItem().toString();
                        System.out.println("email is " +memail);
                        memail.replace("" ,"%20");
                         job.put("email",memail);

                         job.put("status_key","2");
                         job.put("method","delete_scndryemails");
                         job.put("login_token",login_token);

                        StringEntity se = new StringEntity(job.toString());
                        HttpPost httppost = new HttpPost("http://suntechwebsolutions.com/clients/mobileapp_now/webservice.php");
                        httppost.setEntity(se);
                        HttpResponse response = httpclient.execute(httppost);
                        String data = EntityUtils.toString(response.getEntity());
                           Log.i("response", data);

                            System.out.println("response "+data);
                                    String call;
                                    call = data;

                                    System.out.println("print me............."+call);

                                    JSONObject jo = new JSONObject(data);
                                    Log.d("response", jo.toString(4));

                                    if(jo.getString("err-code").equals("0"))
                                    {
                                        final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                                        alert.setTitle("Alert!!!");
                                        alert.setMessage(jo.getString("message"));
                                        alert.setPositiveButton("Ok",
                                                new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog,
                                                    int whichButton) 
                                            {
                                                pDialog.dismiss();
                                    dialog.dismiss();
                                        }
                                        });
                                        getActivity().runOnUiThread(new Runnable() {
                                            public void run() {
                                                alert.show();
                                            }
                                        });
                                    }
                                    else
                                    {
                                final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                                        alert.setTitle("Alert !");
                                        alert.setMessage(jo.getString("message"));
                                        alert.setPositiveButton("Ok",
                                                new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog,
                                                    int whichButton) {
                                                dialog.dismiss();

                                            }
                                        });
                                        getActivity().runOnUiThread(new Runnable() 
                                        {
                                            public void run() 
                                            {
                                                pDialog.dismiss();

                                                alert.show();
                                            }
                                        });
                                    }

                                }
                                catch (Exception e) 
                                {
                                    e.printStackTrace();
                                }
                            }
                            return params;
                        }
                        @Override
                        protected void onPostExecute(String[] result) 
                        {

                            super.onPostExecute(result);
                        }

        }   

    }




**GroupAdapter.java**



package com.sunmobile.navigationdrawer;

import java.util.ArrayList;
import com.sunmobileappnow.mobileappnow.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;



public class GroupAdapter extends ArrayAdapter<member> {


    GroupAdapter gdb;
    Activity activity;
    int layoutResourceId;
    member user;
    ArrayList<member> data = new ArrayList<member>();
    ListView l1;
    SendJobDataBase db;
   FragmentGroups fg;




public GroupAdapter(Activity act, int layoutResourceId,
            ArrayList<member> data) {
            super(act, layoutResourceId, data);
            this.layoutResourceId = layoutResourceId;
            this.activity = act;
            this.data = data;
            notifyDataSetChanged();
        }



    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        MemberHolder holder = null;

        if (row == null) {
        LayoutInflater inflater = LayoutInflater.from(activity);

        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new MemberHolder();
        holder.name = (TextView) row.findViewById(R.id.user_name_txt);
        holder.delete = (Button) row.findViewById(R.id.btn_delete);
        row.setTag(holder);
       } else {
    holder = (MemberHolder) row.getTag();
        }
       user = data.get(position);

        holder.name.setText(user.getName());




      holder.delete.setOnClickListener(new OnClickListener() {

        public void onClick(final View v) {


            AlertDialog.Builder adb = new AlertDialog.Builder(activity);
            adb.setTitle("Delete?");
            adb.setMessage("Are you sure you want to delete ");
            final int user_id = Integer.parseInt(v.getTag().toString());
            adb.setNegativeButton("Cancel", null);
            adb.setPositiveButton("Ok",
                new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog,
                    int which) {
                    // MyDataObject.remove(positionToRemove);
                    SendJobDataBase dBHandler = new SendJobDataBase(
                        activity.getApplicationContext());
                   dBHandler.Delete_Contact(user_id);


                }
                });    


            adb.show();
        }

        });  
        return row;

    }

    class MemberHolder {
        TextView name;
       Button delete;
    }

    }

解决方案

The template I use for SQLite database interaction is the following:

The helper for creating database:

public class DatabaseHelper extends SQLiteOpenHelper
{
   private static final String DATABASE_NAME = "database.db";
   private static final int DATABASE_VERSION = 1;

   public DatabaseHelper(Context context)
   {
      super(context, DATABASE_NAME, null, DATABASE_VERSION);
   }

   @Override
   public void onCreate(SQLiteDatabase database)
   {
      Table1.onCreate(database);
      Table2.onCreate(database);
      Table3.onCreate(database);
   }

   @Override
   public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion)
   {
      Table1.onUpgrade(database, oldVersion, newVersion);
      Table2.onUpgrade(database, oldVersion, newVersion);
      Table3.onUpgrade(database, oldVersion, newVersion);
   }
}

A table as an example:

public class Table1
{
    public static final String TABLE_TABLE1 = "table1";

    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_NAME = "name";
    public static final String COLUMN_PHONENUMBER = "phoneNumber";
    public static final String COLUMN_EMAIL = "email";

    private static final String DATABASE_CREATE = "create table " + TABLE_TABLE1 + "(" 
    + COLUMN_ID + " integer primary key autoincrement, " 
    + COLUMN_NAME + " text not null, "
    + COLUMN_PHONENUMBER + " text not null, "
    + COLUMN_EMAIL + " text not null, "
    + ");";

    public static void onCreate(SQLiteDatabase database)
    {
       database.execSQL(DATABASE_CREATE);
    }

    public static void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion)
    {
       Log.w(Table1.class.getName(), "Upgrading from version " + oldVersion + " to " + newVersion);
       database.execSQL("DROP TABLE IF EXISTS " + TABLE_TABLE1);
       onCreate(database);
    }
}

Operations for table:

public class Table1DataSource
{
    // Database fields
    private SQLiteDatabase database;
    private DatabaseHelper dbHelper;
    private String[] allColumns = { Table1.COLUMN_ID, Table1.COLUMN_NAME,
            Table1.COLUMN_PHONENUMBER, Table1.COLUMN_EMAIL };

    public Table1DataSource(Context context)
    {
        dbHelper = new DatabaseHelper(context);
    }

    public void open() throws SQLException
    {
        database = dbHelper.getWritableDatabase();
    }

    public void close()
    {
        dbHelper.close();
    }

    private EmployeeDB cursorToEmployee(Cursor cursor)
    {
        EmployeeDB Employee = new EmployeeDB();
        Employee.setId(cursor.getLong(0));
        Employee.setName(cursor.getString(1));
        Employee.setPhoneNumber(cursor.getString(2));
        Employee.setEmail(cursor.getString(3));
        return Employee;
    }

    public EmployeeDB createEmployee(String name, String phoneNumber, String email)
    {
        ContentValues values = new ContentValues();
        values.put(Table1.COLUMN_NAME, name);
        values.put(Table1.COLUMN_PHONENUMBER, phoneNumber);
        values.put(Table1.COLUMN_EMAIL, email);
        long insertId = database.insert(Table1.TABLE_TABLE1, null, values);
        Cursor cursor = database.query(Table1.TABLE_TABLE1, allColumns, Table1.COLUMN_ID
                + " = " + insertId, null, null, null, null);
        cursor.moveToFirst();
        EmployeeDB newEmployee = cursorToEmployee(cursor);
        cursor.close();
        return newEmployee;
    }

    public void updateEmployee(EmployeeDB employee)
    {
        ContentValues values = new ContentValues();
        values.put(Table1.COLUMN_NAME, name);
        values.put(Table1.COLUMN_PHONENUMBER, phoneNumber);
        values.put(Table1.COLUMN_EMAIL, email);
        database.update(Table1.TABLE_TABLE1, values, Table1.COLUMN_ID + " = ?", new String[]{""+employee.getId()});
    }

    public void deleteEmployee(EmployeeDB Employee)
    {
        long id = Employee.getId();
        database.delete(EmployeeTable.TABLE_EMPLOYEE, EmployeeTable.COLUMN_ID + " = " + id, null);
    }

    public Cursor getAllEmployees()
    {
        return database.query(EmployeeTable.TABLE_EMPLOYEE, allColumns, null, null, null, null, null);
    }

    public EmployeeDB findEmployeeById(long id)
    {
        EmployeeDB employee = null;
        Cursor cursor = database.query(EmployeeTable.TABLE_EMPLOYEE, allColumns, EmployeeTable.COLUMN_ID + " = ?", new String[] {""+id}, null, null, null);
        if(cursor.moveToFirst())
        {
            employee = cursorToEmployee(cursor);
        }
        return employee;
    }

    public List<EmployeeDB> getAllEmployeesList()
    {
        List<EmployeeDB> Employees = new ArrayList<EmployeeDB>();

        Cursor cursor = getAllEmployees();

        if (cursor.moveToFirst())
        {
            do
            {
                EmployeeDB Employee = cursorToEmployee(cursor);
                Employees.add(Employee);
                cursor.moveToNext();
            }
            while (cursor.moveToNext());
        }
        cursor.close();
        return Employees;
    } 
}

这篇关于创建离线的SQLite数据库组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆