本文介绍了无法从Android上的SQLite数据库的任何数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DB帮手,做这样的功能:

 公开光标getCourseNames()抛出的SQLException {
    MDB = mDbHelper.getReadableDatabase();

    返回mDb.query(课程,空,COURSE_ROWID,NULL,NULL,NULL,NULL,NULL);
}
 

它是从长相拉像这样的表:

 私有静态最后弦乐COURSE_ID =CourseID;
私有静态最后弦乐COURSE_NAME =名称;
私有静态最后弦乐COURSE_ code =课code;
私有静态最后弦乐COURSE_ROWID =_id;


私有静态最后弦乐COURSE_CREATE =
    创建表+
    课程+(+
    COURSE_ROWID +整数主键自动增量,+
    COURSE_ID +的整数不为空,+
    COURSE_NAME +文字不为空,+
    COURSE_ code +的文字不为空+);
 

在我的主要活动我尝试这一点,得到一个空指针......

 公共无效buildCoursetoChapterList(){

    光标光标= dbHelper.getCourseNames();
    SimpleCursorAdapter适配器=新SimpleCursorAdapter(MainActivity.this,android.R.layout.simple_list_item_1,光标,NULL,NULL);

    ListView控件的ListView =(ListView控件)findViewById(R.id.list);

    listView.setAdapter(适配器);
}
 

任何人有一个想法,我的问题是什么?我把数据放到数据库早前:

 如果(dbHelper.checkCourseForData()!= NULL)
{
    的setContentView(R.layout.classlist);
}
其他
{
    dbHelper.addFirstClassToDb(course_ code,姓名,COURSE_ID);
    Log.d + I(课程加入到DB,course_ code ++姓名++ COURSE_ID);
}
 

尝试这样做,还不算什么,我想在课程选择所有名称值。不知道......失去希望。

 公开光标checkCourseForData()抛出的SQLException {
    的String []值= {COURSE_NAME};
    光标mCursor = mDb.query(课程,价值观,COURSE_ROWID +=+姓名,NULL,NULL,NULL,NULL,NULL);
    如果(!mCursor = NULL){mCursor.moveToFirst(); }

    返回mCursor;

}
 

解决方案

这应该是这样

 公开光标getCourseNames()抛出的SQLException {
    的String []值= {COURSE_NAME};
    MDB = mDbHelper.getReadableDatabase();

    返回mDb.query(课程,价值观,COURSE_ROWID,NULL,NULL,NULL,NULL,NULL);
}
 

说明:

在API的medthod已被定义为

所以,你需要通过相应的字符串。

用户我的例子,因为它为我的作品参考

 私人字符串名称;
私人字符串Events_Table =事件;
私有String []列= {_id,姓名,日期,TIME_SLOT,地点,详细信息,EHName,EHNumber};
私人字符串WhereClause =列[1] +=? ;

    光标光标= db.query(Events_Table,列,WhereClause,新的String [] {名},NULL,NULL,NULL);
 

考虑阅读本

参数

的表名编译根据查询。

的名单,其中列返回。传递null将返回所有列,这是不鼓励从存储是不会被使用prevent读取数据。

的过滤器宣称要返回的行,格式化为SQL WHERE子句(不含WHERE本身)。传递null将返回所有行对给定表。

selectionArgs两个您可以包括哪些?S在选择,这将是自selectionArgs两个,该值取代,以使他们出现在选择。该值将被绑定为字符串。

GROUPBY 的过滤器,宣布如何分组行,格式化为SQL GROUP BY子句(不包括GROUP BY本身)。传递null将导致行不进行分组。具有过滤申报包括光标这行组,如果行的分组被使用,格式化为SQL HAVING子句(不包括HAVING本身)。传递null将导致被包括所有行组,并且当行分组不被使用是必需的。

排序依据如何对行进行排序,格式化为一个SQL ORDER BY子句(不包括ORDER BY本身)。传递null将使用默认的排序顺序,这可能是无序的。

I have a DB helper that does this function:

public Cursor getCourseNames() throws SQLException {
    mDb = mDbHelper.getReadableDatabase();

    return mDb.query("Course",null, COURSE_ROWID, null, null, null, null, null);
}

The table it is pulling from looks like this:

private static final String COURSE_ID = "CourseID";
private static final String COURSE_NAME = "Name";
private static final String COURSE_CODE = "CourseCode";
private static final String COURSE_ROWID = "_id";


private static final String COURSE_CREATE =
    "create table " +
    "Course" + " ( " +
    COURSE_ROWID + " integer primary key autoincrement, " +
    COURSE_ID + "integer not null," +
    COURSE_NAME + "text not null, " +
    COURSE_CODE + "text not null" + ");";

In my main activity I try this and get a null pointer...

public void buildCoursetoChapterList(){

    Cursor cursor = dbHelper.getCourseNames();
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(MainActivity.this, android.R.layout.simple_list_item_1, cursor, null, null);

    ListView listView = (ListView) findViewById(R.id.list);

    listView.setAdapter(adapter);
}

Anyone have an idea what my problem is?I put data into the db earlier on:

if(dbHelper.checkCourseForData() !=null)
{
    setContentView(R.layout.classlist);
}
else
{
    dbHelper.addFirstClassToDb(course_code, name, course_id);
    Log.d+i("Course added to DB", course_code + " " + name + " " + course_id);
}

tried this and still nothing, I want to select all the Name values within Course.No clue... losing hope.

public Cursor checkCourseForData() throws SQLException {
    String[] values = {COURSE_NAME};
    Cursor mCursor = mDb.query("Course",values,COURSE_ROWID + "=" + "Name", null, null, null, null, null);
    if (mCursor != null) { mCursor.moveToFirst(); }

    return mCursor;

}
解决方案

It should be this

public Cursor getCourseNames() throws SQLException {
    String[] values = {COURSE_NAME};
    mDb = mDbHelper.getReadableDatabase();

    return mDb.query("Course",values,COURSE_ROWID, null, null, null, null, null);
}

Explanation :

the medthod in the api has been defined as

So you need to pass the strings accordingly.

User my example as a reference it works for me

    private String name;
private String Events_Table = "events";
private String[] Columns = {"_id", "Name", "Date", "Time_Slot", "Venue", "Details", "EHName", "EHNumber"} ;
private String WhereClause = Columns[1]+"=?" ;

    Cursor cursor = db.query(Events_Table, Columns, WhereClause, new String[] {name}, null, null, null);

Consider Reading this

Parameters

table The table name to compile the query against.

columns A list of which columns to return. Passing null will return all columns, which is discouraged to prevent reading data from storage that isn't going to be used.

selection A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table.

selectionArgs You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.

groupBy A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself). Passing null will cause the rows to not be grouped.having A filter declare which row groups to include in the cursor, if row grouping is being used, formatted as an SQL HAVING clause (excluding the HAVING itself). Passing null will cause all row groups to be included, and is required when row grouping is not being used.

orderBy How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.

这篇关于无法从Android上的SQLite数据库的任何数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:24