通过Android中的Edittext字段插入数据库

时间:2012-06-29 09:26:21

标签: android database button android-edittext

我是Android的新手。我无法将数据插入我的数据库。虽然它通过editText字段接受文本,但它没有保存在数据库中。

我想使用XML按钮的android:onClick属性将数据插入数据库表。

当我点击我的保存按钮时,我的应用程序立即崩溃

// InterviewActivity.java

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
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;
import android.widget.RemoteViews.ActionException;
import android.widget.Toast;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;

public class InterviewActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcomeactivity);

    }

    public void onQuestionClicked(View view) {
        EditText Q = (EditText)findViewById(R.id.Questiont);
        EditText A = (EditText)findViewById(R.id.Answert);    

        Q.setVisibility(View.VISIBLE);
        A.setVisibility(View.INVISIBLE);

        Toast.makeText(this, "Enter your Question here", Toast.LENGTH_LONG).show();
    }
    public void onAnswerClicked(View view) {
        EditText Q = (EditText)findViewById(R.id.Questiont);
        EditText A = (EditText)findViewById(R.id.Answert);    

        A.setVisibility(View.VISIBLE);
        Q.setVisibility(View.INVISIBLE);

        Toast.makeText(this, "Enter your Answer here", Toast.LENGTH_LONG).show();
    }

    public void onSave(View view) {
        EditText Q = (EditText)findViewById(R.id.Questiont);
        EditText A = (EditText)findViewById(R.id.Answert);    

        String q = Q.getText().toString();
        String a = A.getText().toString();

        Toast.makeText(this,"Question :"+" " + q + "\n" +
                "Answer :"+" " + a, Toast.LENGTH_LONG).show();

    }


    public void onCancel(View view) {
        finish();
    }
}

// trying.java

package com.ques.ans;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class trying extends SQLiteOpenHelper{

    public static final String DB_NAME = "save.db";
    public static final int DB_VERSION=2;
    public static final String TABLE="qnadb";
    public static final String SNO = "Sno";
    public static final String QUESTION = "Question";
    public static final String ANSWER = "Answer";   

    public trying(Context context) {
        super(context,DB_NAME, null, DB_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table TABLE (SNO integer autoincrement primary key not null, QUESTION text , ANSWER text);";
        // TODO Auto-generated method stub
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE);
        onCreate(db);
    }

    public void onsaving(String q,String a) {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put(SNO, 1);
        cv.put(QUESTION, q);
        cv.put(ANSWER, a);
        db.insert(TABLE,null, cv);
        db.close();
    }
}

1 个答案:

答案 0 :(得分:0)

所以您的PK(主键)使用AUTOICREMENT,因此您无法将其添加到数据库中。 尝试使用以下代码片段修复您的方法:

ContentValues cv = new ContentValues();
   cv.put(QUESTION, q);
   cv.put(ANSWER, a);
   db.insert(TABLE, null, cv);

这也是我认为错的。您将表名定义为TABLE

String sql = "create table TABLE (SNO integer autoincrement primary key not null, QUESTION text , ANSWER text);";

您的static variable的值 gnadb 。这是不正确的,不是吗?

public static final String TABLE = "qnadb";

因此,请将您的表名更改为

String sql = "create table qnadb (SNO integer autoincrement primary key not null, QUESTION text , ANSWER text);

或更改TABLE变量的值。


所以现在尝试改变这些建议然后它应该有效。