Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.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,我正在编写一个应用程序,它将从远程服务器上的xml文件中读取数据,然后将其放入电话/选项卡上的SQLite数据库中。我知道它正在成功地从xml读取数据并将其放入SQLite.db文件。然而,问题是我的应用程序的其余部分好像SQLite.db文件是空的。在我退出应用程序并再次运行它之前,它不会访问数据。我做错了什么 以下是我获取数据的方式: // Get Instrument data from XML while (eventType != XmlResourceParser.

我正在编写一个应用程序,它将从远程服务器上的xml文件中读取数据,然后将其放入电话/选项卡上的SQLite数据库中。我知道它正在成功地从xml读取数据并将其放入SQLite.db文件。然而,问题是我的应用程序的其余部分好像SQLite.db文件是空的。在我退出应用程序并再次运行它之前,它不会访问数据。我做错了什么

以下是我获取数据的方式:

//  Get Instrument data from XML
        while (eventType != XmlResourceParser.END_DOCUMENT) {
            if (eventType == XmlResourceParser.START_TAG) {
                //  Get the name of the tag (eg data or datum)
                String strName = instruments.getName();
                if (strName.equals("datum")) {
                    bFoundInstruments = true;
                    String datumDate = instruments.getAttributeValue(null, "date");
                    Float datumWtioil = Float.parseFloat(instruments.getAttributeValue(null, "wtioil"));
                    Float datumHh = Float.parseFloat(instruments.getAttributeValue(null, "hh"));
                    Float datumPlat = Float.parseFloat(instruments.getAttributeValue(null, "plat"));


                    publishProgress(datumDate, Float.toString(datumWtioil), Float.toString(datumHh), Float.toString(datumPlat));

                    // add data to SQLite database
                    datasource.createInstrument(datumDate, Float.toString(datumWtioil), Float.toString(datumHh), Float.toString(datumPlat));


                }
            }
下面是我如何使用createInstrument()方法将其放入SQLite.db文件的:

为什么应用程序不能立即获得这些数据?为什么我必须在应用程序可见之前关闭并重新启动它


任何建议都将不胜感激。

首先,请从该函数中删除从db获取值的查询,因为您尝试将一个仪器插入db,然后将其读回。这样做:
a) 读取xml文件并将其添加到数据库中,然后
b) 创建一个查询函数,并从数据库返回所有仪器

public List< Instrument > getAllInstruments() {
    // create a list
    List<Instrument> list = new ArrayList< Instrument>();

     Cursor cursor = database.query(ForecastSQLiteHelper.TABLE_DATA,
    allColumns, null, null, null, null, null);

    // check if cursor has columns
    if (cursor.getColumnCount() > 0) {
        // read all cursor values
        while (cursor.moveToNext()) {
            Instrument newInstrument = new Instrument();
            // now you have to get the values from the cursor and add them to your object
            // like this
           String instrumentName= cursor.getString(cursor.getColumnIndex(COLUMN_NAME));
           // COLUMN_NAME is the name of the column in your database that you want the value of

          // now add the values to the Instrument object
         newInstrument.setName(instrumentName);

         // now add the object to the list
         list.add(newInstrument);
        }
    }

    cursor.close();
    return list; //finally return the list
}
public ListgetAllInstruments(){
//创建一个列表
列表=新阵列列表();
Cursor Cursor=database.query(ForecastSQLiteHelper.TABLE_数据,
所有列,null,null,null,null,null,null);
//检查游标是否有列
if(cursor.getColumnCount()>0){
//读取所有光标值
while(cursor.moveToNext()){
仪器新仪器=新仪器();
//现在,您必须从光标获取值并将其添加到对象中
//像这样
String instrumentName=cursor.getString(cursor.getColumnIndex(COLUMN_NAME));
//COLUMN_NAME是数据库中要计算其值的列的名称
//现在将这些值添加到Instrument对象
newInstrument.setName(instrumentName);
//现在将对象添加到列表中
列表。添加(新仪器);
}
}
cursor.close();
return list;//最后返回列表
}

首先,请从该函数中删除从db获取值的查询,因为您尝试将一个仪器插入db,然后将其读回。这样做:
a) 读取xml文件并将其添加到数据库中,然后
b) 创建一个查询函数,并从数据库返回所有仪器

