如何从SQLite数据库中读取数据?

时间:2017-12-09 10:34:34

标签: java android sqlite cursor

我正在尝试使用列名作为键来读取一行,但我无法得到它。

示例我输入了一个字符串" name"

列表只显示" 1"在ListView

这是我的插入代码

public void insertStudent(String name, int faculty){
        ContentValues cv = new ContentValues();
        cv.put("name", name);
        cv.put("faculty", faculty);
        mSqliteDatabase.insert("students", null, cv);
    }

以下是我查询数据的方式:

public List<String> selectAllEngg(){
        List<String> allStudents = new ArrayList<>();
        Cursor cursor = mSqliteDatabase.query("students", new String[]{"faculty"}, null, null, null, null, null);
        if(cursor != null && cursor.moveToFirst()){
            do {

                allStudents.add(cursor.getString(0));
            } while (cursor.moveToNext());
        }
        return allStudents;
    }

&#13;
&#13;
package test;

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

import java.util.ArrayList;
import java.util.List;

public class MyDBAdapter {
    public static final String DATABASE_NAME = "data";

    private Context mContext;
    private MyDBHelper mDbHelper;
    private SQLiteDatabase mSqliteDatabase;
    private int DATABASE_VERSION = 1;

    public MyDBAdapter(Context context){
        this.mContext = context;
        mDbHelper = new MyDBHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public void open(){
        mSqliteDatabase = mDbHelper.getWritableDatabase();
    }

    public class MyDBHelper extends SQLiteOpenHelper{
        public MyDBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db){
            String query = "CREATE TABLE students(id integer primary key autoincrement, name text, faculty integer);";
            db.execSQL(query);
        }

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

    public void insertStudent(String name, int faculty){
        ContentValues cv = new ContentValues();
        cv.put("name", name);
        cv.put("faculty", faculty);
        mSqliteDatabase.insert("students", null, cv);
    }

//    public List<String> selectAllStudents(){
//        List<String> allStudents = new ArrayList<>();
//        Cursor cursor = mSqliteDatabase.query("students", null, null, null, null, null, null);
//        if(cursor != null && cursor.moveToFirst()){
//            do {
//                allStudents.add(cursor.getString(1));
//            } while (cursor.moveToNext());
//        }
//        return allStudents;
//    }

    public List<String> selectAllEngg(){
        List<String> allStudents = new ArrayList<>();

        Cursor cursor = mSqliteDatabase.query("students", new String[]{"faculty"}, null, null, null, null, null);
        if(cursor != null && cursor.moveToFirst()){
            do {
                if(cursor.getString(cursor.getColumnIndex("faculty")).equals("1")){
                    allStudents.add(cursor.getString(cursor.getColumnIndex("name")));
                }
//                allStudents.add(cursor.getString(0));

            } while (cursor.moveToNext());
        }
        return allStudents;
    }

    public void deleteAllEngineers(){
        mSqliteDatabase.delete("students", null, null);
    }
}
&#13;
&#13;
&#13;

我添加了我的DB java文件,请看。

2 个答案:

答案 0 :(得分:2)

在cursor.getString(0)中使用零返回1,因为您的查询只包含一个列“faculty”,根据您的表规范,它是整数。但同意Zoe的观点,这是不好的做法,导致难以维护和调试代码。

为了查询名称和教师,您需要在查询的列数组字符串中包含这两个查询,我推荐这种方式(代码更易读):

String[] columns = {"name","faculty"};
Cursor cursor = mSqliteDatabase.query("students", columns, null, null, null, null, null);

或你的方式:

Cursor cursor = mSqliteDatabase.query("students", new String[]  {"name","faculty"}, null, null, null, null, null);

通过这种方式,您需要将arraylist更改为地图(例如,接受键和值的集合)或将表列实现为类并使用该类类型的arraylist,或者创建类型字符串列表数组如:

ArrayList<String[]> allStudents  = new ArrayList();
allStudents.add(new String[] {"put here faculty integer","put here student name"});
希望这对你有所帮助。

答案 1 :(得分:0)

使用cursor.getString(0)是(IMO)不良做法。这假设你实际上知道列索引,虽然你可以很容易地找到它,但最好这样做:

allStudents.add(cursor.getString(cursor.getColumnIndex("name")));

最好将列名称声明为静态常量,但这仍然有效,假设您将名称写为正确。

您输出"1"作为输出的原因很可能是因为0处的列没有指向名称列。