使用多个表的最佳实践
我在应用程序中使用包含多个表的数据库。我有一个 XML 解析器,需要在解析时将数据写入两个表。我为两个表创建了两个数据库适配器,但现在遇到了问题。当我使用一个表时,这很简单:
FirstDBAdapter firstTable = new FirstDBAdapter(mycontext);
firstTable.open(); // open and close it every time I need to insert something
// may be hundreds of times while parsing
// it opens not a table but whole DB
firstTable.insertItem(Item);
firstTable.close();
因为它是一个 SAX 解析器,在我看来(也许我错了),这会更好:
FirstDBAdapter firstTable = new FirstDBAdapter(mycontext);
@Override
public void startDocument() throws SAXException
{
firstTable.open(); // open and close only once
}
...
firstTable.insertItem(Item);
...
@Override
public void endDocument() throws SAXException
{
firstTable.close();
}
但是如果我需要将数据插入到第二个表中,我该怎么做桌子?例如,如果我有第二个适配器,我认为这将是一个坏主意:
FirstDBAdapter firstTable = new FirstDBAdapter(mycontext);
SecondDBAdapter secondTable = new SecondDBAdapter(mycontext);
@Override
public void startDocument() throws SAXException
{
firstTable.open();
secondTable.open();
}
关于如何实现这一点有什么想法吗?
I use a database with multiple tables in my application. I have an XML parser which needs to write data to two tables while parsing. I created two database adapters for both tables, but now I have a problem. When I'm working with one table, it's easy:
FirstDBAdapter firstTable = new FirstDBAdapter(mycontext);
firstTable.open(); // open and close it every time I need to insert something
// may be hundreds of times while parsing
// it opens not a table but whole DB
firstTable.insertItem(Item);
firstTable.close();
Since it's a SAX parser, in my opinion (maybe I'm wrong), this will be even better:
FirstDBAdapter firstTable = new FirstDBAdapter(mycontext);
@Override
public void startDocument() throws SAXException
{
firstTable.open(); // open and close only once
}
...
firstTable.insertItem(Item);
...
@Override
public void endDocument() throws SAXException
{
firstTable.close();
}
But how do I do it if I need to insert data to the second table? For example, if I have the second adapter, which I think will be a bad idea:
FirstDBAdapter firstTable = new FirstDBAdapter(mycontext);
SecondDBAdapter secondTable = new SecondDBAdapter(mycontext);
@Override
public void startDocument() throws SAXException
{
firstTable.open();
secondTable.open();
}
Any thoughts on how to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我已经成功地使用数据库名称/创建语句和其他共享信息创建了一个抽象基类,然后为每个表扩展了它。这样,我可以将所有 CRUD 方法分开(我更喜欢这样)。唯一的缺点是 DATABASE_CREATE 语句必须驻留在父类中,并且必须包含所有表,因为之后无法添加新表,但在我看来,保持 CRUD 的代价很小每个表的方法是分开的。
这样做相当简单,但这里有一些注意事项:
这是我的抽象父类的代码,它基于记事本教程。孩子们只需扩展它,调用超级的构造函数(随意使用它):
这里有更详细的解释:http://pheide.com/page/11/tab/24#post13
I've had success with creating an abstract base class with the database name/create statement and other shared info, and then extending it for every table. This way, I can keep all of my CRUD methods separate (which I much prefer). The only downside is that the DATABASE_CREATE statement(s) must reside in the parent class, and must include all of the tables, because new tables can't be added afterwards, but in my opinion that's a small price to pay to keep the CRUD methods for each table separate.
Doing this was fairly simple, but here are some notes:
Here is the code for my abstract parent class, which was based on the Notepad Tutorial. The children simply extend this, calling the super's constructor (feel free to use this):
A slightly more detailed explanation is available here: http://pheide.com/page/11/tab/24#post13
我的数据库适配器。实例始终存储在继承自 Application 的 MyApplication 中。想想我定义第一个表的第二个表...目前这只是一个简短的版本,实际上这个适配器处理数据库中的 7 个表。
My database adapter. An instance is always stored in MyApplication which inherites from Application. Just think about a second table where I defined the first one... currently this is just a short version, in reality this adapter handles 7 tables in the database.
phoxicle 的解决方案是一个很好的起点,但根据 Kevin Galligan 的 Android SQLite 序列化说明,此实现不是线程安全的,并且当多个数据库连接(例如来自不同线程)尝试写入数据库时,将默默失败:
下面是 phoxicle 数据库适配器的修改实现,它使用静态 SQLiteOpenHelper 实例,因此仅限于单个数据库连接:
为每个表扩展 DBBaseAdapter 以实现 CRUD 方法:
phoxicle's solution is a great starting point, but per Kevin Galligan's notes on Android's SQLite serialization, this implementation isn't thread safe and will fail silently when multiple database connections (e.g. from different threads) try to write the database:
Here's a modified implementation of phoxicle's database adapter that uses a static SQLiteOpenHelper instance and is thus limited to a single database connection:
Extend DBBaseAdapter for each table to implement your CRUD methods:
我可能有点晚了,但我总是打开我的数据库,而不是我的表。所以这对我来说毫无意义。
不如这样做。
然后,如果您想更新,请选择表格:
仅此而已。希望它可以帮助其他人
I'm a bit late maybe but i always open my database, not my table. So this form me as no sense.
Rather do this.
then if you want to update juste chose the table:
And that's all. Hope it can help other people