- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用 OrmLite 保存我的数据。在这个例子中有这个类:
package com.example.helloandroid;
import java.sql.SQLException;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
/**
* Database helper class used to manage the creation and upgrading of your database. This class also usually provides
* the DAOs used by the other classes.
*/
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
// name of the database file for your application -- change to something appropriate for your app
private static final String DATABASE_NAME = "helloAndroid.db";
// any time you make changes to your database objects, you may have to increase the database version
private static final int DATABASE_VERSION = 1;
// the DAO object we use to access the SimpleData table
private Dao<SimpleData, Integer> simpleDao = null;
private RuntimeExceptionDao<SimpleData, Integer> simpleRuntimeDao = null;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config);
}
/**
* This is called when the database is first created. Usually you should call createTable statements here to create
* the tables that will store your data.
*/
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
try {
Log.i(DatabaseHelper.class.getName(), "onCreate");
TableUtils.createTable(connectionSource, SimpleData.class);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
throw new RuntimeException(e);
}
// here we try inserting data in the on-create as a test
RuntimeExceptionDao<SimpleData, Integer> dao = getSimpleDataDao();
long millis = System.currentTimeMillis();
// create some entries in the onCreate
SimpleData simple = new SimpleData(millis);
dao.create(simple);
simple = new SimpleData(millis + 1);
dao.create(simple);
Log.i(DatabaseHelper.class.getName(), "created new entries in onCreate: " + millis);
}
/**
* This is called when your application is upgraded and it has a higher version number. This allows you to adjust
* the various data to match the new version number.
*/
@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
try {
Log.i(DatabaseHelper.class.getName(), "onUpgrade");
TableUtils.dropTable(connectionSource, SimpleData.class, true);
// after we drop the old databases, we create the new ones
onCreate(db, connectionSource);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
throw new RuntimeException(e);
}
}
/**
* Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached
* value.
*/
public Dao<SimpleData, Integer> getDao() throws SQLException {
if (simpleDao == null) {
simpleDao = getDao(SimpleData.class);
}
return simpleDao;
}
/**
* Returns the RuntimeExceptionDao (Database Access Object) version of a Dao for our SimpleData class. It will
* create it or just give the cached value. RuntimeExceptionDao only through RuntimeExceptions.
*/
public RuntimeExceptionDao<SimpleData, Integer> getSimpleDataDao() {
if (simpleRuntimeDao == null) {
simpleRuntimeDao = getRuntimeExceptionDao(SimpleData.class);
}
return simpleRuntimeDao;
}
/**
* Close the database connections and clear any cached DAOs.
*/
@Override
public void close() {
super.close();
simpleRuntimeDao = null;
}
}
它引用了 SimpleData 类。如果我想持久化各种类的对象,我是否必须为我想要持久化的每个类创建一个 DatabaseHelper 类?
最佳答案
当然不是,你可以定义很多类来持久化和做:
(new DatabaseHelper(getApplicationContext())).getDao(TheClassYouWant.class)
获取任何道。不要忘记在 onCreate、onUpgrade 中定义每个表:
TableUtils.createTable(connectionSource, SimpleData.class);
TableUtils.createTable(connectionSource, OtherSimpleData.class);
关于android - OrmLite Android DatabaseHelper 用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18098188/
使用 ServiceStack.OrmLite,如果查询返回多个结果集,我如何访问所有结果集并将每个结果集分配给相应的 POCO。例如,我有一个包含以下代码的存储过程: SELECT * FROM U
首先,我是 ORMLite 的新手。我希望我的模型类有一个字段,它是一个字符串列表,最终将保存我的模型对象的标签列表。 我应该使用哪些 ORMLite 注释? 首先我不想拥有所有标签的表格,然后使用
在 Ormlite 中,是否可以在不编写实际 SQL 的情况下进行不区分大小写的查询? 例如,如果我正在寻找 列名 - “账户名” 并查询该列,如果我搜索“金融”,我想获得所有“金融”、“金融”、“金
我最近开始使用 ServiceStack 及其 ORMLite 框架。我在谷歌上搜索并浏览了源代码,但找不到任何相关内容。 使用 ORMLite 执行查询时,有没有办法选择特定的列? 类似的东西:Db
我正在寻找一种在 ormlite 中实现分页的好方法,我发现了另一个 question ,其中包含以下代码段: var data = db.Select(predicate).Skip((int) p
这是 Entity Framework : var department = _context.Departments .Include(dep => dep.Empl
我有一张 table CREATE TABLE [dbo].[ServiceTestCase]( [SSN] [int] IDENTITY(600000001,1) NOT NULL,
有没有办法用 servicestack/ormlite 预加载所有嵌套和子嵌套引用? public class Person { public int Id { get; set; }
继 this comment ,如何执行 ServiceStack OrmLite 查询来连接两个或多个表并从每个表中返回一些列? 使用 OrmLite Does_only_populate_Sele
我想知道是否可以查明 ORMLite 的 dao.createOrUpdate() 方法是否实际创建或更新了表行。有一个结果对象(CreateOrUpdateStatus),其中包含这些信息,但所有指
有什么方法可以返回 ServiceStack.OrmLite 中的表的子集吗? 像这样的东西: public class MyStuff { public Guid Id { get; set
使用 ormLite 我可以通过以下方式获取所有记录: myDao.queryForAll(); 如何只获取前 10 条记录而不是所有记录? 最佳答案 您必须使用 QueryBuilder 并设置限制
我正在 ServiceStack 的 OrmLite 中编写分页查询,选择页面范围内的总记录数和记录 ID。假设 query 是一些任意的 SqlExpression 选择一堆记录: var idQu
我正在努力用 ServiceStack 的 ORMLite 替换现有的“重型”商业 ORM。在重型 ORM 中,我们有能力 Hook “OnSaving”或“BeforeSaving”方法以在保存到数
我正在尝试 ServiceStack OrmLite,但现在我被这个异常难住了:A first chance exception of type 'System.NullReferenceExcept
我正在尝试 Ormlite。我发现当我插入一个带有 DateTime 属性的对象时,ormlite 应用它得到 -8:00 (我的时区是 +8)。应按字面意思插入时间。就我而言,它已经是 UTC。 但
我正在对现有的 SQL Server 数据库使用 OrmLite,该数据库已发布用于访问的存储过程。这些 SP 之一采用 3 个 int 参数,但期望其中一个或另一个为空。但是,没有任何参数被声明为可
我正在为数据可视化做一些查询,并依靠 GroupBy、Avg、Sum 和类似函数从数据库中获取良好的数据集。 我想在 ServiceStack OrmLite 中使用类似于 GroupBy 的东西。关
我希望使用 ORMLite 按多个别名表进行分组,但我似乎遇到了问题。 当在 SqlExpression 的 GroupBy 中使用具有匿名类型的 Sql.TableAlias 时,为 group b
这个周末我才第一次发现 ServiceStack,我觉得它非常棒。因此,我已经在将我所有的项目转换为它。然后我遇到了一个小障碍。 我找不到任何提到使用 OrmLite 首先从数据库开始然后将现有模式映
我是一名优秀的程序员,十分优秀!