在 SQLite 中存储对象。是否可以?
我有一个非常复杂的问题,但是如果我能做到这一点,那么我基本上会一击解决。 基本上,我正在创建一个应用程序,该应用程序首先将在 iOS 和 Android 上运行,后来可能在 Blackberry 和 Windows Phone 上运行。问题是,我将使用 Dropbox 来保持所有这些设备同步。我需要保留的数据需要存储在 SQLite 数据库中(理想情况下,因为我无法在所有设备中使用 Core Data,并且 XML 很难在所有平台上使用)。我将创建一个自定义对象(在 C++ 中,或者一个结构体,或者任何我需要创建的对象,只要我可以在所有平台上工作),并且我希望能够将原始 C++ 对象存储在数据库中,然后再存储在 Android 中读取它的数据(以及后来的 Blackberry 和 WP)。
基本上,我的问题是,我可以将原始 C++ 对象存储在 SQLite 数据库中吗?因此能够在 Android 和 Windows Phone 上阅读它们?这听起来像是一个愚蠢的问题,因为 Android 编程是用 Java 完成的,而 WP 是用 C# 完成的,这让我怀疑我的 C++ 对象是否可以从数据库中读取,几乎没有问题。
I have a very complicated issue, but if I can do this, then I will basically solve it in a blow.
Basically, I'm creating an application that at first will run in both iOS and Android, and later possibly for Blackberry and Windows Phone. The problem is, that I will use Dropbox to keep all these devices in sync. The data I need to persist needs to be stored in a SQLite database (ideally, since I can't use Core Data in all the devices and XML is hard to work with in all platforms). I will create a custom object (in C++, or a struct, or anything I need to create as long as I can make this work in all platforms), and I want to be able to store the raw C++ object in the database and later read it's data in Android (and Blackberry and WP later).
So basically, my question is, can I store raw C++ objects in a SQLite database? And therefore be able to read them on Android and Windows Phone? It sounds like a silly question since Android programming is done in Java and WP is done in C#, and that's what makes me doubt whether my C++ objects could be read from the database with little to no problems.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测是,这可能类似于转储二进制数据(即 ie 的结构),答案是肯定的(blob 数据类型)。
不管怎样,如果我做类似的事情,为了简单性和可移植性,我会将编码的 json 存储在文本数据库字段中。请记住,此选择取决于许多事实,例如访问数据库以检索/保存这些对象的频率,或者对象有多大等等。
希望这会有所帮助
My guess is that it could be something like dumping binary data -an struct for i.e.- and the answer is yes (blob datatype).
Anyway, if I were doing something like that, I'd store encoded json in a text db field, for the sake of simplicity and portability. Keep in mind that this choice depends on many facts, for example how often the db will be accessed to retrieve/save these objects, or how big are the objects going to be, etc etc.
Hope this helps
听起来这里几乎有两个问题,我可能可以回答第一个问题,但对第二个问题不太确定。你的第一个问题,你能将编译后的 C++ 对象存储到 SQlite 中吗?答案是肯定的。 SQLite 有一个 BLOB 数据类型,您可以在其中存储二进制对象,并且它不会尝试对它们进行任何验证,它只会存储并返回它们,无论它如何获取它们(根本不进行验证)。这完全与您使用哪种语言编程无关,因为 Java 和 C# 不需要知道或关心这些对象中的内容。
至于我对你的第二个问题的解释是,我可以执行这些二进制对象吗?我倾向于说不。我认为,一旦你从 SQLite 中取出这些二进制对象,就没有任何机制可以执行这些二进制对象,至少在 Android 中是这样。 NDK 允许您编译和构建本机代码,但我不知道有什么方法可以动态加载本机对象。特别是因为你必须为所有你想要的目标编译它
希望它有帮助,
大卫
It sounds almost like there are two questions here, I can probably answer the first but am not so sure about the second. Your first question, can you store a compiled C++ object into SQlite, the answer would be yes. SQLite has a BLOB data type where you can store binary objects and it won't attempt to do any validation on them, it will just store and return them however it gets them (no validation at all). That's totally irrespective to what language you're programming in, because Java and C# don't need to know, or care, what's in those objects.
As for what I interpret your second question to be, can I execute these binary objects, my inclination is to say no. I don't think there is any mechanism, at least in Android, to execute these binary objects once you pull it out of SQLite. The NDK lets you compile and build native code, but I don't know of any way to dynamically load a native object. Especially since you'd have to compile it for all your desired targets
Hope it helps,
David