#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Not adding data to database


    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
    Code:
    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.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    > db.close();
    Why do you close the database after printing the current contents?

    > catch (Exception e)
    Why don't you print the exception to the error log (so you get some useful information), rather than just bombing out without leaving you with any clues at all.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by salem
    > db.close();
    Why do you close the database after printing the current contents?

    > catch (Exception e)
    Why don't you print the exception to the error log (so you get some useful information), rather than just bombing out without leaving you with any clues at all.
    Thanks for the reply. I am pretty new to Android so how would I print and view the log?

    Thanks again.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0
    I got this as a log.

    Code:
    04-02 10:29:21.210: E/AndroidRuntime(1383): at net.connormccarthy.walkingdeadcharacterprofiles.rickAddNotes.insertdata(rickAddNotes.java:48)
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    Well rickAddNotes.java:48 indicates line 48 in the file.

    If that's the file you posted, then line 48 is a blank line, which doesn't make much sense.

    Are you using Eclipse to develop this software, using either an emulator or a handset plugged into the USB?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo