-1

So I have the following piece of code, the code trys to add data to a database and if can't it closes the app. No matter what I do the app just closes, any advice

package net.connormccarthy.walkingdeadcharacterprofiles;

import java.util.ArrayList;


import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class rickAddNotes extends Activity
{

SQLiteDatabase db;




    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        // this try catch block returns better error reporting to the log
            // Android specific calls
            super.onCreate(savedInstanceState);
            setContentView(R.layout.ricknotes);
            Button  btnLeft=(Button)findViewById(R.id.BtnRickSave);


            // create the database manager object
            //db = new MainActivity();


            final Button button = (Button) findViewById(R.id.BtnRickSave);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    data();
                }
            });      


    }


    public long data()
    {
        long d=0;
      //EditText edittext1=(EditText )findViewById(R.id.editText1);
      //String notes = edittext1.toString();


try {

        final String Insert_Data="INSERT INTO Characters VALUES(2,'WOOP','5')";
        db.execSQL(Insert_Data);
    } catch (Exception e) {

        System.exit(0);

    }
    return d;

    }


    public void showdata(View view)
    {
        Cursor c=db.rawQuery("SELECT * from Character WHERE character = rick", null);
         int count= c.getCount();
        c.moveToFirst();
        TableLayout tableLayout = new TableLayout(getApplicationContext());
        tableLayout.setVerticalScrollBarEnabled(true);
       TableRow tableRow;
       TextView textView,textView1,textView2,textView3,textView4,textView5;
       tableRow = new TableRow(getApplicationContext());
       textView=new TextView(getApplicationContext());
       textView.setText("Firstname");
       textView.setTextColor(Color.RED);
        textView.setTypeface(null, Typeface.BOLD);
         textView.setPadding(20, 20, 20, 20);
        tableRow.addView(textView);
        textView4=new TextView(getApplicationContext());
        textView4.setText("LastName");
        textView4.setTextColor(Color.RED);
        textView4.setTypeface(null, Typeface.BOLD);
         textView4.setPadding(20, 20, 20, 20);
        tableRow.addView(textView4);
        textView5=new TextView(getApplicationContext());
        textView5.setText("Email");
        textView5.setTextColor(Color.RED);
        textView5.setTypeface(null, Typeface.BOLD);
        textView5.setPadding(20, 20, 20, 20);
        tableRow.addView(textView5);
       tableLayout.addView(tableRow);
         for (Integer j = 0; j < count; j++)
         {
             tableRow = new TableRow(getApplicationContext());
             textView1 = new TextView(getApplicationContext());
             textView1.setText(c.getString(c.getColumnIndex("character")));
             textView2 = new TextView(getApplicationContext());
             textView2.setText(c.getString(c.getColumnIndex("notes")));
             textView1.setPadding(20, 20, 20, 20);
             textView2.setPadding(20, 20, 20, 20);
             tableRow.addView(textView1);
             tableRow.addView(textView2);
             tableLayout.addView(tableRow);
             c.moveToNext() ;
         }
         setContentView(tableLayout);
    db.close();
    }





}

If I dont add the exception the app just crashes. I want to ultimately get it to work with a variable I pass to it (editText1) but even when I hard code data the app just crashes or closes, depending on weather or not I add the exception.

Any help is greatly appreciated.

Shiv
  • 4,539
  • 3
  • 23
  • 39
  • 1
    Please format your code properly so we don't have to scroll around and post the stack trace from logcat. – Simon Apr 01 '13 at 11:06
  • first you are not initializing `db` instance of `SQLiteDatabase` before performing DB operations and second add logcat result with question to get more help – ρяσѕρєя K Apr 01 '13 at 11:14

1 Answers1

0

in your sql statement, we are viewing 2 types of probable errors -

1>

final String Insert_Data="INSERT INTO Characters VALUES(2,'WOOP','5')";

Here your table name is Characters

where as in

Cursor c=db.rawQuery("SELECT * from Character WHERE character = rick", null);

your table name is Character

Please check this.

2> Your select query should be -

Cursor c=db.rawQuery("SELECT * from Character WHERE character = rick", null);

should be -

Cursor c=db.rawQuery("SELECT * from Character WHERE character = 'rick'", null);

Please look at these 2 pointers. We will help you for any further comments.

Thanks

Piyas De
  • 1,778
  • 15
  • 27