Android数据库未插入数据

时间:2013-04-01 11:05:20

标签: java android database sqlite

所以我有以下代码片段,代码试图将数据添加到数据库,如果不能关闭应用程序。无论我做什么,应用程序都会关闭,任何建议

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();
    }





}

如果我不添加异常,应用程序就会崩溃。我想最终让它与我传递给它的变量(editText1)一起工作,但即使我硬编码数据应用程序只是崩溃或关闭,根据天气或不是我添加例外。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

在你的sql语句中,我们正在查看两种类型的可能错误 -

1&GT;

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

此处您的表名为Characters

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

您的表格名称为Character

请检查一下。

2 - ;您的选择查询应为 -

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

应该是 -

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

请看这两个指针。我们将帮助您进一步发表意见。

由于