在我的 SQLite 数据库中使用什么主键?
我有一个 .csv 文件,我将其转换为 SQLite 数据库,其中第一列作为日期和时间(作为 TEXT 数据类型,因为它的格式为 7/20/2011 12:00:00 PM),第二列作为亚秒(实数,如 0.142857),其余列作为该时间的数据。我将在我的 Android 应用程序中将其用作只读数据库,用户在其中选择开始时间和结束时间,然后以图形方式显示该时间段的数据。
这是我第一次使用数据库,我不知道如何使用它。我相信每行都需要一个唯一的 NUMERIC 主键,但简单地按行号标记它们是浪费空间。第一列不是唯一的,无论如何都是 TEXT 数据类型,但如果要以某种方式合并亚秒,那么它将是唯一的。但我真的无法重写数据库,因为它有 65534 行...您建议我如何访问该数据库中的特定时间?
I have a .csv file that I turned into an SQLite database with the first column as a date and time (as a TEXT datatype because it's in the format 7/20/2011 12:00:00 PM), the second column as the subsecond (a REAL, like 0.142857), and the rest of the columns as data about that time. I will be using it as a read-only db in my Android app, in which the user selects a start time and an end time and then the data is displayed graphically for that time period.
This is my first time using a database and I'm not sure how to work with this. I believe I need a unique NUMERIC primary key for each row but simply labeling them by row number is a waste of space. The first column is not unique and is a TEXT datatype anyway, though if the subsecond was to be somehow incorporated then it would be unique. But I really can't re-write the database because it has 65534 rows... How do you suggest I access specific times in this database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Android 中,您的数据库中需要一个名为 _ID 的列(否则稍后您将遇到一些问题)。您将使用它作为主键。
日期和时间可以按以下格式以文本列的形式存储在 SQLite 中(请参阅 http:// www.sqlite.org/datatype3.html):
如果您的数据库是静态的,只需使用 SQLite 数据库浏览器等工具将其转换为适合 Android 的格式即可。
In Android you need a column named _ID in your database (else you'll face some issues later on). You will use that as the primary key.
Dates and times can be stored in SQLite in the form of a text column in the following format (See http://www.sqlite.org/datatype3.html):
If your database is static, simply use a tool such as SQLite Database Browser to convert it to a format convenient for Android.
如果您的数据库是本地和外部的(不是远程的),那么您必须有 _id 和另一个表 android_metadata 来保存区域设置。
如果您的数据库是远程的。是的,你可以,如果你会写,那只是速度问题,因为你不写。使用 WHERE 子句即可完成这项工作。
If your database is local and external(not remote), than you must have _id and another another table android_metadata which will hold the locale.
If your database was remote. Yes, you can it is only matter of speed if you are write, since you don't. Using WHERE clause will do the work.
每个日期都可以非常容易地转换为数字时间戳:
数字比文本字段更容易、更快处理。如果您完全确定列中具有唯一数据,则可以将其用作主键。
将 csv 文件导入表也应该很容易:
您必须在用于搜索或排序数据的每一列上创建索引。另请注意,表中的行没有“默认”、“自然”或任何其他顺序。如果你执行同一个 select 语句两次,你会得到两个完全不同的排序结果。所以简单的 select 语句应该如下所示:
Every date can be converted to numeric timestamp quite easy:
Numbers are MUCH easier and faster to process than text fields. If you are completely sure, that you have unique data within column you can use it as primary key.
Importing csv file into table should be also easy:
You have to create indexes on every column which will be used to search or order data. Please be also aware, that rows in table has no "default", "natural" or any other order. If you execute this same select statement twice you can get two totally different results in meaning of sorting. So simple select statement should look like that: