Android的SQLite数据库没有更新使用pre填充SQLite数据库必须解除停止并重新安装数据库、重新安装、Android、SQLite

2023-09-04 04:50:35 作者:赏你一巴掌

下面是我的问题。我有一个程序,我已经上传到谷歌Play商店。该应用程序工作正常,直到它到达的地方,我增加了新的记录,我的数据库,因为我在pre-填充SQLite数据库创建水平,当我到达崩溃这一特定水平的部分。就像没有创建新的信息。在code是如下。我已阅读,我需要创建一个版本的变量,并增加它在onupgrade()方法。但我是新来这个sqlite的学习。如果我创建一个版本变量将在code跟踪呢?,我必须重命名版本号数据库,请大家帮忙。

我已经试过这里遵循以下这个例子,这似乎是我的在寻找,但我的问题是看的例子是版本中的code都做或做我必须要追加一个版本号,以我的* .db文件。

 包com.xtremeware.straighthoodtrivia.db;

进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.OutputStream中;
进口的java.util.ArrayList;
进口的java.util.List;

进口android.content.Context;
进口android.database.Cursor;
进口android.database.SQLException;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteException;
进口android.database.sqlite.SQLiteOpenHelper;

进口com.xtremeware.straighthoodtrivia.quiz.Question;

公共类DBHelper扩展SQLiteOpenHelper
{
//该机器人的默认系统应用程序数据库的路径。
私有静态字符串DB_PATH =/data/data/com.xtremeware.straighthoodtrivia /数据库/;
私有静态字符串DB_NAME =QuestionsDb;
私人SQLiteDatabase MyDatabase的;
私人最终语境myContext;

//构造函数
//注意到并保持传递的上下文中的一个参考,以访问
//应用资产和资源。
// @环境

公共DBHelper(上下文的背景下)
{
    超级(上下文,DB_NAME,空,1);
    this.myContext =背景;
}

//创建一个空数据库的系统上,并用我自己的数据库重写它
公共无效的CreateDatabase()抛出IOException异常
{
    布尔dbExist = checkDataBase();
    如果(!dbExist)
    {
        //通过调用此方法的一个空数据库将被创建到系统​​默认路径
        //我的应用程序,所以我要成为能与我的数据库覆盖数据库。
        this.getReadableDatabase();
         尝试
         {
             copyDataBase();
         }
         赶上(IOException异常E)
         {
             抛出新的错误(错误复制数据库);
         }
    }
}

//检查数据库已经存在,以避免重新复制每次打开应用程序时,该文件。
如果它存在,如果它不// @返回true和false

私人布尔checkDataBase()
{
    SQLiteDatabase CHECKDB = NULL;
    尝试
    {
        字符串mypath中= DB_PATH + DB_NAME;
        CHECKDB = SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READONLY);
    }
    赶上(SQLiteException E)
    {
        //数据库还不存在。
    }
    如果(CHECKDB!= NULL)
    {
        checkDB.close();
    }

    返回CHECKDB!= NULL?真假;
}

//将数据库从本地资产文件夹复制到刚创建的空数据库
//系统文件夹,从那里可以访问和处理。
//这是通过传送字节流进行。

私人无效copyDataBase()抛出IOException异常
{
    //打开本地数据库作为输入流
    InputStream的myInput = myContext.getAssets()开(DB_NAME)。

    //路径刚刚创建的空分贝
    字符串outFileName = DB_PATH + DB_NAME;

    //打开空分贝的输出流
    的OutputStream myOutput =新的FileOutputStream(outFileName);

    //将字节从inputfile中到输出文件
    byte []的缓冲区=新的字节[1024];
    INT长;
    而((长度= myInput.read(缓冲液))大于0)
    {
        myOutput.write(缓冲液,0,长度);
    }

    //关闭流
    myOutput.flush();
    myOutput.close();
    myInput.close();
}

公共无效的openDatabase()抛出的SQLException
{
    //打开数据库
    字符串mypath中= DB_PATH + DB_NAME;
    MyDatabase的= SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READONLY);

}

@覆盖
市民同步无效关闭()
{
    如果(MyDatabase的!= NULL)
        myDataBase.close();
    super.close();
}

@覆盖
公共无效的onCreate(SQLiteDatabase DB)
{
}

@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页)
{
}
 

解决方案 Android 使用SQLite数据库

您把这样的:

 私有静态最终诠释DATABASE_VERSION = 2;
 

在你的DBHelper类。

