开发者社区> 问答> 正文

复制自定义的 sqlite 数据库到 android 中的错误

我想复制一个自定义的 sqlite 数据库到 android 中

public class DataBaseHelper extends SQLiteOpenHelper
{
private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window
private static String DB_PATH = "/data/data/mypackagename/databases/";//path of our database
private static String DB_NAME ="application-database";// Database name
private SQLiteDatabase mDataBase; 
private final Context mContext;
public DataBaseHelper(Context context) 
{
    super(context, DB_NAME, null, 1);
    DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
    this.mContext = context;
}   
public void createDataBase() throws IOException
{
    //If database not exists copy it from the assets
      boolean mDataBaseExist = checkDataBase();
    if(!mDataBaseExist)
    {
        this.getReadableDatabase();
        this.close();
        try 
        {
            //Copy the database from assests
            copyDataBase();
            Log.e(TAG, "createDatabase database created");
        } 
        catch (IOException mIOException) 
        {
            throw new Error("ErrorCopyingDataBase");
        }
    }
}

当运行到 InputStream mInput = mContext.getAssets().open(DB_NAME);
给出一个错误,程序直接跳转到

catch (IOException mIOException) 
        {
            throw new Error("ErrorCopyingDataBase");
        }

数据库是在 assets 文件夹中。
这个问题出在哪里呢?

展开
收起
蛮大人123 2016-06-16 15:43:26 1998 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
      /**
         * 
         * @param sqlQuery SQL query to be fired
         * @param myObj Object to be fetched
         * @return Returns a Vector object containing raws fetched by the sqlQuery
         */
    
        public ArrayList<Object> fetchAllRows(String sqlQuery, Object myObj)
        {
            ArrayList<Object> records = new ArrayList<Object>();
            Object newObj;
            Cursor cursor = execQuery(sqlQuery);
    
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    //          System.out.println("Test While");
                    newObj = ClassUtils.newObject(myObj);
                    for (int i = 0; i < cursor.getColumnCount(); i++) {
                        String key = cursor.getColumnName(i);
                        String value = cursor.getString(i);
                        if (value == null) {
                            value = "";
                        }
                        ClassUtils.objectMapping(newObj, key, value);
                    }
                    records.add(newObj);
                }
                cursor.close();
            }
            return records;
        }
        @Override
        public void onCreate(SQLiteDatabase db) 
        {
    
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
        {
    
        }
    }

    在第一个 activity 中添加下面的代码:

     Global.context = this;
     Global.dbMain = new DBConnect(this, Global.DB_MAIN);

    创建一个 Global.java 文件

    public class Global 
    {
        public static String DB_PATH = "data/data/YOUR_PACKAGE_NAME/databases/";
        public static final String DB_MAIN = "Database.sqlite";
    
        public static Context context;
        public static DBConnect dbMain;
    }

    现在假定你想从表格中选择信息

    Global.dbMain.openDataBase();
    System.out.println("database open ");
        try
        {
          Cursor c = Global.dbMain.execQuery("select * from tableName", null);
          while(c.moveToNext())
           {
             System.out.println("in while");
             String str= c.getString(1);
           }
          c.close();
        }
        catch(Exception e)
        {
          e.printStackTrace();
        }
    Global.dbMain.close();
    2019-07-17 19:41:06
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载