简体   繁体   中英

Insert data into SQlite databases in Android Studio while button click

i am making a app which is inserted measurement detail of a customer . but there is problem in my database and i dont know what ius this.when i am click on addbtn it show toast failed(data not insert) i tried all method but nothing is work .please help me out with this why the data is not inserted.

'''

package com.example.customermanagement;
import androidx.appcompat.app.AppCompatActivity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Add_Customer extends AppCompatActivity {
EditText et1,et2,et3,et4,et5,et6,et7,et8,et9,et10;
Button addbtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add__customer);
        MyHelper myhelper=new MyHelper(this);
        addbtn=(Button)findViewById(R.id.addbtn);
        addbtn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               et1 =(EditText)findViewById(R.id.name);
               String customername=et1.getText().toString();
               //showtxt=(EditText) findViewById(R.id.others);
               //showtxt.setText(customername);
               et2 =(EditText)findViewById(R.id.phone);
               String phoneno=et2.getText().toString();
               et3 =(EditText)findViewById(R.id.lenght);
               String lenght=et3.getText().toString();
               et4 =(EditText)findViewById(R.id.teera);
               String teera=et4.getText().toString();
               et5 =(EditText)findViewById(R.id.bazu);
               String bazu=et5.getText().toString();
               et6 =(EditText)findViewById(R.id.chati);
               String chati=et6.getText().toString();
               et7 =(EditText)findViewById(R.id.qamar);
               String qamar=et7.getText().toString();
               et8 =(EditText)findViewById(R.id.shalwar);
               String shalwar=et8.getText().toString();
               et9 =(EditText)findViewById(R.id.paincha);
               String paincha=et9.getText().toString();
               et10 =(EditText)findViewById(R.id.others);
               String others=et10.getText().toString();
              // Toast.makeText(Add_Customer.this,lenght,Toast.LENGTH_SHORT).show();
               myhelper.getWritableDatabase();
               Boolean i= myhelper.insert_data(customername,phoneno,lenght,teera,bazu,chati,qamar,shalwar,paincha,"7","7","7","7","7","7","9","9",others);
                if(i==true)
                {
                    Toast.makeText(Add_Customer.this,"succesfull enter",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(Add_Customer.this,"Fail",Toast.LENGTH_SHORT).show();

                }
           }
       });

    }
}

''' and there is the code of my database helper

'''

package com.example.customermanagement;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.icu.text.StringPrepParseException;
import android.widget.Toast;

public class MyHelper extends SQLiteOpenHelper {
    private static final String dbname="mydb";
    private static final int version=1;
    public MyHelper(Context context){
        super(context,dbname,null,version);
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

        String sql="CREATE TABLE CUSTOMER ( _id INTEGER PRIMARY KEY AUTOINCREMENT , NAME TEXT , PHONENO TEXT , LENGHT TEXT , TEERA TEXT , BAZU TEXT , CHATI TEXT , QAMAR TEXT , SHALWAR TEXT , PAINCHA TEXT , GHEERA TEXT , FRONT TEXT , SIDE TEXT , HIP TEXT , BAN TEXT , COLLAR TEXT , ZIP TEXT , T_PAINCHA TEXT , OTHERS TEXT )";
        sqLiteDatabase.execSQL(sql);
    }


    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL("drop table if exists CUSTOMER");
        onCreate(sqLiteDatabase);

    }
    public Boolean insert_data(String name1, String phoneno1,String lenght1,String teera1,String bazu1,String chati1,String qamar1,String shalwar1,String paincha1,String gheera1,String front1,String side1,String hip1,String ban1,String collar1,String zip1,String t_paincha1,String others1) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues c = new ContentValues();
            long r = 0;

            c.put("NAME", name1);
            c.put("PHONENO", phoneno1);
            c.put("LENGHT", lenght1);
            c.put("TEERA", teera1);
            c.put("BAZU", bazu1);
            c.put("CHATI", chati1);
            c.put("QAMAR", qamar1);
            c.put("SHALWAR", shalwar1);
            c.put("PAINCHA", paincha1);
            c.put("GHEERA", gheera1);
            c.put("FRONT", front1);
            c.put("SIDE", side1);
            c.put("HIP", hip1);
            c.put("BAN", ban1);
            c.put("COLLAR", collar1);
            c.put("ZIP", zip1);
            c.put("T_PAINCHA", t_paincha1);
            c.put("OTHERS", others1);
             r = db.insert("CUSTOMER", null, c);


       

             if (r!=0)
        {
            return false;
        }
        else
        {
            return true;
        }

    }
}

'''

This is not an answer to your post.

Any reason for not using Room library by google? It is very easy/simple and reduces all this biolderplate code related to sqlite helper and cursors. For my personal project I always use Room.

check this codelab : https://developer.android.com/codelabs/android-room-with-a-view-kotlin#0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM