我创建了一个数据库和一个用于保存数据的表。但是当我从应用程序退出并再次运行时,我的数据已经存储,然后根本不存在?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我创建了一个数据库和一个用于保存数据的表。但是当我从应用程序退出并再次运行时,我的数据已经存储,然后根本不存在?相关的知识,希望对你有一定的参考价值。

我是android的新手。我想将我的音频数据存储到数据库中。我可以在保存数据之前创建数据库和表。但问题是当我从应用程序退出并再次运行时,我的数据已经存储,之前根本不存在。我需要之前保存的那些数据。但我的数据库存在这个问题。我确信数据存储真实。可能是问题是每次我运行应用程序,数据库覆盖现有数据库。实际上是什么问题?你的帮助让我高兴。谢谢。这是我用过的,真正有效的课程。

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MySqilteOpenHelper extends SQLiteOpenHelper 
{

public static final String TABLE_AUDios = "audios";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_fORMAT = "format";
public static final String COLUMN_PATH = "path";
public static final String COLUMN_TOTAL_TIME = "total_time";
public static final String COLUMN_SIZE = "size";
public static final String COLUMN_CREATEDATE = "create_date";
public static final String COLUMN_CREATETIME= "create_time";
private static final String DATABASE_NAME = "my_database.db";
private static final int DATABASE_VERSION = 1;  
private static final String DATABASE_CREATE = "create table "
    + TABLE_AUDIOS + "(" + COLUMN_ID
    + " integer primary key autoincrement, " + COLUMN_NAME
    + " text not null, " + COLUMN_fORMAT + " text not null, " +COLUMN_PATH + " text not null, " + COLUMN_TOTAL_TIME + " text not null, " + COLUMN_SIZE + " text not null, " +  COLUMN_CREATETIME + " text not null, "+COLUMN_CREATEDATE + " text not null);";

public MySqilteOpenHelper(Context context) 
{
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    Log.d("database create statement:", DATABASE_CREATE);       
}

@Override
public void onCreate(SQLiteDatabase database)
{
    database.execSQL( ""+DATABASE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) 
{
    Log.w(MySqilteOpenHelper.class.getName(),
            "Upgrading database from version " + oldVersion +
            " to "
            + newVersion + ", which will destroy all old data");
            database.execSQL("DROP TABLE IF EXISTS " + TABLE_AUDIOS);
            onCreate(database);

}

}

这是一个可以插入和获取行的类。

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class Interface_For_UpgradingAudiosTable
{
private SQLiteDatabase database;
private MySqilteOpenHelper dbHelper;
private String[] allColumns = { MySqilteOpenHelper.COLUMN_ID,
MySqilteOpenHelper.COLUMN_NAME,
MySqilteOpenHelper.COLUMN_fORMAT,MySqilteOpenHelper.COLUMN_PATH ,MySqilteOpenHelper.COLUMN_TOTAL_TIME ,MySqilteOpenHelper.COLUMN_SIZE , MySqilteOpenHelper.COLUMN_CREATETIME , MySqilteOpenHelper.COLUMN_CREATEDATE  };

private String[] mycolumns={MySqilteOpenHelper.COLUMN_NAME,
        MySqilteOpenHelper.COLUMN_fORMAT,MySqilteOpenHelper.COLUMN_PATH ,MySqilteOpenHelper.COLUMN_TOTAL_TIME ,MySqilteOpenHelper.COLUMN_SIZE , MySqilteOpenHelper.COLUMN_CREATETIME , MySqilteOpenHelper.COLUMN_CREATEDATE  };

public Interface_For_UpgradingAudiosTable(Context context)
{

    dbHelper = new MySqilteOpenHelper(context);
}

public void open() throws SQLException 
{
    database = dbHelper.getWritableDatabase();
}

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

public StructureDetails createAudio(StructureDetails sd) 
{
    ContentValues values = new ContentValues();

    values.put(MySqilteOpenHelper.COLUMN_NAME, sd.getName());
    values.put(MySqilteOpenHelper.COLUMN_fORMAT, sd.getFormat());
    values.put(MySqilteOpenHelper.COLUMN_PATH, sd.getPath());
    values.put(MySqilteOpenHelper.COLUMN_TOTAL_TIME, sd.getTotalTime());
    values.put(MySqilteOpenHelper.COLUMN_SIZE, sd.getSize());
    values.put(MySqilteOpenHelper.COLUMN_CREATETIME, sd.getCreateTime());
    values.put(MySqilteOpenHelper.COLUMN_CREATEDATE, sd.getCreateDate());

    long insertId = database.insert(MySqilteOpenHelper.TABLE_AUDIOS,null,values);
    Log.d("insert iddddddddddddddddddddddddddddddddd:",insertId+"");
    Cursor cursor = database.query(MySqilteOpenHelper.TABLE_AUDIOS,
    allColumns, MySqilteOpenHelper.COLUMN_ID + " = " +
    insertId, null,
    null, null, null);
    cursor.moveToFirst();
    StructureDetails newAudio = cursorToAudio(cursor);
    cursor.close();
    return newAudio;
}

/*public void deleteAudio(StructureDetails Audio) 
{
    String path = Audio.getPath();
    System.out.println("Comment deleted with id: " + path);
    database.delete(MySqilteOpenHelper.TABLE_AUDIOS,
    MySqilteOpenHelper.COLUMN_PATH
    + " = " + path, null);
}*/

public ArrayList<StructureDetails> getAllAudiosFromDb() 
{
    List<StructureDetails> audiosList = new ArrayList<StructureDetails>();
    Log.d("errorrrrrrrrrrrr in get allllll", "");
    Cursor cursor = database.query(MySqilteOpenHelper.TABLE_AUDIOS,
            allColumns, null, null, null, null, null);
    if(!cursor.moveToFirst())
    while (!cursor.isAfterLast()) 
    {
        StructureDetails audio = cursorToAudio(cursor);
        audiosList.add(audio);
        cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return (ArrayList<StructureDetails>) audiosList;
}

private StructureDetails cursorToAudio(Cursor cursor) 
{
    StructureDetails Audio = new StructureDetails();
    Audio.setId(cursor.getLong(0));
    Audio.setName(cursor.getString(1));
    Audio.setFormat(cursor.getString(2));
    Audio.setPath(cursor.getString(3));
    Audio.setTotalTime(cursor.getString(4));
    Audio.setSize(cursor.getString(5));
    Audio.setCreateTime(cursor.getString(6));
    Audio.setCreateDate(cursor.getString(7));
    return Audio;
}

}

答案

create table更改为CREATE TABLE IF NOT EXISTS否则每次都会创建表格,数据将丢失

所以一定是

private static final String DATABASE_CREATE = "CREATE TABLE IF NOT EXISTS "
+ TABLE_AUDIOS + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_NAME
+ " text not null, " + COLUMN_fORMAT + " text not null, " +COLUMN_PATH + " text not null, " + COLUMN_TOTAL_TIME + " text not null, " + COLUMN_SIZE + " text not null, " +  COLUMN_CREATETIME + " text not null, "+COLUMN_CREATEDATE + " text not null);";

UPDATE

当创建Db时,onCreate()onUpgrade()方法真的被称为第一次。事实上,它在SQLiteOpenHelper的getReadableDatabase()getWritebleDatabase()方法中进行了检查。它将检查数据库目录中是否已存在DB以及它的版本是什么。根据这个,如果db文件存在正确的版本,它将调用onCreate()onUpgrade()或者什么也没有。

您可以搜索代码以执行myDBHelper.getReadable(Writable)Database()。这是执行此检查的时间。

另一答案

我的数据库真的有用,如果我有这个代码而不是代码:

enter code herepublic ArrayList<StructureDetails> getAllAudiosFromDb() 
{
List<StructureDetails> audiosList = new ArrayList<StructureDetails>();
Log.d("errorrrrrrrrrrrr in get allllll", "");
Cursor cursor = database.query(MySqilteOpenHelper.TABLE_AUDIOS,
        allColumns, null, null, null, null, null);
cursor.moveToFirst();// this is changed!
while (!cursor.isAfterLast()) 
{
    StructureDetails audio = cursorToAudio(cursor);
    audiosList.add(audio);
    cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return (ArrayList<StructureDetails>) audiosList;

}

以上回答“Arju”可能会有所帮助。我觉得我的方法“getAllAudiosFromDb”有问题。可能这对你有所帮助。

以上是关于我创建了一个数据库和一个用于保存数据的表。但是当我从应用程序退出并再次运行时,我的数据已经存储,然后根本不存在?的主要内容,如果未能解决你的问题,请参考以下文章

MYSQL创建一个触发器,用于24小时刷新一下数据库的表.

当我使用Model.findOneAndUpdate时,不会调用用于保存和更新的猫鼬预钩子

使用 Hibernate 和 JSP 创建表和数据未保存

从两个表更新新创建的表 - SQL

Spring Security 创建一个名为 USERS 的表

动态创建一个对象