public List< Instrument > getAllInstruments() {
    // create a list
    List<Instrument> list = new ArrayList< Instrument>();

     Cursor cursor = database.query(ForecastSQLiteHelper.TABLE_DATA,
    allColumns, null, null, null, null, null);

    // check if cursor has columns
    if (cursor.getColumnCount() > 0) {
        // read all cursor values
        while (cursor.moveToNext()) {
            Instrument newInstrument = new Instrument();
            // now you have to get the values from the cursor and add them to your object
            // like this
           String instrumentName= cursor.getString(cursor.getColumnIndex(COLUMN_NAME));
           // COLUMN_NAME is the name of the column in your database that you want the value of

          // now add the values to the Instrument object
         newInstrument.setName(instrumentName);

         // now add the object to the list
         list.add(newInstrument);
        }
    }

    cursor.close();
    return list; //finally return the list
}
public ListgetAllInstruments(){
//创建一个列表
列表=新阵列列表();
Cursor Cursor=database.query(ForecastSQLiteHelper.TABLE_数据,
所有列,null,null,null,null,null,null);
//检查游标是否有列
if(cursor.getColumnCount()>0){
//读取所有光标值
while(cursor.moveToNext()){
仪器新仪器=新仪器();
//现在,您必须从光标获取值并将其添加到对象中
//像这样
String instrumentName=cursor.getString(cursor.getColumnIndex(COLUMN_NAME));
//COLUMN_NAME是数据库中要计算其值的列的名称
//现在将这些值添加到Instrument对象
newInstrument.setName(instrumentName);
//现在将对象添加到列表中
列表。添加(新仪器);
}
}
cursor.close();
return list;//最后返回列表
}

首先,请从该函数中删除从db获取值的查询,因为您尝试将一个仪器插入db,然后将其读回。这样做:
a) 读取xml文件并将其添加到数据库中,然后
b) 创建一个查询函数,并从数据库返回所有仪器

public List< Instrument > getAllInstruments() {
    // create a list
    List<Instrument> list = new ArrayList< Instrument>();

     Cursor cursor = database.query(ForecastSQLiteHelper.TABLE_DATA,
    allColumns, null, null, null, null, null);

    // check if cursor has columns
    if (cursor.getColumnCount() > 0) {
        // read all cursor values
        while (cursor.moveToNext()) {
            Instrument newInstrument = new Instrument();
            // now you have to get the values from the cursor and add them to your object
            // like this
           String instrumentName= cursor.getString(cursor.getColumnIndex(COLUMN_NAME));
           // COLUMN_NAME is the name of the column in your database that you want the value of

          // now add the values to the Instrument object
         newInstrument.setName(instrumentName);

         // now add the object to the list
         list.add(newInstrument);
        }
    }

    cursor.close();
    return list; //finally return the list
}
public ListgetAllInstruments(){
//创建一个列表
列表=新阵列列表();
Cursor Cursor=database.query(ForecastSQLiteHelper.TABLE_数据,
所有列,null,null,null,null,null,null);
//检查游标是否有列
if(cursor.getColumnCount()>0){
//读取所有光标值
while(cursor.moveToNext()){
仪器新仪器=新仪器();
//现在,您必须从光标获取值并将其添加到对象中
//像这样
String instrumentName=cursor.getString(cursor.getColumnIndex(COLUMN_NAME));
//COLUMN_NAME是数据库中要计算其值的列的名称
//现在将这些值添加到Instrument对象
newInstrument.setName(instrumentName);
//现在将对象添加到列表中
列表。添加(新仪器);
}
}
cursor.close();
return list;//最后返回列表
}

首先,请从该函数中删除从db获取值的查询,因为您尝试将一个仪器插入db,然后将其读回。这样做:
a) 读取xml文件并将其添加到数据库中,然后
b) 创建一个查询函数,并从数据库返回所有仪器

public List< Instrument > getAllInstruments() {
    // create a list
    List<Instrument> list = new ArrayList< Instrument>();

     Cursor cursor = database.query(ForecastSQLiteHelper.TABLE_DATA,
    allColumns, null, null, null, null, null);

    // check if cursor has columns
    if (cursor.getColumnCount() > 0) {
        // read all cursor values
        while (cursor.moveToNext()) {
            Instrument newInstrument = new Instrument();
            // now you have to get the values from the cursor and add them to your object
            // like this
           String instrumentName= cursor.getString(cursor.getColumnIndex(COLUMN_NAME));
           // COLUMN_NAME is the name of the column in your database that you want the value of

          // now add the values to the Instrument object
         newInstrument.setName(instrumentName);

         // now add the object to the list
         list.add(newInstrument);
        }
    }

    cursor.close();
    return list; //finally return the list
}
public ListgetAllInstruments(){
//创建一个列表
列表=新阵列列表();
Cursor Cursor=database.query(ForecastSQLiteHelper.TABLE_数据,
所有列,null,null,null,null,null,null);
//检查游标是否有列
if(cursor.getColumnCount()>0){
//读取所有光标值
while(cursor.moveToNext()){
仪器新仪器=新仪器();
//现在,您必须从光标获取值并将其添加到对象中
//像这样
String instrumentName=cursor.getString(cursor.getColumnIndex(COLUMN_NAME));
//COLUMN_NAME是数据库中要计算其值的列的名称
//现在将这些值添加到Instrument对象
newInstrument.setName(instrumentName);
//现在将对象添加到列表中
列表。添加(新仪器);
}
}
cursor.close();