复制数据库给出错误没有这样的表

时间:2014-08-26 19:31:45

标签: android sqlite

我的应用程序一运行,就会实现一个管理数据库版本的SQLiteOpenHelper对象。

如果数据库不存在或已过时,我们会将其从assets文件夹复制到Android设备。

问题是,当我尝试查询数据库时,我收到以下错误。

08-26 14:56:21.712: V/Database Milestone(2403): The Database has been successfully copied.
08-26 14:56:21.722: V/Databae Log Statement(2403): The onUpgrade method of the DataBaseHelper has been called.

08-26 14:56:21.892: E/SQLiteLog(2403): (1) no such table: CARDS
08-26 14:56:21.942: W/System.err(2403): android.database.sqlite.SQLiteException: no such table: CARDS (code 1): , while compiling: Select DISTINCT _id, Name FROM CARDS LEFT JOIN `CARD-EFFECT` USING (_ID)
08-26 14:56:22.022: W/System.err(2403):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
08-26 14:56:22.042: W/System.err(2403):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
08-26 14:56:22.082: W/System.err(2403):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
08-26 14:56:22.082: W/System.err(2403):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
08-26 14:56:22.092: W/System.err(2403):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
08-26 14:56:22.092: W/System.err(2403):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
08-26 14:56:22.092: W/System.err(2403):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
08-26 14:56:22.092: W/System.err(2403):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
08-26 14:56:22.142: W/System.err(2403):     at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
08-26 14:56:22.152: W/System.err(2403):     at com.example.mycursoradapter.CardListView.onActivityCreated(CardListView.java:92)

这是我用来管理数据库的SQLiteOpenHelper类。

public class DataBaseHelper extends SQLiteOpenHelper 
{
 //The Android's default system path of your application database.
    private static String DB_PATH = "/data/data/com.example.mycursoradapter/databases/";
    private static String DB_NAME = "hearthstonedata";
    private final Context myContext;
    private final static int LASTEST_DATABASE_VERSION = 15;


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

/**
 * Copies your 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 transfering bytestream.
 * */
public void copyDataBase() throws IOException 
{
    // Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    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 outputfile
    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();
    Log.v("Database Milestone", "The Database has been successfully copied.");
}    

@Override
public void onCreate(SQLiteDatabase arg0) 
{
    Log.v("Pierre D Log Statement", "The database has not been copied yet.");


    try 
    {
        this.copyDataBase();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

    Log.v("Pierre D Log Statement", "The onCreate method of the DataBaseHelper has been called.");

}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) 
{
    try
    {
        Log.v("Pierre db OnUpgrade", String.valueOf(arg0.isOpen()));


        this.copyDataBase();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    Log.v("Pierre D Log Statement", "The onUpgrade method of the DataBaseHelper has been called.");


}

}

这是我查询数据库的地方。

public class CardListView extends Fragment 
{   
    private final String SQL_BASE_QUERY = "Select DISTINCT _id, Name FROM CARDS LEFT JOIN `CARD-EFFECT` USING (_ID)";

    private Activity currentActivity;
    private DataBaseHelper myDbHelper;
    public static SQLiteDatabase database;
    private Cursor mainCursor;

    public CardListView()
    {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState)
    {   
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.list_view, container, false);
    }

    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);

        currentActivity = this.getActivity();
        myDbHelper = new DataBaseHelper(currentActivity);
        database = myDbHelper.getReadableDatabase();

        Log.v("Testing the Which value", "Database size in bytes " +   String.valueOf(database.getPageSize()));
        Log.v("Testing the Which value", "Database path " + String.valueOf(database.getPath()));
        Log.v("Testing the Which value", "Database version number " + String.valueOf(database.getVersion()));

        // I am getting the error at this line
        mainCursor = database.rawQuery(SQL_BASE_QUERY, null);

我知道我的查询正在运行,因为我已经在SQLite程序上测试了它。我知道桌子卡片退出了。这只是创建/复制和打开数据库的问题。

2 个答案:

答案 0 :(得分:1)

您是否尝试卸载应用程序并重新安装它,这将删除现有数据库,否则它将不会从其资产资源中复制数据库。它将使用先前存在的数据库。

答案 1 :(得分:0)

您的数据库输出已成功复制&#39;无论如何打印。即使数据库没有被复制。所以你不知道数据库是否真的被复制了。问题必须出在副本上,因为表示表格不存在

尝试使用从文件夹导入数据库的那个:

importDb.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                importDB();
            }
        });

// importing database
    @SuppressLint("SdCardPath")
    private void importDB() {
        try {
//          File file = getBaseContext().getFileStreamPath(Environment.getExternalStorageDirectory()
//                  + "/MyDatabase");
//          if(file.exists()){
                FileInputStream fis = new FileInputStream(Environment.getExternalStorageDirectory()
                        + "/MyDatabase");       
                String outFileName = "/data/data/com.example.mycarfuel/databases/MyDatabase";
                OutputStream output = new FileOutputStream(outFileName);
                byte[] buffer = new byte[1024];
                int length;
                while ((length = fis.read(buffer)) > 0) {
                    output.write(buffer, 0, length);
                }
                // Close the streams
                output.flush();
                output.close();
                fis.close();
                Toast.makeText(SettingsActivity.this, "Database loaded succesfully",
                        Toast.LENGTH_LONG).show();
            //}else{
                //Toast.makeText(SettingsActivity.this, "File does not exist in 'mnt/sdcard/' location. Please add the 'MyDatabase' file here in order to import",
                //      Toast.LENGTH_LONG).show();
            //}

        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
                    .show();
        }
    }