Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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
Android SQLite将数据从数据库获取到数组_Android_Sqlite - Fatal编程技术网

Android SQLite将数据从数据库获取到数组

Android SQLite将数据从数据库获取到数组,android,sqlite,Android,Sqlite,我想从游标中获取不带SimpleCursorAdapter部分的值 public Cursor queueAll(){ String[] columns = new String[]{KEY_ID, KEY_CONTENT1, KEY_CONTENT2,KEY_CONTENT3}; Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, null, null, null, null, null); ret

我想从游标中获取不带SimpleCursorAdapter部分的值

public Cursor queueAll(){
  String[] columns = new String[]{KEY_ID, KEY_CONTENT1, KEY_CONTENT2,KEY_CONTENT3};
  Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
    null, null, null, null, null);
  return cursor;
 }
以及活动端代码

cursor = mySQLiteAdapter.queueAll();

       from = new String[]{SQLiteAdapter.KEY_ID, SQLiteAdapter.KEY_CONTENT1, SQLiteAdapter.KEY_CONTENT2, SQLiteAdapter.KEY_CONTENT3};
       int[] to = new int[]{R.id.id, R.id.text1, R.id.text2,R.id.text3};
       cursorAdapter =
       new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
       listContent.setAdapter(cursorAdapter); 

       while(!cursor.isAfterLast())
       {
           String tilt = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CONTENT1));
           String pkg = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CONTENT3));
           if(tilt.equals("LEFT"))
           {
           Log.v("LEFT",pkg);
           }
           else if(tilt.equals("RIGHT"))
           {
               Log.v("RIGHT",pkg);
           }
           cursor.moveToNext();
       }
我得到的pkg值是正确的。但是我想在删除SimpleCursorAdapter部分时直接从光标获得值。代码不起作用。
任何帮助都将不胜感激:)

您无需声明适配器即可获得值。如果要在列表小部件中显示数据,则需要适配器。因此,您可以选择以下选项:

cursor = mySQLiteAdapter.queueAll();

if (cursor == null) {
//check if there are errors or query just return null
}
if (cursor.moveToFirst()) {
       while(!cursor.isAfterLast())
       {
           String tilt = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CONTENT1));
           String pkg = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CONTENT3));
           if(tilt.equals("LEFT"))
           {
           Log.v("LEFT",pkg);
           }
           else if(tilt.equals("RIGHT"))
           {
               Log.v("RIGHT",pkg);
           }
           cursor.moveToNext();
       }
}

首先,您必须计算表中有多少行。然后将该计数分配给变量,并使用该变量创建数组。对于数组大小,可以使用该计数变量。然后创建一个for循环,并将该count变量用于轮数。创建查询并为数组赋值后。100%成功了

    int sqlcount;
    Cursor mFetch;

    SQLiteDatabase mydb = openOrCreateDatabase("your database name", MODE_PRIVATE, null);
    mydb.execSQL("create table if not exists customer(name varchar,email varchar)");
    Cursor mCount = mydb.rawQuery("select count(*) from customer", null);
    mCount.moveToFirst();
    sqlcount = mCount.getInt(0);
    mCount.close();

    String cname[] = new String[sqlcount];
    String cemail[] = new String[sqlcount];


     for(int i=0;i<sqlcount;i++) {

          mFetch= mydb.rawQuery("select name,email from customer", null);
          mFetch.moveToPosition(i);

          cname[i] = mFetch.getString(0);
          cemail[i] = mFetch.getString(1);


     }

     mFetch.close();

    Toast.makeText(MainActivity.this,String.valueOf(cname[0]),Toast.LENGTH_SHORT).show();
    Toast.makeText(MainActivity.this,String.valueOf(cemail[1]),Toast.LENGTH_SHORT).show();
intsqlcount;
光标蚀刻;
SQLiteDatabase mydb=openOrCreateDatabase(“您的数据库名称”,MODE_PRIVATE,null);
execSQL(“创建表,如果不存在客户(名称varchar,email varchar)”;
游标mCount=mydb.rawQuery(“从客户选择计数(*),null);
mCount.moveToFirst();
sqlcount=mCount.getInt(0);
mCount.close();
字符串cname[]=新字符串[sqlcount];
字符串cemail[]=新字符串[sqlcount];

for(int i=0;i不清楚您在这里要查找的内容。我想从游标获取值。但是如果没有这段代码,请从=new String[]{SQLiteAdapter.KEY\u ID,SQLiteAdapter.KEY\u CONTENT1,SQLiteAdapter.KEY\u CONTENT2,SQLiteAdapter.KEY\u CONTENT3};int[]到=new int[]{R.id.id,R.id.text1,R.id.text2,R.id.text3};cursorAdapter=new simplecursorsadapter(this,R.layout.row,cursor,from,to);listContent.setAdapter(cursorAdapter);在进入循环之前必须调用cursor.moveToFirst()(或使用moveToNext()的结果作为退出条件)