簡體   English   中英

將數據插入SQLITE數據庫

[英]Inserting data into SQLITE database

我正在嘗試在SQLITE數據庫中插入數據,但嘗試執行此操作時我的應用程序崩潰了,這是我要在最終點擊或單選按鈕上插入數據的代碼。 這是插入塊

public void onCheckedChanged(RadioGroup group, int checkedId) {
    // TODO Auto-generated method stub
    // Log.d("Questions", "Moving to next question");
    a++;
    /**
     * validate a checkbox has been selected
     */
    if (!checkAnswer())
        return;

    /**
     * check if end of game
     */
    if (currentGame.isGameOver()) {
         db.open();

        String total = currentGame.getRight() + "";
        Log.i("Sanket", total);
        db.insertOptions(topic1, total, mon);
        db.close();
        Intent i = new Intent(this, EndgameActivity.class);
        startActivity(i);
        a = 0;
        finish();
    } else {
        Intent i = new Intent(this, QuestionActivity.class);
        startActivity(i);
        finish();
    }
}

這是我的DatabaseAdapter類,我在這里定義了所有方法

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;

public class DBAdapter {


static final String topic = "topic";
static final String score = "score";
static final String month = "month";
static final String DATABASE_NAME = "questionDb";
static final String DATABASE_TABLE = "marks";
static final int DATABASE_VERSION = 1;
static final String DATABASE_CREATE = "create table marks"
        + "(topic text,score text,month text);";

final Context context;
DatabseHelper DBHelper;
SQLiteDatabase db;

public DBAdapter(Context context) {
    this.context = context;
    DBHelper = new DatabseHelper(context);
}

    private static class DatabseHelper extends SQLiteOpenHelper {

    public DatabseHelper(Context context) {
        // TODO Auto-generated constructor stub
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        Log.i("Sanket", "Upgrading database version from versin "
                + oldVersion + " to " + newVersion
                + " which may cause to destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS marks");
        onCreate(db);
    }

}


public DBAdapter open() {
    // TODO Auto-generated method stub
    db = DBHelper.getWritableDatabase();
    return this;
}

public void close() {
    // TODO Auto-generated method stub
    DBHelper.close();
}

public long insertOptions(String topic1, String mon,
        String total) {
    // TODO Auto-generated method stub
    ContentValues initialValues = new ContentValues();

    initialValues.put(topic, topic1);
    initialValues.put(score, mon);
    initialValues.put(month, total);

    return db.insert(DATABASE_TABLE, null, initialValues);

}

Cursor getAllQuestions() {
    // TODO Auto-generated method stub
    return db.query(DATABASE_TABLE, new String[] { topic, score, month },
            null, null, null, null, null);

}

long getNoOfRows() {
    String sql = "SELECT COUNT(*) FROM " + DATABASE_TABLE;
    SQLiteStatement statement = db.compileStatement(sql);
    long count = statement.simpleQueryForLong();
    return count;
}

Cursor getAlldata() {
    String selectQuery = ("select * from marks");
    Cursor cursor = db.rawQuery(selectQuery, null);

    return cursor;
}

}

請檢查我的代碼,讓我知道所需的更改

這是LOGCAT

02-04 13:06:57.259:E / AndroidRuntime(1114):致命異常:主02-04 13:06:57.259:E / AndroidRuntime(1114):java.lang.NullPointerException:println需要消息02-04 13 :06:57.259:E / AndroidRuntime(1114):在android.util.Log.println_native(本機方法)02-04 13:06:57.259:E / AndroidRuntime(1114):在android.util.Log.i(日志.java:159)02-04 13:06:57.259:E / AndroidRuntime(1114):at com.tmm.android.chuck.QuestionActivity.onCheckedChanged(QuestionActivity.java:195)02-04 13:06:57.259:E / AndroidRuntime(1114):位於android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)02-04 13:06:57.259:E / AndroidRuntime(1114):位於android.widget.RadioGroup.access $ 600(RadioGroup.java :54)02-04 13:06:57.259:E / AndroidRuntime(1114):位於android.widget.RadioGroup $ CheckedStateTracker.onCheckedChanged(RadioGroup.java:358)02-04 13:06:57.259:E / AndroidRuntime(1114 ):在android.widget.CompoundButton.setChecked(CompoundButton.java:129)02-04 13:06:57.259:E / AndroidRuntime(1114):在android.widget.CompoundButto n.toggle(CompoundButton.java:87)02-04 13:06:57.259:E / AndroidRuntime(1114):位於android.widget.RadioButton.toggle(RadioButton.java:76)02-04 13:06:57.259: E / AndroidRuntime(1114):在android.widget.CompoundButton.performClick(CompoundButton.java:99)02-04 13:06:57.259:E / AndroidRuntime(1114):在android.view.View $ PerformClick.run(View .java:17721)02-04 13:06:57.259:E / AndroidRuntime(1114):at android.os.Handler.handleCallback(Handler.java:730)02-04 13:06:57.259:E / AndroidRuntime(1114 ):在android.os.Handler.dispatchMessage(Handler.java:92)02-04 13:06:57.259:E / AndroidRuntime(1114):在android.os.Looper.loop(Looper.java:137)02- 04 13:06:57.259:E / AndroidRuntime(1114):位於android.app.ActivityThread.main(ActivityThread.java:5103)02-04 13:06:57.259:E / AndroidRuntime(1114):位於java.lang。 Reflection.Method.invokeNative(本機方法)02-04 13:06:57.259:E / AndroidRuntime(1114):at java.lang.reflect.Method.invoke(Method.java:525)02-04 13:06:57.259 :E / AndroidRuntime(1114):位於com.android.interna l.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)02-04 13:06:57.259:E / AndroidRuntime(1114):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553 )02-04 13:06:57.259:E / AndroidRuntime(1114):在dalvik.system.NativeStart.main(本機方法)

您尚未在DBAdapter類中創建marks

創建表

@Override
public void onCreate(SQLiteDatabase db) {

    // creating required tables
    db.execSQL(TABLE_NAME);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // on upgrade drop older tables
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

    // create new tables
    onCreate(db);
}

您的數據庫處理程序onCreate()為空。 您的數據庫沒有任何表。

將所需的CREATE TABLE SQL放在此處,然后卸載您的應用程序,以便刪除舊的空數據庫文件。


您添加到問題中的異常堆棧跟蹤“ println需要消息”來自此處:

String total = currentGame.getRight() + "";
Log.i("Sanket", total);

因為total是一個空字符串。 將日志記錄更改為類似

String total = currentGame.getRight() + "";
Log.i("Sanket", "total: " + total);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM