有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从数据库中获取所有值,并将其存储在字符串数组中

我制作了一个蔬菜类,在这个类中,我将从数据库类中获取所有数据,并且我需要将数据存储在一个字符串数组中。 假设我有洋葱,土豆,在数据库里有50,80的价格。 现在我需要从数据库中获取这些值,并作为

String items[] = {"onion","potato"};
String price[] = {"50","80"};

我的主要课程如下:

package com.ku.bazzar;

public class VegetableActivity extends Activity {
//String items[];
//String price[];

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.vegetables_info);

我尝试了以下方法:

Vegetablesdatabase info = new Vegetablesdatabase(this);
        info.open();
        items[] = { info.getvegetable();}
     price[]= { info.getprice();}
        info.close();

我知道这是错误的:

 items[] = { info.getvegetable();}
         price[]= { info.getprice();}

所以任何人都可以教我制作商品和价格的字符串数组,并在我的vegetabledatabase文件中创建方法getvegetable()getprice()

我制作了一个数据库类,如下所示

    package com.ku.bazzar;


    public class Vegetablesdatabase {

    public static final String KEY_ROWID = "_id";
    public static final String KEY_VEGETABLES = "vegetables";
    public static final String KEY_PRICE = "price";

    private static final String DATABASE_NAME="ITEM_VEGETABLES";
    private static final String DATABASE_TABLE="VEGETABLES";
    private static final int DATABASE_VERSION= 1;

    private DbHelper ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourdatabase;

    private static class DbHelper extends SQLiteOpenHelper{

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);


        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL( "CREATE TABLE " + DATABASE_TABLE + " (" +
        KEY_ROWID  + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    KEY_VEGETABLES + " TEXT NOT NULL, " + 
                    KEY_PRICE + " TEXT NOT NULL);"
            );

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            onCreate(db);

        }


    }

    public Vegetablesdatabase(Context c){
        ourContext = c;
    }

    public Vegetablesdatabase open() throws SQLException{
        ourHelper = new DbHelper(ourContext);
        ourdatabase = ourHelper.getWritableDatabase();
        return this;
    }

    public void close(){
        ourHelper.close();
    }

    public long createEntry(String vegetables, String price) {
        ContentValues cv = new ContentValues();
        cv.put(KEY_VEGETABLES, vegetables);
        cv.put(KEY_PRICE, price);
        return ourdatabase.insert(DATABASE_TABLE, null, cv);

    }



    public String getvegetablename(long l)throws SQLException {
        // TODO Auto-generated method stub
        String[] columns = new String[]{ KEY_ROWID,KEY_VEGETABLES,KEY_PRICE};
        Cursor c= ourdatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l,null, null, null, null);
        if(c!= null){
            c.moveToFirst();
            String name = c.getString(1);
            return name;
        }
        return null;
    }

    public String getvegetableprice(long l)throws SQLException {
        String[] columns = new String[]{ KEY_ROWID,KEY_VEGETABLES,KEY_PRICE};
        Cursor c= ourdatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l,null, null, null, null);
        if(c!= null){
            c.moveToFirst();
            String name = c.getString(2);
            return name;
        }
        return null;
    }

    public void updateentry(long lRow, String vegename, String vegeprice) throws SQLException {
        // TODO Auto-generated method stub
        ContentValues cvupdate = new ContentValues();
        cvupdate.put(KEY_VEGETABLES, vegename);
        cvupdate.put(KEY_PRICE, vegeprice);
        ourdatabase.update(DATABASE_TABLE, cvupdate, KEY_ROWID + "=" + lRow, null);

    }

    public String getData() {
        String [] columns = new String[]{ KEY_ROWID,KEY_VEGETABLES,KEY_PRICE};
        Cursor C =ourdatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
        String result = "";
        int iRow = C.getColumnIndex(KEY_ROWID);
        int ivegetable = C.getColumnIndex(KEY_VEGETABLES);
        int iprice = C.getColumnIndex(KEY_PRICE);

        for(C.moveToFirst(); !C.isAfterLast(); C.moveToNext()){
            result = result + C.getString(iRow) + " " + C.getString(ivegetable) + " " + C.getString(iprice) + "\n";

        }
        return result;
    }

    public void deleteEntry(long lRow1) throws SQLException {
        // TODO Auto-generated method stub
        ourdatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + lRow1, null);


    }
   }

共 (2) 个答案

  1. # 1 楼答案

    字符串数组可以在声明期间直接用值初始化。但是,当您想通过调用一个方法来初始化这些值时,应该遵循以下步骤

        String[] strArray = new String[5]; //Ex: 5 is the size of the array
        Vegetablesdatabase info = new Vegetablesdatabase(this);
        for(int i=0;i<strArray.length;i++){
        strArray[i] = info.getvegetable();
        }
    
    //strArray is filled with values after the loop
    

    请注意info.getvegetable()应该返回字符串文本。如果你不想要一个固定大小的集合,可以使用列表实现

    您必须选择ArrayList,因为您没有初始化期间所需的大小

    public ArrayList<String> getallvegetable() {
        String [] columns = new String[]{KEY_VEGETABLES};
        Cursor C =ourdatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
        ArrayList<String> result = new ArrayList<String>();
    
        int ivegetable = C.getColumnIndex(KEY_VEGETABLES);
        int iprice = C.getColumnIndex(KEY_PRICE);
    
        for(C.moveToFirst(); !C.isAfterLast(); C.moveToNext()){
            result.add(C.getString(ivegetable));
    
        }
        return result;
    }
    

    希望你现在明白了

  2. # 2 楼答案

    你可以使用八达通提供的答案,也可以通过像这样传递id给它

    for(int i=0;i<strArray.length;i++){
        strArray[i] = info.getvegetable(i);
        }
    

    或者您可以更改该方法,使其返回如下所示的字符串数组

        public String[] getvegetablenames()throws SQLException {
                // TODO Auto-generated method stub
                String[] columns = new String[]{ KEY_ROWID,KEY_VEGETABLES,KEY_PRICE};
                Cursor c= ourdatabase.query(DATABASE_TABLE, columns, null,null, null, null, null);
    int i=0;
    String[] values=new String[c.getCount()];
    c.moveToFirst();
                do{
    
                    values[i] = c.getString(1);
                   i++;
                }while(c.moveToNext());
                return values;
            }
    

    上面的代码可能有错误,但它已经足够让你开始了