如何使用这个demo从数据库中获取数据?

发布于 2025-01-01 12:14:19 字数 344 浏览 2 评论 0原文

我正在使用 这个示例在 amy android 应用程序中创建数据库。

但这里没有给出如何从数据库中获取数据。

我的字段如下:

_id

名称

地址

类型

现在我想显示数据库中的所有可用名称。

我知道我必须使用选择查询。但我不知道在哪里必须使用它。所以请任何伙伴可以帮助我们解决这个问题。

谢谢。

I am using this example to create the database in to amy android app.

But here it is not given that how to fetch the data from the database.

My fields are like:

_id

name

address

type

now i want to show the all available name from the database.

I know that i have to use Select query. but where i have to use it that i dont know. So please can anybudy help e for that.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

单身狗的梦 2025-01-08 12:14:19

这里有有关 Android 数据的精彩教程.本教程涵盖了数据库的各个方面。

Here's brilliant tutorial about android data.The tutorial covers all the aspects of database.

我早已燃尽 2025-01-08 12:14:19

DataBaseHelper 中编写您的方法,如下所示

public ArrayList<User> getUsersList() {

        ArrayList<User> userList = null;
        Cursor cursor = null;

        try {

            String queryString = "SELECT _id , name, address, type FROM User";

            cursor =  myDataBase.rawQuery(queryString, null);

            if (cursor != null && cursor.moveToFirst()) {
                userList = new ArrayList<User>();
                do {
                    User nextUser = new User(cursor.getInt(0),
                            cursor.getString(1), cursor.getString(2) , cursor.getString(3));
                    userList.add(nextUser);
                } while (cursor.moveToNext());
            }
        } catch (Exception e) {
            e.printStackTrace();
            userList = null;
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.deactivate();
                cursor.close();
                cursor = null;
            }
            if(myDataBase != null){
                myDataBase.close();
            }
        }
        return userList;
    }

Write your method in DataBaseHelper that will be like this

public ArrayList<User> getUsersList() {

        ArrayList<User> userList = null;
        Cursor cursor = null;

        try {

            String queryString = "SELECT _id , name, address, type FROM User";

            cursor =  myDataBase.rawQuery(queryString, null);

            if (cursor != null && cursor.moveToFirst()) {
                userList = new ArrayList<User>();
                do {
                    User nextUser = new User(cursor.getInt(0),
                            cursor.getString(1), cursor.getString(2) , cursor.getString(3));
                    userList.add(nextUser);
                } while (cursor.moveToNext());
            }
        } catch (Exception e) {
            e.printStackTrace();
            userList = null;
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.deactivate();
                cursor.close();
                cursor = null;
            }
            if(myDataBase != null){
                myDataBase.close();
            }
        }
        return userList;
    }
千紇 2025-01-08 12:14:19

您可以从我的电脑下载一个示例。这是链接。仅限今天。我还向您提供了其代码,如下所示。

package com.collabera.labs.sai.db;

import java.util.ArrayList;

import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;

public class CRUDonDB extends ListActivity {

    private final String SAMPLE_DB_NAME = "myFriendsDb";
    private final String SAMPLE_TABLE_NAME = "friends";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        ArrayList<String> results = new ArrayList<String>();
        SQLiteDatabase sampleDB = null;

        try {
            sampleDB =  this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);

            sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                    SAMPLE_TABLE_NAME +
                    " (LastName VARCHAR, FirstName VARCHAR," +
                    " Country VARCHAR, Age INT(3));");

            sampleDB.execSQL("INSERT INTO " +
                    SAMPLE_TABLE_NAME +
                    " Values ('Makam','Sai Geetha','India',25);");
            sampleDB.execSQL("INSERT INTO " +
                    SAMPLE_TABLE_NAME +
                    " Values ('Chittur','Raman','India',25);");
            sampleDB.execSQL("INSERT INTO " +
                    SAMPLE_TABLE_NAME +
                    " Values ('Solutions','Collabera','India',20);");

            Cursor c = sampleDB.rawQuery("SELECT FirstName, Age FROM " +
                    SAMPLE_TABLE_NAME +
                    " where Age > 10 LIMIT 5", null);

            if (c != null ) {
                if  (c.moveToFirst()) {
                    do {
                        String firstName = c.getString(c.getColumnIndex("FirstName"));
                        int age = c.getInt(c.getColumnIndex("Age"));
                        results.add("" + firstName + ",Age: " + age);
                    }while (c.moveToNext());
                } 
            }

            this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));

        } catch (SQLiteException se ) {
            Log.e(getClass().getSimpleName(), "Could not create or Open the database");
        } finally {
            if (sampleDB != null) 
                sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAME);
                sampleDB.close();
        }
    }
}

you can download one sample example from my computer. This is link. It will be available for today only. I am also giving you its code as below.

package com.collabera.labs.sai.db;

import java.util.ArrayList;

import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;

public class CRUDonDB extends ListActivity {

    private final String SAMPLE_DB_NAME = "myFriendsDb";
    private final String SAMPLE_TABLE_NAME = "friends";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        ArrayList<String> results = new ArrayList<String>();
        SQLiteDatabase sampleDB = null;

        try {
            sampleDB =  this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);

            sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                    SAMPLE_TABLE_NAME +
                    " (LastName VARCHAR, FirstName VARCHAR," +
                    " Country VARCHAR, Age INT(3));");

            sampleDB.execSQL("INSERT INTO " +
                    SAMPLE_TABLE_NAME +
                    " Values ('Makam','Sai Geetha','India',25);");
            sampleDB.execSQL("INSERT INTO " +
                    SAMPLE_TABLE_NAME +
                    " Values ('Chittur','Raman','India',25);");
            sampleDB.execSQL("INSERT INTO " +
                    SAMPLE_TABLE_NAME +
                    " Values ('Solutions','Collabera','India',20);");

            Cursor c = sampleDB.rawQuery("SELECT FirstName, Age FROM " +
                    SAMPLE_TABLE_NAME +
                    " where Age > 10 LIMIT 5", null);

            if (c != null ) {
                if  (c.moveToFirst()) {
                    do {
                        String firstName = c.getString(c.getColumnIndex("FirstName"));
                        int age = c.getInt(c.getColumnIndex("Age"));
                        results.add("" + firstName + ",Age: " + age);
                    }while (c.moveToNext());
                } 
            }

            this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));

        } catch (SQLiteException se ) {
            Log.e(getClass().getSimpleName(), "Could not create or Open the database");
        } finally {
            if (sampleDB != null) 
                sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAME);
                sampleDB.close();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文