和你的构造函数,然后实现,以及的onCreate和onUpgrade方法,如:

  DBHelper(上下文的背景下){
      超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
   }

   @覆盖
   公共无效的onCreate(SQLiteDatabase DB){
      db.execSQL(CREATE TABLE existing_table ......);
      db.execSQL(CREATE TABLE NEW_TABLE ......);
   }

   @覆盖
   公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
      Log.w(TAG,从版本升级数据库+ oldVersion +到+动态网页+。);
      //db.execSQL("DROP表,如果存在+ DATABASE_TABLE);
      如果(oldVersion == 1&安培;&安培; NEWVERSION> = 2){
         db.execSQL(ALTER TABLE existing_table加列NEW_COLUMN整数空默认-1;);
         db.execSQL(创建表NEW_TABLE(......));
      }
   }
 

该版本将在code做,但版本code是通过在超(...)调用JN的DBHelper的构造,这需要确保照顾数据库意识到其目前的版。你不必追加自己的版本号为您的* .db的文件。

基本上,通过比较oldVersion和动态网页,你决定在升级运行的脚本。 然而,对于新的应用程序安装,你只要确定的onCreate版本创建的最新表的时候了。

接下来的时间要发布一个版本,您的应用程序需要更新的表结构,你增加你的DATABASE_VERSION字段的值,并根据需要改变onUpgrade。

Here is my question. I have a app I have uploaded to the Google play store. The app works fine until it gets to the part where I added new records to my database for a level I created in a pre-populated sqlite database, when I get to this particular level it crashes. Like the new information was not created. the code is below. I have read that I need to create a version variable and increment it, in the onupgrade() method. but I am new to this sqlite learning. If I create a version variable will the code keep track of it?, do I have to rename the database with a version number, please help.

I have tried following the following this example here, and it seems to be what I am looking for, but my question is looking at that the example is the versioning all done within code or do I have to append a version number to my *.db file.

package com.xtremeware.straighthoodtrivia.db;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

import com.xtremeware.straighthoodtrivia.quiz.Question;

public class DBHelper extends SQLiteOpenHelper
{   
// The androids's default system path of the application database.
private static String DB_PATH = "/data/data/com.xtremeware.straighthoodtrivia    /databases/";
private static String DB_NAME = "QuestionsDb";
private SQLiteDatabase myDataBase;
private final Context myContext;

// Constructor
// Takes and keeps a reference of the passed context in order to access to the 
// applications assets and resources.
// @ context

public DBHelper(Context context)
{
    super(context, DB_NAME, null, 1);
    this.myContext = context;
}

// Creates a empty database on the system and rewrites it with my own database
public void createDataBase() throws IOException
{
    boolean dbExist = checkDataBase();
    if(!dbExist)
    {
        // By calling this method an empty database will be created into the default system path
        // of my application so I am gonna be able to overwrite the database with my database.
        this.getReadableDatabase();
         try 
         {
             copyDataBase();
         }
         catch (IOException e)
         {
             throw new Error("Error copying database");
         }
    }
}

// Check if the database already exists to avoid re-copying the file each time you open the application.
// @return true if it exists, false if it doesn't

private boolean checkDataBase()
{
    SQLiteDatabase checkDB = null;
    try
    {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    }
    catch (SQLiteException e)
    {
        // Database doesn't exist yet.
    }
    if(checkDB != null)
    {
        checkDB.close();
    }

    return checkDB != null ? true : false;
}

// Copies the database from your local assets-folder to the just created empty database in 
// the system folder, from where it can be accessed and handled.
// This is done by transferring bytestreams.

private void copyDataBase() throws IOException
{
    // Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the empty db just created
    String outFileName = DB_PATH + DB_NAME;

    // Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    // Transfer bytes from the inputfile to the output file
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0)
    {
        myOutput.write(buffer, 0, length);
    }

    // Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
}

public void openDataBase() throws SQLException
{
    // Open the database
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}

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

@Override
public void onCreate(SQLiteDatabase db)
{
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
}

解决方案

You put something like:

private static final int DATABASE_VERSION = 2;

in your DBHelper class.

And then implementations of your constructor, and the onCreate and onUpgrade methods like:

   DBHelper(Context context) {
      super(context, DATABASE_NAME, null, DATABASE_VERSION);
   }

   @Override
   public void onCreate(SQLiteDatabase db) {
      db.execSQL("create table existing_table...");
      db.execSQL("create table new_table...");
   }

   @Override
   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ".");
      //db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
      if (oldVersion == 1 && newVersion >= 2){
         db.execSQL("alter table existing_table add column new_column integer not null default -1;");
         db.execSQL("create table new_table (...)");
      }
   }

The versioning is done in the code, but the version code is passed in the super(...) call jn the DBHelper constructor, which takes care of making sure the database is aware of its current version. You do not have to append the version number yourself to your "*.db" file.

Basically, by comparing the oldVersion and the newVersion you decide which scripts to run on the upgrade. However, for new app installs, you just make sure the onCreate version creates the up to date tables right away.

Next time to you want to publish a version of your app that needs an update to the table structure, you increment the value of your DATABASE_VERSION field, and change the onUpgrade as required.