本文介绍了具有现有DB&的Android Sugar ORM自定义文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过提供的示例,我完全能够使用Sugar ORM.

I'm perfectly able to use Sugar ORM using provided examples.

在我的用例中,我从服务器上下载了一个SQLite数据库(因为它的ETL负载是在数百万条记录中,所以必须在服务器端完成).下载内容将保存到内部存储中的自定义路径.

In my usecase I download a SQLite DB from the server (ETL load for it is in millions of records so has to be done server side). The download saves to a custom path on internal storage.

就我而言,我不需要基于POCO的动态数据库创建.

In my case I do not need dynamic DB creation based on POCOs.

如果所有POCO类字段均与表结构匹配,是否可以将Sugar ORM与预先存在的SQLite DB一起使用,并指向自定义路径?

Is it possible to use Sugar ORM with pre-existing SQLite DB, pointing to a custom path, provided if all POCO classes fields match the table structure?

推荐答案

  1. 首先,我对Sugar扩展的想法不满意应用程序类.如果我还有其他任务需要怎么办在应用启动之前?!因此,让我们用自己的工具扩展SugarApp然后,AppClass在清单中注册appClass名称.另外,我相信这是第一次初始化db的正确位置.

  1. First of all, I am not comfortable with the idea that Sugar extendsthe app class. What if I have other tasks need to be carried outbefore app start?! So let's extend SugarApp with our ownAppClass then register the appClass name in manifest. Also, this is the right place to init db the first time I believe.

public class MyAppStartClass extends SugarApp {

@Override
public final void onCreate() {
    init();
    super.onCreate();
}

private void init() {
    initDB();
}

private void initDB() {
    try {
        if (!doesDatabaseExist(this, consts.dbPath)) {
            Context context = getApplicationContext();
            SQLiteDatabase db = context.openOrCreateDatabase(consts.dbName, context.MODE_PRIVATE, null);
            db.close();
            InputStream dbInput = getApplicationContext().getAssets().open(consts.dbName);
            String outFileName = consts.dbPath;
            OutputStream dbOutput = new FileOutputStream(outFileName);
            try {
                byte[] buffer = new byte[1024];
                int length;
                while ((length = dbInput.read(buffer)) > 0) {
                    dbOutput.write(buffer, 0, length);
                }
            } finally {
                dbOutput.flush();
                dbOutput.close();
                dbInput.close();
            }
        }
    } catch (Exception e) {
        e.toString();
    }
}

private boolean doesDatabaseExist(ContextWrapper context, String dbName)       {
    File dbFile = context.getDatabasePath(dbName);
    return dbFile.exists();
}
}

  • 清单:android:name ="com.myPackageName.MyAppStartClass"

  • Manifest: android:name="com.myPackageName.MyAppStartClass"

    请确保首先创建一个空的数据库,否则,您将从FileOutputStream()中得到一个错误,并且dbPath =/data/data/com.myPackageName/databases/myDb.db

    Make sure you create an empty db first, if you don't you'll get an error from FileOutputStream() and dbPath = /data/data/com.myPackageName/databases/myDb.db

    SQLiteDatabase db = context.openOrCreateDatabase(consts.dbName,context.MODE_PRIVATE,null);

    SQLiteDatabase db = context.openOrCreateDatabase(consts.dbName, context.MODE_PRIVATE, null);

    db.close();

    db.close();

    确保现有的数据库模式具有主键列ID.哦耶! Sugar只将ID视为检索数据的主键.

    Make sure your existing db schema has a primary key column ID. Oh yeah! Sugar only sees ID as primary key to retrieve data.

    如果要使用现有表,则在扩展SugarRecord时不要指定T,而必须将Sugar添加为模块,并且项目依赖于它!

    If you want to use existing tables, do NOT specify T when you extend SugarRecord AND you have to add Sugar as a module and your project depends on it!

    public class Book extends SugarRecord {
      String title;
      String edition;
    
      public Book(){
      }
    
      public Book(String title, String edition){
        this.title = title;
        this.edition = edition;
      }
    }
    

  • 6.如果要使用现有表.请注意,Sugar会查找大写的列名,因此,如果您现有的表列名是小写字母,那么您将永远不会获得任何现有数据!

    6.If you want to use existing tables. Be aware that Sugar looks for UPPERCASE column names so if your existing table column names are lowercase you will never get any existing data out of it!

    7.这使我得出一个勉强的结论:Sugar是很棒的,如果您从头开始创建db并使用它为您生成db和表.但是,如果您已经有一个包含数据的现有数据库,则不是这样.

    7.That leads me to a reluctant conclusion: Sugar is great if your start db from scratch and use it to generate db and tables for you. But not so when you have already had an existing db with data in it.

    这篇关于具有现有DB&的Android Sugar ORM自定义文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    07-23 04:56
    查看更多