在SQLite Android中插入和检索数据

时间:2016-05-25 13:56:12

标签: android database sqlite

我第一次使用SQLite在Android上工作,我遇到了一些麻烦,所以我想做的是在我的数据库中插入日期,

然后onCLickLinsten插入然后检索数据将它们发送到另一个活动,只是尝试了解它并实践它,这是我的代码的第二部分:

公共类MainActivity扩展了AppCompatActivity {

private RequestQueue requestQueue;
private static final String URL = "http://192.168.1.104/sync_adapter.php";
private StringRequest request;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SQLiteDatabase myDB= null;
    final String TableName = "Personne";
    final String[] name1 = new String[1];

    final EditText edi1 =  (EditText) findViewById(R.id.edittext1);
    final TextView text1 =  (TextView) findViewById(R.id.textview1);
    Button btn =  (Button) findViewById(R.id.button);
    Button btn2 =  (Button) findViewById(R.id.button2);

    myDB = this.openOrCreateDatabase("namesDB", MODE_PRIVATE, null);
    myDB.execSQL("CREATE TABLE IF NOT EXISTS "
            + TableName
            + " (id  INT(3),name VARCHAR );");

    final SQLiteDatabase finalMyDB = myDB;
    btn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            final String text2 = edi1.getText().toString();

            requestQueue = Volley.newRequestQueue(MainActivity.this);
            request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    JSONArray contacts = null;
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(response);
                        contacts = jsonObject.getJSONArray("result");
                            JSONObject c = contacts.getJSONObject(0);
                            final String  name = c.getString("nom");

                        Toast.makeText(MainActivity.this, "Mr "+name+" ID: "+text2, Toast.LENGTH_SHORT).show();
                        text1.setText(""+name);

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


                }
            },
                    new Response.ErrorListener() {
                public void onErrorResponse(VolleyError error) {

                }
            }){

                protected Map<String, String> getParams() throws AuthFailureError {
                    HashMap<String,String> hashMap = new HashMap<String, String>();
                    hashMap.put("text2",text2);

                    return hashMap;
                }
            };

            requestQueue.add(request);

        }
    });



    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            finalMyDB.execSQL("INSERT INTO "
                    + TableName
                    + " (id, name)"
                    + " VALUES ('text2' ,'name');");

            Cursor c = finalMyDB.rawQuery("SELECT name FROM " + TableName , null);
                int Column1 = c.getColumnIndex("name");

            c.moveToFirst();
            if (c != null) {
                // Loop through all Results
                do {
                     name1[0] = c.getString(Column1);
                }
                while(c.moveToNext());
            }


            Intent i = new Intent(MainActivity.this,ResultActivity.class);
            i.putExtra("name1", name1[0]);
            startActivity(i);


        }
    });
}
我正在做对吗?谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

您的活动中缺少部分代码希望这会对您有所帮助

试着看看logcat或尝试这个

http://androidcoding.in/2016/03/22/android-sqlite-tutorial-on-inserting-deleting-values-into-database/