-2

My application crashes down as I create a database. Here's my code

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

    //Step 1:Create DB and table
    final SQLiteDatabase 
 db=openOrCreateDatabase("Test_db",MODE_PRIVATE,null);
    String c_query="create table if not exists clients(sname text,add 
 text,order text,price text, ddate text)";
    db.execSQL(c_query);
    Toast.makeText(getBaseContext(),"DB and Table 
  created",Toast.LENGTH_LONG).show();

    //Step 2: Object Declaration

    final EditText sname=(EditText)findViewById(R.id.editText);
    final EditText add=(EditText)findViewById(R.id.editText2);
    final EditText order=(EditText)findViewById(R.id.editText3);
    final EditText price=(EditText)findViewById(R.id.editText4);
    final EditText ddate=(EditText)findViewById(R.id.editText5);
    Button b=(Button)findViewById(R.id.button);

    //Step 4:Click event of button

    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Step 5: get the value of Objects

            String ssname=sname.getText().toString();
            String sadd=add.getText().toString();

            String sorder=order.getText().toString();
            String sprice=price.getText().toString();
            String sddate=ddate.getText().toString();

            //Step 6: Execute Insert query

            String i_query= "insert into clients values ('"+ssname+"','"+sadd+"','"+sorder+"','"+sprice+"','"+sddate+"')";
            db.execSQL(i_query);
            Toast.makeText(getBaseContext(),"Record 
  Inserted",Toast.LENGTH_LONG).show();


        }
    });



}
Zoe
  • 27,060
  • 21
  • 118
  • 148
H.Patel
  • 1
  • 1
  • 7

2 Answers2

1

add and order are sqlite keywords and cannot be used as column names without quoting. Consider renaming those columns.

laalto
  • 150,114
  • 66
  • 286
  • 303
0

Please update your query with the below-mentioned query and it will not crash.

String c_query="create table if not exists clients(sname text,'add' text,'order' text,price text, ddate text)";

Mohit patel
  • 296
  • 1
  • 8