尝试转换现有的 SQLite 数据库以用于 Android 操作系统

发布于 2024-10-29 20:51:56 字数 472 浏览 0 评论 0原文

大家好,我有一个预先存在的 SQLite 数据库,我想将其与我的 Android 应用程序一起使用。我从头开始创建了一个示例数据库用于测试目的,其中每个主键名为 _id 并添加了表 android_metadata。这很好用。

因此,现在当我尝试重命名已有数据库的主键并将其上传到应用程序时,它不起作用。

谁能告诉我到底需要对现有数据库做什么才能使其与 Android 操作系统兼容?就像数据库中到底需要更改什么才能使其正常工作?

是的,我已经看过大多数教程,但大多数教程都没有详细说明您必须在预先存在的数据库中更改哪些内容。

这是我正在使用的数据库: http://www.mediafire.com/file/bpbpm19y6kbpjot/database.db

谢谢。

Hey guys, I have this pre-existing SQLite database that I want to use with my Android application. I have created a sample database from scratch for testing purposes where each primary key is named _id and also adding the table android_metadata. This works great.

So now when I've tried to rename the primary keys of the database I already have, and upload it to the application, it doesn't work.

Can anyone tell me what exactly I have to do to my existing database to get it to work with the Android OS? Like what exactly has to be changed in the database for it to work?

And yes, I have looked at most tutorials, but most of them don't go into detail about what you have to change in the pre-existing database.

Here is the database I am using:
http://www.mediafire.com/file/bpbpm19y6kbpjot/database.db

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我为君王 2024-11-05 20:51:56

我通常在调用 SQLiteDatabase.openDatabase() 时设置标志 NO_LOCALIZED_COLLATORS 。那么你就不需要 android_metadata 表。据我所知, _id 列也必须是 INTEGER PRIMARY KEY AUTOINCRMENT 类型。

I usually set the flag NO_LOCALIZED_COLLATORS when calling SQLiteDatabase.openDatabase(). Then you don't need the android_metadata table. As far as I know the _id column also must be of the type INTEGER PRIMARY KEY AUTOINCREMENT.

安穩 2024-11-05 20:51:56

您实际上并不需要将主 ID 列命名为 _id - 您只需在 select 语句中使用类似 SELECT my_id as _id, another_field ... 的内容即可。

您可以按照上面的 Omokoii 操作并设置 NO_LOCALIZED_COLLATORS 标志,也可以创建 android_metadata 表并插入值 en-US进入其中。

至于使用现有的数据库,也许这篇博客文章可能会有所帮助:http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

You don't actually need the primary ID column to be named _id -- you can just use something like SELECT my_id as _id, another_field ... in your select statement.

And you can either do as Omokoii said above and set the NO_LOCALIZED_COLLATORS flag, or you can create the android_metadata table and insert the value en-US into it.

As for using an existing DB, perhaps this blog post might help: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

谈场末日恋爱 2024-11-05 20:51:56

确保现有的 SQLite 数据库仅使用“INTEGER”(逐字)声明整数主键,而不是“int”或“int16”或任何其他声明 SQLite 将识别的整数的可能性。

我在 Adob​​e AIR 中导入 SQLite 数据库时遇到了相关问题(该数据库与 Goodle 和 Mozilla 以及其他联盟成员 IIRC 具有共同的代码库)。我的 PK 被定义为“int”(逐字)。 SQLite 对待“INTEGER”主键与对待“int”或“INT”或“int16”等主键不同!

记录在此处:http://www.sqlite.org/datatypes.html

SQLite 将 INTEGER 主键视为 RowId 的同义词。任何其他 int 类型就像标准列,并且对于标准列 RowId 不一定等于 PK 列中的值。

然而,Adobe 和 SQLite 联盟成员的其他相关子组并未实现此(已记录的)行为 - 对于他们来说,用作 PK 列的任何/每个整数类型都被视为行 id 的同义词-- 当预先存在的 SQLite 数据库导入到其实现中时,如果预先存在的数据库在声明其整数类型主数据库时使用除“INTEGER”以外的任何内容,则它们未能实现这种区别可能会导致错误的连接键。

PS 我将此问题引起了 Adob​​e 的注意,并在 SQLite 邮件列表和 Adob​​e AIR 论坛上进行了令人厌烦的讨论。 Adobe 写信给我说,他们会记录其与“标准”SQLite 行为的偏离,但保持原样,因此我相信 Android 在这方面也将不同于 SQLite 记录的行为。

PPS 看来这个联盟成员小组要么没有预见到导入数据库的可能性(即他们假设数据库总是通过他们的界面重新创建),要么他们只是忽略了 SQLite 中的这种(诚然是靠不住的)异常行为。

例如,如果通过未实现“标准”INTEGER/int(等)的 SQLite 实现附加该表,OP 正在使用的数据库中的该表在涉及 [stop_id] 列上的联接时将返回虚假结果异常行为,但在与 PK 一起使用时将任何/每个 int 类型视为 rowid 的同义词:

  CREATE TABLE mt_stop (
    stop_id  int  NOT NULL PRIMARY KEY ASC,
    stop_lat real NOT NULL CHECK (stop_lat >= -90 AND stop_lat <= 90),
    stop_lon real NOT NULL CHECK (stop_lon >= -180 AND stop_lon <= 180),
    stop_name varchar (120) DEFAULT 'Unknown'
)

Make sure that your existing SQLite database declares integer primary keys using only "INTEGER" (verbatim)--not "int" or "int16" or any of the other possibilities for declaring an integer that SQLite will recognize.

I ran into a related problem when importing a SQLite database in Adobe AIR (which has a common codebase with Goodle and Mozilla and other consortium members, IIRC). My PK had been defined as "int" (verbatim). SQLite treats "INTEGER" primary keys differently than it treats "int" or "INT" or "int16" etc primary keys!

Documented here: http://www.sqlite.org/datatypes.html

An INTEGER primary key is treated by SQLite as a synonym for the RowId. Any other int type is just like a standard column, and with a standard column RowId will not necessarily equal the value in the PK column.

However, Adobe and the other related subgroup of SQLite consortium members did not implement this (documented) behavior--for them any/every integer type used as the PK column is treated as a synonym for the row id-- and their failing to implement this distinction can result in erroneous joins when a pre-existing SQLite database is imported into their implementation(s), if the pre-existing database used anything other than "INTEGER" when declaring its integer-type primary keys.

P.S. I brought this to Adobe's attention and discussed it ad nauseam on the SQLite mailing list and on the Adobe AIR forum. Adobe wrote me that they would document their departure from "standard" SQLite behavior but leave it as is, so I believe Android will also differ from SQLite documented behavior in this regard.

P.P.S. It seems this subgroup of consortium members either did not envision the possibility that a database would be imported (i.e. they assumed the database would always be created anew via their interface) or they simply overlooked this (admittedly wonky) exceptional behavior in SQLite.

P.P.P.S. This table, for example, from the database the OP is using would return spurious results when involved in joins on the [stop_id] column if attached by an implementation of SQLite that did not implement the "standard" INTEGER/int (et al) exceptional behavior but treated any/every int-type when used with the PK as a synonym for the rowid:

  CREATE TABLE mt_stop (
    stop_id  int  NOT NULL PRIMARY KEY ASC,
    stop_lat real NOT NULL CHECK (stop_lat >= -90 AND stop_lat <= 90),
    stop_lon real NOT NULL CHECK (stop_lon >= -180 AND stop_lon <= 180),
    stop_name varchar (120) DEFAULT 'Unknown'
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文