Android SQLite值返回null除了第一个,为什么?

时间:2014-08-27 22:09:13

标签: android sql sqlite

试图理解在Android中使用SQL命令,到目前为止我认为我没有创建表或插入int数据的问题,但我似乎只能得到表中的第一个值,其余的看起来像是空的。

主要活动:

package com.ebook.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class HelloActivity extends Activity {
    ScoreDB scoreDB;
    String[] results;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_hello);
        TextView tv = (TextView)findViewById(R.id.sqlText);
        CursorFactory cf = null;
        scoreDB = new ScoreDB(this, "scoreDB", cf);
        scoreDB.addScore(3);
        scoreDB.addScore(10);
        results = scoreDB.getAllScores();
        showResults(tv, results);
    }

    public void showResults(TextView tv, String[] results){
        String str = "";
        for (String s: results){
            str+="Score: "+s+"\n";
        }
        tv.setText(str);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.hello, menu);
        return true;
    }

}

我的数据库课程:

package com.ebook.helloworld;

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

public class ScoreDB extends SQLiteOpenHelper{
    private static final int VERSION = 1;
    private static final String KEY_SCORE = "Score";
    private static final String SCORE_TABLE = "ScoreTable";

    public ScoreDB(Context context, String name, CursorFactory factory) {
        super(context, name, factory, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        createTable(db);
    }

    public void createTable(SQLiteDatabase db){
        String createScoreTable = "CREATE TABLE "+SCORE_TABLE+ "(" +
                "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_SCORE+" TEXT" +
                ");";
        db.execSQL(createScoreTable);
    }

    public void addScore(int score){
        SQLiteDatabase db = this.getReadableDatabase();
        String insert = "INSERT INTO "+SCORE_TABLE+" VALUES (null, "+score+");";
//      ContentValues values = new ContentValues();
//      values.put(KEY_SCORE, score);
//      db.insert(SCORE_TABLE, null, values);
        db.execSQL(insert);
        db.close();
    }

    public void deleteAllScores(){
        SQLiteDatabase db = this.getWritableDatabase();
        String delete = "DELETE FROM "+SCORE_TABLE+";";
        db.execSQL(delete);
        db.close();
    }

    public String[] getAllScores() {

        // Select All Query
        String selectQuery = "SELECT  * FROM " + SCORE_TABLE+";";

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list

        int i = 0;

        String[] data = new String[cursor.getCount()];

        while (cursor.moveToNext()) {

            //changed from 1 to 0
            data[i] = cursor.getString(0);

            i = i++;

        }
        cursor.close();
        db.close();
        // return score array
        return data;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String dropSQL = "DROP TABLE IF EXISTS"+SCORE_TABLE+";"; 
        db.execSQL(dropSQL);
        createTable(db);

    }

}

它没有输出错误所以我不确定出了什么问题。

2 个答案:

答案 0 :(得分:1)

尝试使用:

if (cursor.moveToFirst()) {
        do {
            data[i] = cursor.getString(1);

           i++;

        } while (cursor.moveToNext());
}

答案 1 :(得分:0)

该行

i = i++;
  1. 读取i的值,然后
  2. 增加i,然后
  3. 将步骤1中读取的旧值分配给i
  4. 只做增量:

    i++;