Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.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
Java 如何从sqllite数据库获取数据并将其存储在ListView中_Java_Android_Database_Sqlite - Fatal编程技术网

Java 如何从sqllite数据库获取数据并将其存储在ListView中

Java 如何从sqllite数据库获取数据并将其存储在ListView中,java,android,database,sqlite,Java,Android,Database,Sqlite,我有一个数据库我需要从中读取数据并在列表视图中查看它包含一些表名为“main_categories”的表有一个名为“category_name”的字段,它是主键 这是数据适配器: public DataAdapter(Context context) { this.mContext = context; mDbHelper = new DataBaseHelper(mContext); } public DataAdapter creat

我有一个数据库我需要从中读取数据并在列表视图中查看它包含一些表名为“main_categories”的表有一个名为“category_name”的字段,它是主键 这是数据适配器:

    public DataAdapter(Context context) {
        this.mContext = context;
        mDbHelper = new DataBaseHelper(mContext);
    }

    public DataAdapter createDatabase() throws SQLException {
        try {
            mDbHelper.createDataBase();
        } catch (IOException mIOException) {
            Log.e(TAG, mIOException.toString() + "  UnableToCreateDatabase");
            throw new Error("UnableToCreateDatabase");
        }
        return this;
    }

    public DataAdapter open() throws SQLException {
        try {
            mDbHelper.openDataBase();
            mDbHelper.close();
            mDb = mDbHelper.getReadableDatabase();
        } catch (SQLException mSQLException) {
            Log.e(TAG, "open >>" + mSQLException.toString());
            throw mSQLException;
        }
        return this;
    }

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

    public Cursor getTestData() {
        try {
            String sql = "SELECT * FROM myTable";

            Cursor mCur = mDb.rawQuery(sql, null);
            if (mCur != null) {
                mCur.moveToNext();
            }
            return mCur;
        } catch (SQLException mSQLException) {
            Log.e(TAG, "getTestData >>" + mSQLException.toString());
            throw mSQLException;
        }
    }
}
这是DataBaseHelper:

    public  class DataBaseHelper extends SQLiteOpenHelper
    {
        private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window
        //destination path (location) of our database on device
        private static String DB_PATH = "";
        private static String DB_NAME ="my_knowledge";// Database name
        private SQLiteDatabase mDataBase;
        private final Context mContext;

        public DataBaseHelper(Context context)
        {
            super(context, DB_NAME, null, 1);// 1? Its database Version
            if(android.os.Build.VERSION.SDK_INT >= 17){
                DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
            }
            else
            {
                DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
            }
            this.mContext = context;
        }

        public void createDataBase() throws IOException
        {
            //If the database does not exist, copy it from the assets.

            boolean mDataBaseExist = checkDataBase();
            if(!mDataBaseExist)
            {
                this.getReadableDatabase();
                this.close();
                try
                {
                    //Copy the database from assests
                    copyDataBase();
                    Log.e(TAG, "createDatabase database created");
                }
                catch (IOException mIOException)
                {
                    throw new Error("ErrorCopyingDataBase");
                }
            }
        }

        //Check that the database exists here: /data/data/your package/databases/Da Name
        private boolean checkDataBase()
        {
            File dbFile = new File(DB_PATH + DB_NAME);
            //Log.v("dbFile", dbFile + "   "+ dbFile.exists());
            return dbFile.exists();
        }

        //Copy the database from assets
        private void copyDataBase() throws IOException
        {
            InputStream mInput = mContext.getAssets().open(DB_NAME);
            String outFileName = DB_PATH + DB_NAME;
            OutputStream mOutput = new FileOutputStream(outFileName);
            byte[] mBuffer = new byte[1024];
            int mLength;
            while ((mLength = mInput.read(mBuffer))>0)
            {
                mOutput.write(mBuffer, 0, mLength);
            }
            mOutput.flush();
            mOutput.close();
            mInput.close();
        }

        //Open the database, so we can query it
        public boolean openDataBase() throws SQLException
        {
            String mPath = DB_PATH + DB_NAME;
            //Log.v("mPath", mPath);
            mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
            //mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
            return mDataBase != null;
        }

        @Override
        public synchronized void close()
        {
            if(mDataBase != null)
                mDataBase.close();
            super.close();
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {

        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

        }
    }

and this is the main activity for now:

public class MainActivity extends AppCompatActivity {
    SQLiteDatabase sqLiteDatabase;
    private static String DB_NAME ="my_knowledge"; // Database name

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView list = findViewById(R.id.main_list);
        DataAdapter mDbHelper = new DataAdapter(this);
        mDbHelper.createDatabase();
        mDbHelper.open();

        Cursor testdata = mDbHelper.getTestData();
        mDbHelper.close();
    }
}

我想知道如何将数据存储在数组的表中(例如“main_categories”),并根据您的表列类型使用列表视图查看它们

List<String> temp;
testdata.moveToFirst();
do {
      temp.add(testdata.getString(0));
} while (testdata.moveToNext());
列表温度;
testdata.moveToFirst();
做{
temp.add(testdata.getString(0));
}while(testdata.moveToNext());
您的光标
testdata
位于
testdata.moveToFirst()中的第一行和每列与列索引关联,即0、1等。
testdata.moveToNext()将有连续的行。

有两件事:

    < >首先,我会考虑将<代码>查询>代码>方法添加到<代码>数据库助手>代码>类中,而不是使用<代码> RAWQuase<代码>,例如:

    光标查询(字符串表、字符串[]投影、字符串where、字符串orderBy){
    返回getReadableDatabase().query(表、投影、where、null、null、null、orderBy);
    }

  • 您可以在
    mDbHelper.getTestData()中调用查询方法
    ,并使用
    游标
    对象填充列表

  • 请看一下如何使用? 将数据从游标中取出并放入适配器


应用程序崩溃,日志显示“无法启动activity ComponentInfo:android.database.sqlite.SQLiteException:没有这样的表,而编译“没有这样的表”;确保在sql查询中调用的表存在。