为什么我的数据库似乎没有安装?

时间:2013-04-01 17:24:03

标签: android

我的应用程序使用数据库。 在我的nexus 4上,没有root,4.2.1,应用程序正在运行,数据库已经安装了。 但是在我的Iconia A501 4.0.3上,根目录,该应用程序无法运行,并且有消息称该数据库不存在。

数据库位于资产目录的脚本文件中,这是DB.java中使用的代码:

    package fr.app.tutorielfragment;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
import android.content.ContextWrapper;


public class DB extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 28;

    // Database Name
//    private static final String DATABASE_NAME = "contactsManager";
    private static final String DATABASE_NAME = "BASE2.db";



    public static final String METIER_TABLE_DROP = "DROP TABLE IF EXISTS " + DATABASE_NAME + ";";
    Context context;


    public DB(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // Store the context for later use
        this.context = context;
        Toast.makeText(context, "DB context",1000).show();



    }


    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase database) {
        String a = null;
        Log.d("aaa","Création des bases");
        Toast.makeText(context, "Création des bases",1000).show();
                        // creer la base avec le script
    executeSQLScript(database, "install7.sql");                     
        Log.d("aaa","Création des bases");
        a=a;
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        // Lorsque l'on change le numéro de version de la base on supprime la
        // table puis on la recrée
        if (oldVersion < DATABASE_VERSION) {    
            try
            {
                context.deleteDatabase(DATABASE_NAME);
                onCreate(db);
                Toast.makeText(context, "Supression des bases",1000).show();
                //              db.execSQL("DROP TABLE " + "base_codematiere" + ";");

            } 
            catch (SQLException e) 
            {
                Toast.makeText(context, "ERREUR pdt Supression des bases",1000).show();
                // TODO Handle Script Failed to Execute
                e=e;
                e=e;
            }

            db.close();
        }
        Toast.makeText(context, "onUpgrade",1000).show();
    }    

    public void removeAll()
    {
        // db.delete(String tableName, String whereClause, String[] whereArgs);
        // If whereClause is null, it will delete all rows.
        SQLiteDatabase db = this.getWritableDatabase(); // helper is object extends SQLiteOpenHelper
        db.delete(DATABASE_NAME, null, null);
//      db.delete(DatabaseHelper.TAB_USERS_GROUP, null, null);
    }

    private void executeSQLScript(SQLiteDatabase database, String dbname) 
    {
        int num=0;
        ByteArrayOutputStream outputStream = new  ByteArrayOutputStream();
        byte buf[] = new byte[1024];
        int len;
        AssetManager assetManager = context.getAssets();
        InputStream inputStream = null;

        try
        {
            inputStream = assetManager.open(dbname);
            while ((len = inputStream.read(buf)) != -1) 
            {
                outputStream.write(buf, 0, len);
            }
            outputStream.close();
            inputStream.close();

            String[] createScript = outputStream.toString().split(";");
            for (int i = 0; i < createScript.length; i++) 
            {
                String sqlStatement = createScript[i].trim();
                // TODO You may want to parse out comments here
                if (sqlStatement.length() > 0) 
                {
                    database.execSQL(sqlStatement + ";");
                    num=num+1;
                }
            }
            len=createScript.length;

        } 
        catch (IOException e)
        {
            // TODO Handle Script Failed to Load
            e=e;
            e=e;
        } 
        catch (SQLException e) 
        {
            // TODO Handle Script Failed to Execute
            e=e;
            e=e;
        }
    }



    }

有人可以解释一下为什么它在我的nexus4上正常工作以及为什么不在A501上工作?以及如何在A501上午餐我的应用程序?

提前,

更新:LogCat:

04-02 19:18:53.847: D/dalvikvm(15243): GC_FOR_ALLOC freed 2160K, 29% free 10164K/14151K, paused 21ms
04-02 19:18:53.877: I/SqliteDatabaseCpp(15243): sqlite returned: error code = 1, msg = statement aborts at 2: [BEGIN TRANSACTION;] cannot start a transaction within a transaction, db=/data/data/fr.app.tutorielfragment/databases/BASE2.db
04-02 19:18:53.877: D/aaa(15243): Création des bases
04-02 19:18:53.977: I/SqliteDatabaseCpp(15243): sqlite returned: error code = 1, msg = no such table: ID_DANGER_CLASSE_1, db=/data/data/fr.app.tutorielfragment/databases/BASE2.db
04-02 19:18:53.977: D/AndroidRuntime(15243): Shutting down VM
04-02 19:18:53.977: W/dalvikvm(15243): threadid=1: thread exiting with uncaught exception (group=0x40a681f8)
04-02 19:18:53.987: E/AndroidRuntime(15243): FATAL EXCEPTION: main
04-02 19:18:53.987: E/AndroidRuntime(15243): android.database.sqlite.SQLiteException: no such table: ID_DANGER_CLASSE_1: , while compiling: SELECT * FROM ID_DANGER_CLASSE_1 ;
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1538)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at fr.app.tutorielfragment.A_Page_Classe1.RD_Database_InfoClasse(A_Page_Classe1.java:47)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at fr.app.tutorielfragment.A_Page_Classe1.onActivityCreated(A_Page_Classe1.java:32)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1468)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1012)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1366)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.view.View.measure(View.java:12728)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.view.View.measure(View.java:12728)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.view.View.measure(View.java:12728)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2118)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.view.View.measure(View.java:12728)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.os.Looper.loop(Looper.java:137)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at android.app.ActivityThread.main(ActivityThread.java:4424)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at java.lang.reflect.Method.invokeNative(Native Method)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at java.lang.reflect.Method.invoke(Method.java:511)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-02 19:18:53.987: E/AndroidRuntime(15243):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

no such table: ID_DANGER_CLASSE_1: , while compiling: SELECT * FROM ID_DANGER_CLASSE_1 ;

从日志中看,您的表尚未创建。

3.877: I/SqliteDatabaseCpp(15243): sqlite returned: error code = 1, msg = statement aborts at 2: [BEGIN TRANSACTION;] cannot start a transaction within a transaction, db=/data/data/fr.app.tutorielfragment/databases/BASE2.db
04-02 19:18:53.877: D/aaa(15243): Création des bases

尝试调试executeSQLScript方法,并查看它的失败位置。

此行之后

database.execSQL(sqlStatement + ";");

尝试打印出sqlStatement的值。