Android SQLite 和数据库方案上的 ORM

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2973982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:07:59  来源:igfitidea点击:

ORM on Android SQLite and database scheme

androidsqliteormmany-to-manycrud

提问by BlackLabrador

I'm looking for a very simple ORM framework working on Android for SQLite. I've been testing ActiveAndroidbut none of the example could ever build on Eclipse.
By the way, how do guys implement a "many to many" relationship in SQLite for Android? How do you reflect the cascade regarding deletion of rows and guarantee the database integrity?

我正在寻找一个非常简单的 ORM 框架,适用于 Android for SQLite。我一直在测试ActiveAndroid,但没有一个示例可以在 Eclipse 上构建。
顺便说一句,人们如何在 Android 的 SQLite 中实现“多对多”关系?你如何反映行删除的级联并保证数据库的完整性?

回答by Gray

I am the main author of ORMLitewhich has a Android backend which makes calls to the native Android OS database APIs to support its ORM functionality. We have a large number of Android developers that are using the framework successfully. See here for more information:

我是ORMLite的主要作者,它有一个 Android 后端,它调用原生 Android 操作系统数据库 API 以支持其 ORM 功能。我们有大量的 Android 开发人员成功地使用了该框架。浏览此处获取更多信息:

http://ormlite.com/sqlite_java_android_orm.shtml

http://ormlite.com/sqlite_java_android_orm.shtml

In terms of many-to-many relationships, ORMLite does not support cascading or any of the more advanced ORM features but there are examples of easy many-to-many implementations:

在多对多关系方面,ORMLite 不支持级联或任何更高级的 ORM 功能,但有一些简单的多对多实现示例:

http://ormlite.com/docs/examples

http://ormlite.com/docs/examples

回答by Markus Junginger

For those still looking for an ORM solution, I released greenDAOsome months ago. Several apps in the Android Market already use it. Unlike other Android ORM tools, one of greenDAOs primary design goals was performance. For many operations it should be multiple times faster than other solutions, e.g. it's 4-5 times faster compared to ORMLite for loading entities.

对于那些仍在寻找 ORM 解决方案的人,我几个月前发布了greenDAO。Android Market 中的几个应用程序已经在使用它。与其他 Android ORM 工具不同,greenDAO 的主要设计目标之一是性能。对于许多操作,它应该比其他解决方案快数倍,例如,与加载实体的 ORMLite 相比,它要快 4-5 倍。

It supports relations. The documentation describes how to use relations and how you could model many-to-many relations.

它支持关系。该文档描述了如何使用关系以及如何建模多对多关系

Delete-cascade is a dangerous thing, and thus unsupported by greenDAO. The safer way is to delete the entities bottom-up inside a transaction.

删除级联是一件危险的事情,因此不受 greenDAO 支持。更安全的方法是在事务中自下而上地删除实体。

回答by philgiese

I wrote a lightweight ORM myself and called it Androrm. As I love Django, the query syntax looks much alike. Please try and give me feedback :)

我自己写了一个轻量级的 ORM,叫它 Androrm。因为我喜欢 Django,所以查询语法看起来很相似。请尝试给我反馈:)

Webpage: http://androrm.com/

网页:http: //androrm.com/

Also on GitHub: https://github.com/androrm/androrm

同样在 GitHub 上:https: //github.com/androrm/androrm

回答by YongJiang Zhang

Below is how I do it with SORMA - An Android Content Provider solution.

下面是我如何使用 SORMA - Android 内容提供程序解决方案。

  1. Map your Java POJO to database table:

    @Table(
    name="contact",
    keyColumn="id",
    autoId=true,
    create="create table if not exists contact (" 
    + " id INTEGER primary key autoincrement"
    + ", firstName text"
    + ", lastName text"
    + ", married tinyint"
    + ")"
    )
    public class Contact {
    private Integer id;
    private String firstName;
    private String lastName;
    private boolean married;
    ......
    
  2. Create content provider class:

    import com.gaoshin.sorma.annotation.ContentProvider;
    import com.gaoshin.sorma.annotation.SormaContentProvider;
    
    @ContentProvider(
        version = 1,
        mappingClasses = {
            Contact.class,
            Phone.class 
        }
    )
    public class AddressBookContentProvider extends SormaContentProvider {
    }
    
  3. Define content provider in the AndroidManifest.xml:

    <provider    
    android:authorities="com.gaoshin.sorma.examples.addressbook.AddressBookContentProvider"
    android:name="com.gaoshin.sorma.examples.addressbook.AddressBookContentProvider" />
    
  4. Use the content provider:

    sorma = SORMA.getInstance(getBaseContext(), AddressBookContentProvider.class);
    
    // insert contact
    Contact contact = new Contact();
    contact.setFirstName("fname1");
    contact.setLastName("lname1");
    sorma.insert(contact);
    
  1. 将您的 Java POJO 映射到数据库表:

    @Table(
    name="contact",
    keyColumn="id",
    autoId=true,
    create="create table if not exists contact (" 
    + " id INTEGER primary key autoincrement"
    + ", firstName text"
    + ", lastName text"
    + ", married tinyint"
    + ")"
    )
    public class Contact {
    private Integer id;
    private String firstName;
    private String lastName;
    private boolean married;
    ......
    
  2. 创建内容提供者类:

    import com.gaoshin.sorma.annotation.ContentProvider;
    import com.gaoshin.sorma.annotation.SormaContentProvider;
    
    @ContentProvider(
        version = 1,
        mappingClasses = {
            Contact.class,
            Phone.class 
        }
    )
    public class AddressBookContentProvider extends SormaContentProvider {
    }
    
  3. 在 AndroidManifest.xml 中定义内容提供者:

    <provider    
    android:authorities="com.gaoshin.sorma.examples.addressbook.AddressBookContentProvider"
    android:name="com.gaoshin.sorma.examples.addressbook.AddressBookContentProvider" />
    
  4. 使用内容提供者:

    sorma = SORMA.getInstance(getBaseContext(), AddressBookContentProvider.class);
    
    // insert contact
    Contact contact = new Contact();
    contact.setFirstName("fname1");
    contact.setLastName("lname1");
    sorma.insert(contact);
    

Done!

完毕!

(You can find more info here.)

(您可以在此处找到更多信息。)

回答by Ian Warwick

I use, and am the author of, Mechanoid DB, that provides a sqlite like dsl for generating sqlite backed content providers.

我使用 Mechanoid DB 并且是 Mechanoid DB 的作者,它提供了一个像 dsl 这样的 sqlite 来生成 SQLite 支持的内容提供者。

Check it out:

一探究竟:

http://www.robotoworks.com/mechanoid-plugin/mechanoid-db/

http://www.robotoworks.com/mechanoid-plugin/mechanoid-db/

回答by Ryan Hayes

Try SQLiteGen. It's not as feature-rich as hibernate, but you can generate some simple ORM classes with it in Eclipse.

试试SQLiteGen。它不像 hibernate 那样功能丰富,但是您可以在 Eclipse 中用它生成一些简单的 ORM 类。

Hope that helps!

希望有帮助!