DBAdapter插入数据

时间:2014-05-03 18:28:37

标签: java android sqlite insert

我得到了DBAdapter.java,我创建了DB等等。我得到了以下内容(当我调用它时,它应该将数据存入数据库):

public long insertRow(String valorglicemico, String datahora) { 

        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_VALORGLICEMICO, valorglicemico);
        initialValues.put(KEY_DATAHORA, datahora);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

现在,在我的mainActivity上,我调用之前的东西,我有一个等待某事的按钮:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diab2014_activity_1);           


        // enable the home button
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeButtonEnabled(true);

        btn1 = (Button)findViewById(R.id.button1);          

    value = (EditText)findViewById(R.id.editText2);

    btn1.setOnClickListener(new View.OnClickListener() {        

     public void onClick(View v) { 

                     String a = value.getText().toString();
                     String b = "";

                     myDB.insertRow(a,  b);
                }
    });

logcat的:

E/AndroidRuntime(1267): FATAL EXCEPTION: main
E/AndroidRuntime(1267): Process: com.example.project, PID: 1267
E/AndroidRuntime(1267): java.lang.NullPointerException
E/AndroidRuntime(1267):     at com.example.projdiab2014.Diab2014Activity_1$1.onClick(mainActivity.java:53)
E/AndroidRuntime(1267):     at android.view.View.performClick(View.java:4438)
E/AndroidRuntime(1267):     at android.view.View$PerformClick.run(View.java:18422)
E/AndroidRuntime(1267):     at android.os.Handler.handleCallback(Handler.java:733)
E/AndroidRuntime(1267):     at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(1267):     at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(1267):     at android.app.ActivityThread.main(ActivityThread.java:5017)
E/AndroidRuntime(1267):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1267):     at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(1267):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
E/AndroidRuntime(1267):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
E/AndroidRuntime(1267):     at dalvik.system.NativeStart.main(Native Method)

我忘记了什么吗?

1 个答案:

答案 0 :(得分:0)

你在这里定义myDb?数据库应该得到一个上下文参数,但我看不到定义。这可能是你的问题。在执行db操作时也总是使用try catch。这里是一个定义数据库帮助器并将记录插入table的示例。我尝试了代码并且它正在运行。

public class DbHelper extends SQLiteOpenHelper {

public static final String TAG = "db";
public static final int VERSION = 1;

public DbHelper(Context context) {
    super(context, "mydb", null, VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE IF NOT EXISTS " + "MY_TABLE"
            + " (NAME TEXT PRIMARY KEY, " + "SURNAME TEXT);");
}

public long insertRow(String valorglicemico, String datahora) {
    final SQLiteDatabase db = this.getWritableDatabase();
    ContentValues initialValues = new ContentValues();
    long retval = 0;
    try {
        initialValues.put("NAME", valorglicemico);
        initialValues.put("SURNAME", datahora);
        retval = db.insert("MY_TABLE", null, initialValues);
    } catch (Exception e) {
        Log.e(TAG, "insertRow exception", e);
    } finally {
        db.close();
    }
    return retval;
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}
}

在活动课上

dbHelper = new DbHelper(this);
name = (EditText) findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    String a = name.getText().toString();
    String b = "";
    long val = dbHelper.insertRow(a, b);
    Log.d("DB", val+"");
    }
});