English 中文(简体)
从 SQLite 数据库获取数据到字符串和机器人
原标题:Getting data from SQLite database to String Android

我试图从我的数据库中获取信息, 并将其放入一个字符串, 以在屏幕上打印。 我以为我可以使用下面的代码来做, 但是它会提供光标的一些信息, 而不是光标内的信息 。

datasource = new DataBaseHelper(this);
datasource.open();
Cursor c = datasource.getAllGoals();
startManagingCursor(c);
String g = c.toString();
goal.setText(g);
datasource.close();
最佳回答

光标可以被视为某些基本数据的指针。 在光标对象上运行 < code> c.toString () 将打印默认的 Cursor 类执行字符串表达式( 一个签名字符 和该对象散列代码的未签名十六进制表示), 这不是你想要的 。

要检索基本数据库数据, 您需要调用 c. get String( clunnIndex) ( source ), 或该特定列索引所需的任何列数据类型 。

这里举个例子从来源修改:

说你创造了一张桌子

private static final String DATABASE_CREATE = 
            "create table comments ( "
            + "_id integer primary key autoincrement, "
            + "comment text not null);";

您的 getalles 函数返回一个光标, 该光标指向有关 comment 的数据。 现在你只想显示有关 comment 列的细节。 所以, 您必须运行 c. getString(1) 。 假设您的 getallgoles 函数返回只指向 comment 列的数据的光标。 现在您必须运行 c. gettring(0)

我建议您下载所提供的示例中的源代码,并查看如何从光标中提取数据。

<强度 > EDIT:

    public List<Comment> getAllComments() {
        List<Comment> comments = new ArrayList<Comment>();

        Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
                allColumns, null, null, null, null, null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {//retrieve data from multiple rows
            Comment comment = cursorToComment(cursor);
            comments.add(comment);
            cursor.moveToNext();
        }
        // Make sure to close the cursor
        cursor.close();
        return comments;
    }

    private Comment cursorToComment(Cursor cursor) {
        Comment comment = new Comment();
        comment.setId(cursor.getLong(0));
        comment.setComment(cursor.getString(1));
        return comment;
    }

来源

问题回答
openDataBase();
Cursor c = myDataBase.rawQuery("SELECT * FROM tb_aa_City where City_Name =  "+cityname+" ", null);
if (c.getCount() > 0) {
    c.moveToFirst();
    seqid = c.getString(4);
    System.out.println("In DB..getSequenceID..."+seqid);
    }
    c.close();
    myDataBase.close();
    SQLiteDatabase.releaseMemory();




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...