Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从SQLite数据库读取数据?_Java_Android_Sqlite_Cursor - Fatal编程技术网

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

Java 如何从SQLite数据库读取数据?,java,android,sqlite,cursor,Java,Android,Sqlite,Cursor,我试图读取一行使用列名作为键,但不知何故,我无法获得它 示例我输入了一个字符串“name” 列表仅在ListView中显示“1” 这是我的插入代码 public void insertStudent(String name, int faculty){ ContentValues cv = new ContentValues(); cv.put("name", name); cv.put("faculty", faculty); m

我试图读取一行使用列名作为键,但不知何故,我无法获得它

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

列表仅在ListView中显示“1”

这是我的插入代码

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;
    }
public List selectAllEngg(){
列出所有学生=新建ArrayList();
Cursor Cursor=mSqliteDatabase.query(“学生”,新字符串[]{“教员”},null,null,null,null);
if(cursor!=null&&cursor.moveToFirst()){
做{
allStudents.add(cursor.getString(0));
}while(cursor.moveToNext());
}
归还所有学生;
}

封装测试;
导入android.content.ContentValues;
导入android.content.Context;
导入android.database.Cursor;
导入android.database.sqlite.SQLiteDatabase;
导入android.database.sqlite.SQLiteOpenHelper;
导入java.util.ArrayList;
导入java.util.List;
公共类MyDBAdapter{
公共静态最终字符串数据库\u NAME=“data”;
私有上下文;
私人MyDBHelper-mDbHelper;
私有SQLiteDatabase-mSqliteDatabase;
私有int数据库_VERSION=1;
公共MyDBAdapter(上下文){
this.mContext=上下文;
mDbHelper=新的MyDBHelper(上下文、数据库名称、null、数据库版本);
}
公开作废{
mSqliteDatabase=mDbHelper.getWritableDatabase();
}
公共类MyDBHelper扩展了SQLiteOpenHelper{
公共MyDBHelper(上下文上下文、字符串名称、SQLiteDatabase.CursorFactory工厂、int版本){
超级(上下文、名称、工厂、版本);
}
@凌驾
public void onCreate(SQLiteDatabase db){
String query=“创建表students(id integer主键自动递增,name text,faculty integer);”;
execSQL(查询);
}
@凌驾
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
String query=“如果存在,则删除表;”;
execSQL(查询);
onCreate(db);
}
}
public void insertStudent(字符串名称,int){
ContentValues cv=新的ContentValues();
简历(姓名);
简历put(“教员”,教员);
mSqliteDatabase.insert(“学生”,null,cv);
}
//公共列表selectAllStudents(){
//列出所有学生=新建ArrayList();
//Cursor Cursor=mSqliteDatabase.query(“学生”,null,null,null,null,null);
//if(cursor!=null&&cursor.moveToFirst()){
//做{
//add(cursor.getString(1));
//}while(cursor.moveToNext());
//        }
//归还所有学生;
//    }
公共列表selectAllEngg(){
列出所有学生=新建ArrayList();
Cursor Cursor=mSqliteDatabase.query(“学生”,新字符串[]{“教员”},null,null,null,null);
if(cursor!=null&&cursor.moveToFirst()){
做{
if(cursor.getString(cursor.getColumnIndex(“函数”)).equals(“1”)){
add(cursor.getString(cursor.getColumnIndex(“name”));
}
//allStudents.add(cursor.getString(0));
}while(cursor.moveToNext());
}
归还所有学生;
}
公共空间删除所有工程师(){
mSqliteDatabase.delete(“学生”,null,null);
}
}

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

使用
游标。getString(0)
是(IMO)错误的做法。这假设您确实知道列索引,虽然您可以相当容易地找到它,但最好这样做:

allStudents.add(cursor.getString(cursor.getColumnIndex("name")));
不过,最好将列名声明为静态常量,但如果您写对了名称,这仍然有效


“1”
作为输出的原因很可能是因为
0
处的列没有指向name列

在游标中使用零。getString(0)返回1,因为您的查询只包含一列“faculty”,该列是符合表规范的整数。但同意Zoe的观点,这是一种糟糕的做法,会导致代码维护和调试困难

为了使查询具有名称和功能,您需要在查询的列数组字符串中同时包含这两个内容,我建议采用这种方式(更可读的代码):

或者你的方式:

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"});
ArrayList allStudents=new ArrayList();
add(新字符串[]{“放在这里的整数”,“放在这里的学生名”});

希望这能对您有所帮助。

Okay的可能副本这是它的工作原理:我有一个用户输入姓名的编辑文本,我还有一个微调器,用户可以在其中选择教员(工程、商业和艺术)。这里有一个按钮,你可以将信息保存到数据库中。我正在尝试获取每个学院的名字列表,但我似乎不知道该怎么做?有什么想法吗?
Cursor cursor = mSqliteDatabase.query("students", new String[]  {"name","faculty"}, null, null, null, null, null);
ArrayList<String[]> allStudents  = new ArrayList();
allStudents.add(new String[] {"put here faculty integer","put here student name"});